-
Notifications
You must be signed in to change notification settings - Fork 1
/
Articulation Bridge.cpp
81 lines (80 loc) · 1.7 KB
/
Articulation Bridge.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Tested on LightOJ 1026
#include<bits/stdc++.h>
using namespace std;
#define MX 10005
vector<int>vec[MX];
bool vist[MX];
int low[MX],dist[MX],ans,par[MX],cur;
vector<pair<int,int> >str;
void ArticulationBridge(int u)
{
low[u] = dist[u] = ++cur;
vist[u] = 1;
for(int i = 0; i < vec[u].size(); i++)
{
int v = vec[u][i];
if(vist[v])
{
if(v != par[u])
{
low[u] = min(low[u], dist[v]);
}
}
else // not visited
{
par[v] = u;
ArticulationBridge(v);
low[u] = min(low[u], low[v]);
if(low[v] > dist[u])
{
ans += 1;
str.push_back(make_pair(min(u,v),max(u,v)));
}
}
}
}
void init()
{
ans=cur=0;
str.clear();
for(int i=0;i<MX;i++)
{
vec[i].clear();
low[i]=dist[i]=vist[i]=0;
par[i]=i;
}
}
int main()
{
int t,n,x,y,v;
scanf("%d",&t);
for(int cs=1; cs<=t; cs++)
{
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%d (%d)",&x,&y);
for(int j=1; j<=y; j++)
{
scanf("%d",&v);
vec[x].push_back(v);
}
}
for(int i=0; i<n; i++)
{
if(vist[i]==0)
ArticulationBridge(i);
}
printf("Case %d:\n",cs);
if(ans==0)
printf("0 critical links\n");
else
{
sort(str.begin(),str.end());
printf("%d critical links\n",ans);
for(int i=0;i<ans;i++)
printf("%d - %d\n",str[i].first,str[i].second);
}
init();
}
}