UESTC Training for Graph Theory——A、Railway

Railway

Time Limit: 3000 ms Memory Limit: 65535 kB Solved: 54 Tried: 399

Description

There are some locations in a park, and some of them are connected by roads. The park manger needs to build some railways along the roads, and he would like to arrange tourist routes to each circuit. If a railway belongs to more than one tourist routes, there might be clash on it, and if a railway belongs to none tourist route, it doesn?t need to build. 
Now we know the plan, and can you tell us how many railways are no need to build and how many railways where clash might happen.

Input

The Input consists of multiple test cases. The first line of each test case contains two integers, n (0 < n <= 10000), m (0 <= m <= 100000), which are the number of locations and the number of the railways. The next m lines, each line contains two integers, u, v (0 <= u, v < n), which means the manger plans to build a railway on the road between u and v. 
You can assume that there is no loop and no multiple edges. 
The last test case is followed by two zeros on a single line, which means the end of the input.

Output

Output the number of railways that are no need to build, and the number of railways where clash might happen. Please follow the format as the sample.

Sample Input

8 10 
0 1 
1 2 
2 3 
3 0 
3 4 
4 5 
5 6 
6 7 
7 4 
5 7 
0 0

Sample Output

1 5

Source

The 5th Guangting Cup Central China Invitatio

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

          

/*算法思想:
  给一个图,要求找出图中的桥的数量,以及同时属于两个环以上的边的数量。
  这道题目在用tarjan求图中的块的时候需要特殊的处理,比如这种情况:
      4---5
      |  /
      | /
      |/
      1---2
      |  /
      | /
      |/
      3
  这种情况就应该是当做两个联通块来处理,两个联通块分别是:
      4---5      1---2
      |  /       |  /
      | /        | /
      |/         |/
      1          3
  而不是当做整的一个联通块来处理,这样,我们就可以统计每个联通快中的边数edge和点数node,
  1、edge>node :这个联通块中所有边均是冲突边(同时属于两个环以上)
  2、edge<node :这个联通块中的边全是桥(实际上,它是这种情况:x---y,即联通块中只有两个点,一条边)
  3、edge=node : 这个联通块中的边既不是桥,也不是冲突边
  最后统计输出就行了
*/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#define N 10005
using namespace std;
int n,m;
struct data
{
    int en,next;
} edge[N*20];
int head[N],tot;
void add_edge(int st,int en)
{
    edge[tot].en=en;
    edge[tot].next=head[st];
    head[st]=tot++;
}
int block[N],sum1,sum2;
bool vs[N];
int dfn[N],low[N],Time;
stack<int>s;
void tarjin(int x,int from)
{
    low[x]=dfn[x]=Time++;
    s.push(x);
    for(int i=head[x];i!=-1;i=edge[i].next)
    {
        if(edge[i].en==from) continue;  //处理重边
        if(dfn[edge[i].en]==-1)
        {
            tarjin(edge[i].en,x);
            low[x]=min(low[x],low[edge[i].en]);
            if(low[edge[i].en]>=dfn[x])
            {
                block[0]=0;
                int v;
                memset(vs,0,sizeof(vs));
                do
                {
                    v=s.top();
                    s.pop();
                    vs[v]=1;
                    block[++block[0]]=v;
                }
                while(edge[i].en!=v);
                block[++block[0]]=x;
                vs[x]=true;
                int sum=0;

                for(int i=1;i<=block[0];i++)
                    printf("%d ",block[i]);
                printf("\n");

                for(int i=1;i<=block[0];i++)
                    for(int j=head[block[i]];j!=-1;j=edge[j].next)
                        if(vs[edge[j].en])
                        {
                            printf("%d--%d\n",block[i],edge[j].en);
                            sum++;
                        }
                sum/=2;
                printf("sum=%d block[o]=%d\n",sum,block[0]);
                if(block[0]>sum) sum1+=sum;
                if(block[0]<sum) sum2+=sum;

            }
        }
        else low[x]=min(dfn[edge[i].en],low[x]);
    }
}
int main()
{
    while(scanf("%d%d",&n,&m),n!=0 || m!=0)
    {
        memset(head,-1,sizeof(head));
        tot=0;
        int a,b;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&a,&b);
            add_edge(a,b);
            add_edge(b,a);
        }
        Time=1;
        memset(dfn,-1,sizeof(dfn));
        sum1=sum2=0;
        for(int i=1;i<=n;i++)
            if(dfn[i]==-1) tarjin(i,-1);
        printf("%d %d\n",sum1,sum2);
    }
    return 0;
}


 

 

你可能感兴趣的:(UESTC Training for Graph Theory——A、Railway)