Railway
Problem 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.
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.
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
Author
momodi@whu
Source
题意:给一个无向图。如果至少有两个环共用了一些边,那么这些边被认为是“冲突边”。如果一些边不在任何一个环中,这些边被认为是“多余边”。你要找出这个图中有多少“多余边”和“冲突边”然后输出条数。无重边
1.“多余边”不在任何一个环中,那么多余边一定是桥,所以统计这个无向图中有多少桥即可
2.“冲突边”:如果一个环有n个点刚好n条边,例如(1,2,3,1)这种环,这个环内,一条“冲突边”都没有,但是如果一个环内的边数大于点数,那么这个环内所有边都是“冲突边”(因为有多出来的那些边后,相当于把最外面的大环分割成了内部的几个小环,这些小环和小环之间,小环和大环之间一定会公用一些边,这些边就是“冲突边”,而且可以发现,所有边都会被公用),例如sample里面的(5,6)(5,4)(6,7)(4,7)(5,7),相当于最外面的大环<6,5,4,7,6> , 而里面的边(5,7)把这个大环分割成了两个小环(此处题意分析参考了其他人的)
一个没有关节点(割点)的连通图称为双连通图。
#include<stdio.h>
#include<string.h>
#include<vector>
#include<stack>
#include<algorithm>
#define maxn 10005
using namespace std;
int pre[maxn]; //时间戳
int bccno[maxn]; //记录i结点所属的连通分量编号
int dfs_clock,bcc_cnt,sum,n,cnt1;
vector<int> G[maxn];
struct edge
{
int U,V;
};
stack<edge> S;
int dfs(int u,int fa)
{
int v;
int lowu=pre[u]=++dfs_clock;
int child=0;
for(int i=0; i<G[u].size(); i++)
{
v=G[u][i];
edge e;
e.U=u;
e.V=v;
if(!pre[v])
{
S.push(e);
child++;
int lowv=dfs(v,u);
lowu=min(lowu,lowv);
if(lowv>=pre[u])
{
if(lowv>pre[u]) //uv为桥
{
cnt1++; //桥的计数
}
bcc_cnt++;
int nume=0;
for(;;)
{
edge x=S.top();
S.pop();
nume++;
if(bccno[x.U]!=bcc_cnt)
{
bccno[x.U]=bcc_cnt; //表示u结点属于bcc_cnt这个连通分量
}
if(bccno[x.V]!=bcc_cnt)
{
bccno[x.V]=bcc_cnt;
}
if(x.U == u && x.V == v) break;
}
int cnt=0;
for(int i=0;i<n;i++)
{
if(bccno[i]==bcc_cnt)
cnt++;
}
if(nume>cnt)
sum+=nume;
}
}
else if(pre[v] < pre[u] && v!=fa)
{
S.push(e);
lowu=min(lowu,pre[v]);
}
}
return lowu;
}
void find_bcc()
{
sum=0;
cnt1=0;
memset(pre,0,sizeof(pre));
memset(bccno,0,sizeof(bccno));
dfs_clock=bcc_cnt=0;
for(int i=0; i<n; i++)
{
if(!pre[i])
dfs(i,-1);
}
}
int main()
{
int m,i,a,b,j,cnt;
while(~scanf("%d%d",&n,&m)&&(n||m))
{
cnt=0;
for(i=0; i<n; i++)
G[i].clear();
for(i=1; i<=m; i++)
{
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
}
find_bcc();
printf("%d %d\n",cnt1,sum);
}
return 0;
}