Financial Crisis
Time Limit: 2000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 374964-bit integer IO format: %I64d Java class name: Main
Because of the financial crisis, a large number of enterprises go bankrupt. In addition to this, other enterprises, which have trade relation with the bankrup enterprises, are also faced with closing down. Owing to the market collapse, profit decline and funding chain intense, the debt-ridden entrepreneurs
have to turn to the enterprise with stably developing for help.
Nowadays, there exist a complex net of financial trade relationship between enterprises. So if one of enterprises on the same financial chain is faced with bankrupt, leading to cashflow's obstruction, the other enterprises related with it will be influenced as well. At the moment, the foresight entrepreneurs are expected the safer cooperation between enterprises. In this sense, they want to know how many financial chains between some pairs of enterprises are independent with each other. The indepence is defined that if there exist two roads which are made up of enterprises(Vertexs) and their financial trade relations(Edge) has the common start point S and end point T, and expect S and T, none of other enterprise in two chains is included in these two roads at the same time. So that if one of enterpirse bankrupt in one of road, the other will not be obstructed.
Now there are N enterprises, and have M pair of financial trade relations between them, the relations are Mutual. They need to ask about Q pairs of enterprises' safety situations. When two enterprises have two or more independent financial chains, we say they are safe enough, you needn't provide exact answers.
have to turn to the enterprise with stably developing for help.
Nowadays, there exist a complex net of financial trade relationship between enterprises. So if one of enterprises on the same financial chain is faced with bankrupt, leading to cashflow's obstruction, the other enterprises related with it will be influenced as well. At the moment, the foresight entrepreneurs are expected the safer cooperation between enterprises. In this sense, they want to know how many financial chains between some pairs of enterprises are independent with each other. The indepence is defined that if there exist two roads which are made up of enterprises(Vertexs) and their financial trade relations(Edge) has the common start point S and end point T, and expect S and T, none of other enterprise in two chains is included in these two roads at the same time. So that if one of enterpirse bankrupt in one of road, the other will not be obstructed.
Now there are N enterprises, and have M pair of financial trade relations between them, the relations are Mutual. They need to ask about Q pairs of enterprises' safety situations. When two enterprises have two or more independent financial chains, we say they are safe enough, you needn't provide exact answers.
Input
The Input consists of multiple test cases. The first line of each test case contains three integers, N ( 3 <= N <= 5000 ), M ( 0 <= M <= 10000 ) and Q ( 1 <= Q <= 1000 ). which are the number of enterprises, the number of the financial trade relations and the number of queries.
The next M lines, each line contains two integers, u, v ( 0 <= u, v < N && u != v ), which means enterpirse u and enterprise v have trade relations, you can assume that the input will not has parallel edge.
The next Q lines, each line contains two integers, u, v ( 0 <= u, v < N && u != v ), which means entrepreneurs will ask you the financial safety of enterpirse u and enterprise v.
The last test case is followed by three zeros on a single line, which means the end of the input.
The next M lines, each line contains two integers, u, v ( 0 <= u, v < N && u != v ), which means enterpirse u and enterprise v have trade relations, you can assume that the input will not has parallel edge.
The next Q lines, each line contains two integers, u, v ( 0 <= u, v < N && u != v ), which means entrepreneurs will ask you the financial safety of enterpirse u and enterprise v.
The last test case is followed by three zeros on a single line, which means the end of the input.
Output
For each case, output the test case number formated as sample output. Then for each query, output "zero" if there is no independent financial chains between those two enterprises, output "one" if there is only one such chains, or output "two or more".
Sample Input
3 1 2 0 1 0 2 1 0 4 4 2 0 1 0 2 1 2 2 3 1 2 1 3 0 0 0
Sample Output
Case 1: zero one Case 2: two or more one
Source
解题:点双连通分量
给你一个无向图,然后有一些询问,问你从起点到终点除起点与终点有交点外,没有其他交点的路径有多少条
如果u v不连通,输出0
如果u v属于同一个点双连通分量,那么输出two or more,需要注意特殊情况 只有两个点的点双连通分量,这个要输出1
其余的输出1
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 5010; 4 struct arc { 5 int u,v; 6 arc(int x = 0,int y = 0) { 7 u = x; 8 v = y; 9 } 10 }; 11 int low[maxn],dfn[maxn],bccno[maxn],clk,bcc_cnt; 12 vector<int>g[maxn],bcc[maxn],belong[maxn]; 13 stack<arc>stk; 14 void tarjan(int u,int fa) { 15 dfn[u] = low[u] = ++clk; 16 bool flag = false; 17 for(int i = g[u].size()-1; i >= 0; --i) { 18 if(!flag && g[u][i] == fa) { 19 flag = true; 20 continue; 21 } 22 if(!dfn[g[u][i]]) { 23 stk.push(arc(u,g[u][i])); 24 tarjan(g[u][i],u); 25 low[u] = min(low[u],low[g[u][i]]); 26 if(low[g[u][i]] >= dfn[u]) { 27 bcc[++bcc_cnt].clear(); 28 while(true) { 29 arc e = stk.top(); 30 stk.pop(); 31 if(bccno[e.u] != bcc_cnt) { 32 bccno[e.u] = bcc_cnt; 33 bcc[bcc_cnt].push_back(e.u); 34 belong[e.u].push_back(bcc_cnt); 35 } 36 if(bccno[e.v] != bcc_cnt) { 37 bccno[e.v] = bcc_cnt; 38 bcc[bcc_cnt].push_back(e.v); 39 belong[e.v].push_back(bcc_cnt); 40 } 41 if(e.u == u && e.v == g[u][i]) break; 42 } 43 } 44 } else if(dfn[g[u][i]] < dfn[u]) { 45 stk.push(arc(u,g[u][i])); 46 low[u] = min(low[u],dfn[g[u][i]]); 47 } 48 } 49 } 50 int uf[maxn]; 51 int Find(int x) { 52 if(x != uf[x]) uf[x] = Find(uf[x]); 53 return uf[x]; 54 } 55 void init() { 56 for(int i = clk = bcc_cnt = 0; i < maxn; ++i) { 57 bccno[i] = dfn[i] = 0; 58 g[i].clear(); 59 belong[i].clear(); 60 uf[i] = i; 61 } 62 while(!stk.empty()) stk.pop(); 63 } 64 int main() { 65 int n,m,q,u,v,cs = 1; 66 while(scanf("%d%d%d",&n,&m,&q),n||m||q) { 67 init(); 68 for(int i = 0; i < m; ++i) { 69 scanf("%d%d",&u,&v); 70 g[u].push_back(v); 71 g[v].push_back(u); 72 u = Find(u); 73 v = Find(v); 74 if(u != v) uf[v] = u; 75 } 76 for(int i = 0; i < n; ++i) 77 if(!dfn[i]) tarjan(i,-1); 78 printf("Case %d: ",cs++); 79 while(q--) { 80 scanf("%d%d",&u,&v); 81 if(Find(u) != Find(v)) puts("zero"); 82 else { 83 bool flag = false; 84 for(auto &a:belong[u]) 85 for(auto &b:belong[v]) 86 if(a == b && bcc[a].size() > 2) 87 flag = true; 88 puts(flag?"two or more":"one"); 89 } 90 } 91 } 92 return 0; 93 }