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
题意:给你一个图,求出其中桥的个数和同时处于多个圈中的边的个数
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL INF = 1000000000;
const LL MAX = 1e5 + 50;
int n, m;
struct Edge
{
int to, next;
} edge[MAX * 2];
int head[MAX];
int k = 0;
void add(int a, int b){
edge[k].to = b;
edge[k].next = head[a];
head[a] = k++;
}
int dfn[MAX], low[MAX], vis[MAX];
int ans1, ans2, tim;
int s[MAX], top;
int cnt = 0, node[MAX];
void init(){
memset(head, -1, sizeof(head));
k = ans1 = ans2 = top = 0;
tim = 0;
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
}
void cal(){
int res = 0;
for(int i = 0; i < cnt; i++){
for(int j = head[node[i]]; j != -1; j = edge[j].next){
int to = edge[j].to;
if(vis[to]){
res++;
}
}
}
res /= 2;
if(res > cnt){
ans2 += res;
}
}
void dfs(int u, int pre){
dfn[u] = low[u] = ++tim; // 记录时间戳
s[++top] = u; // 经过的点加入栈
for(int i = head[u]; i != -1; i = edge[i].next){
int to = edge[i].to;
if(to == pre){
continue;
}
if(!dfn[to]){
dfs(to, u);
low[u] = min(low[u], low[to]); // 更新u所能访问到的最早时间戳
if(low[to] > dfn[u]){ // 桥的判断
ans1++;
}
if(low[to] >= dfn[u]){
cnt = 0;
memset(vis, 0, sizeof(vis));
int x; //把该双连通分量中的点从栈中取出
do{
x = s[top--];
vis[x] = 1;
node[cnt++] = x;
} while(x != to);
node[cnt++] = u; //注意不能把割点也拿出来
vis[u] = 1;
cal(); // 计算该连通分量中边的个数
}
} else{
low[u] = min(low[u], dfn[to]);
}
}
}
int main()
{
while(scanf("%d%d", &n, &m)){
if(n == 0 && m == 0){
break;
}
init();
for(int i = 1; i <= m; i++){
int a, b;
scanf("%d%d", &a, &b);
a++, b++;
add(a, b);
add(b, a);
}
for(int i = 1; i <= n; i++){
if(!dfn[i]){
dfs(i, -1);
}
}
printf("%d %d\n", ans1, ans2);
}
return 0;
}