LightOJ 1026 Critical Links 求桥

来源:互联网 发布:flash网站源码 编辑:程序博客网 时间:2024/06/13 05:45


Description

In a computer network a link L, which interconnects two servers, is considered critical if there are at least two servers A and B such that all network interconnection paths between A and B pass through L. Removing a critical link generates two disjoint sub-networks such that any two servers of a sub-network are interconnected. For example, the network shown in figure 1 has three critical links that are marked red: 0 - 13 - 4 and 6 - 7 in figure2.

Figure 1: Original Graph

Figure 2: The Critical Links

It is known that:

1.      The connection links are bi-directional.

2.      A server is not directly connected to itself.

3.      Two servers are interconnected if they are directly connected or if they are interconnected with the same server.

4.      The network can have stand-alone sub-networks.

Write a program that finds all critical links of a given computer network.

Input

Input starts with an integer T (≤ 15), denoting the number of test cases.

Each case starts with a blank line. The next line will contain n (0 ≤ n ≤ 10000) denoting the number of nodes. Each of the next n lines will contain some integers in the following format:

u (k) v1 v2 ... vk

Where u is the node identifier, k is the number of adjacent nodes; v1, v2 ... vk are the adjacent nodes of u. You can assume that there are at most100000 edges in total in a case. Dataset is huge, so use faster i/o methods.

Output

For each case, print the case number first. Then you should print the number of critical links and the critical links, one link per line, starting from the beginning of the line, as shown in the sample output below. The links are listed in ascending order according to their first element and then second element. Since the graph is bidirectional, print a link u v if u < v.

Sample Input

3

 

8

0 (1) 1

1 (3) 2 0 3

2 (2) 1 3

3 (3) 1 2 4

4 (1) 3

7 (1) 6

6 (1) 7

5 (0)

 

0

 

2

0 (1) 1

1 (1) 0

Sample Output

Case 1:

3 critical links

0 - 1

3 - 4

6 - 7

Case 2:

0 critical links

Case 3:

1 critical links

0 - 1

Hint

Dataset is huge, use faster I/O methods.



题目大意:
给出一个无向图(不一定连通),求割边。
解题思路:
求割边的方法跟求强连通分量类似都是通过DFS来实现的。
记录dfn[u],low[u]为u第一次访问的时间、可达到的最早的结点。
dfs的过程中,对于边u->v,如果low[v]>dfn[u]那么u->v是割边,因为v到达的最早的结点也在u点之后,
所以去掉u->v这条边后,u和它之前的点就不可达v了。


#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>#include <vector>#include <cmath>#include <stack>#include <string>#include <sstream>#include <map>#include <set>#define pi acos(-1.0)#define LL long long#define ULL unsigned long long#define inf 0x3f3f3f3f#define INF 1e18#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1#define debug(a) printf("---%d---\n", a)#define mem0(a) memset(a, 0, sizeof(a))#define memi(a) memset(a, inf, sizeof(a))#define mem1(a) memset(a, -1, sizeof(a))#define input1(a) scanf("%d", &a)#define input2(a,b) scanf("%d %d", &a, &b)#define input3(a,b,c) scanf("%d %d %d", &a, &b, &c)using namespace std;typedef pair<int, int> P;const double eps = 1e-10;const int maxn = 3e5 + 5;const int N = 1e4 + 5;const int mod = 1e8;int head[N], nxt[maxn], pnt[maxn], eban[maxn], txt;int n;int dfn[N], low[N], instack[N];int dex, cnt;stack<int>sta;pair<int, int>ans[maxn];void add_edge(int u, int v){pnt[txt] = v;nxt[txt] = head[u];head[u] = txt++;}void Tarjan(int u){sta.push(u);low[u] = dfn[u] = ++dex;instack[u] = 1; for (int i = head[u]; ~i; i = nxt[i]){if (eban[i]) continue;eban[i] = eban[i^1] = 1;int v = pnt[i]; if (!dfn[v]){Tarjan(v);low[u] = min(low[u], low[v]);//if (low[v] > dfn[u])// 桥 //ans[++cnt] = make_pair(min(u,v), max(u,v));  }else if (instack[v]) low[u] = min(low[u], dfn[v]);if (low[v] > dfn[u])// 桥 ans[++cnt] = make_pair(min(u,v), max(u,v));  }if (dfn[u] == low[u]){ while (1){int v = sta.top(); sta.pop();instack[v] = 0;if (v == u) break;}}}int main(void){//freopen("in.txt","r", stdin); int T, u, v, t, cas = 0;input1(T);while (T--){input1(n);memset(head, -1, sizeof(head));txt = 0;for (int i = 1; i <= n; i++){scanf("%d (%d)", &u, &t); while (t--){input1(v);if (v > u){add_edge(u, v); add_edge(v, u);}} }mem0(dfn);mem0(low);mem0(instack);mem0(eban);while (!sta.empty()) sta.pop();dex = cnt = 0;for (int i = 0; i < n; i++)if (!dfn[i]) Tarjan(i);printf("Case %d:\n", ++cas);printf("%d critical links\n", cnt);sort(ans+1, ans+1+cnt);for (int i = 1; i <= cnt; i++)cout << ans[i].first << " - " << ans[i].second << endl;}return 0;}// 另一种写法 //void Tarjan(int u, int fa)//{//sta.push(u);//low[u] = dfn[u] = ++dex;//for (int i = head[u]; ~i; i = nxt[i]){//int v = pnt[i];//if (!dfn[v]){//Tarjan(v, u);//low[u] = min(low[u], low[v]);//if (low[v] > dfn[u]){ // 桥 //ans[++cnt] = make_pair(min(u,v), max(u,v)); //}//}//else if (dfn[v] < dfn[u] && v != fa)//low[u] = min(low[u], dfn[v]);//}//}//int main(void)//{////freopen("in.txt","r", stdin);//int T, u, v, t, cas = 0;//input1(T);//while (T--){//input1(n);//memset(head, -1, sizeof(head));//txt = 0;//for (int i = 1; i <= n; i++){//scanf("%d (%d)", &u, &t); //while (t--){//input1(v);//add_edge(u, v); //} //}//mem0(dfn);//mem0(low); //while (!sta.empty()) sta.pop();//dex = cnt = 0;//for (int i = 0; i < n; i++)//if (!dfn[i]) Tarjan(i, i);//printf("Case %d:\n", ++cas);//printf("%d critical links\n", cnt);//sort(ans+1, ans+1+cnt);//for (int i = 1; i <= cnt; i++)//cout << ans[i].first << " - " << ans[i].second << endl;//}////return 0;//}



0 0
原创粉丝点击