HDU 3394 Railway

Railway

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 3394
64-bit integer IO format: %I64d      Java class name: Main
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

 
解题:点双联通。。
 
桥肯定不属于任何的route...没必要建,如果路属于多个tourist route,那便是多余的,很容易想到,如果一个点双连通分量中,边数大于点数,则这个点双连通分量中的所有边都是多余的。。
 
 1 #include <bits/stdc++.h>
 2 #define pii pair<int,int>
 3 using namespace std;
 4 const int maxn = 10010;
 5 vector<int>g[maxn];
 6 stack< pii >stk;
 7 int n,m,nTime,dfn[maxn],low[maxn],ret1,ret2;
 8 bool vis[maxn];
 9 void tarjan(int u,int fa) {
10     dfn[u] = low[u] = ++nTime;
11     for(int i = 0; i < g[u].size(); ++i) {
12         if(dfn[g[u][i]] == -1) {
13             stk.push(make_pair(u,g[u][i]));
14             tarjan(g[u][i],u);
15             low[u] = min(low[u],low[g[u][i]]);
16             if(dfn[u] <= low[g[u][i]]) {
17                 memset(vis,false,sizeof vis);
18                 int a = 0,b = 0;
19                 while(!stk.empty()) {
20                     pii now = stk.top();
21                     stk.pop();
22                     a++;
23                     if(!vis[now.first]) b += vis[now.first] = true;
24                     if(!vis[now.second]) b += vis[now.second] = true;
25                     if(dfn[now.first] < dfn[g[u][i]]) break;
26                 }
27                 if(dfn[u] < low[g[u][i]]) ret1++;
28                 if(a > b) ret2 += a;
29             }
30         } else if(g[u][i] != fa) {
31             low[u] = min(low[u],dfn[g[u][i]]);
32             if(dfn[u] > dfn[g[u][i]]) stk.push(make_pair(u,g[u][i]));
33         }
34     }
35 }
36 int main() {
37     int u,v;
38     while(scanf("%d %d",&n,&m),n||m) {
39         while(!stk.empty()) stk.pop();
40         for(int i = nTime = 0; i < maxn; ++i) {
41             g[i].clear();
42             dfn[i] = -1;
43         }
44         for(int i = 0; i < m; ++i) {
45             scanf("%d %d",&u,&v);
46             g[u].push_back(v);
47             g[v].push_back(u);
48         }
49         ret1 = ret2 = 0;
50         for(int i = 0; i < n; ++i)
51             if(dfn[i] == -1) tarjan(i,-1);
52         printf("%d %d
",ret1,ret2);
53     }
54     return 0;
55 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/4444354.html