-
Notifications
You must be signed in to change notification settings - Fork 1
/
LightOJ_1026.cpp
102 lines (87 loc) · 2.02 KB
/
LightOJ_1026.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <bits/stdc++.h>
#define mem(x,y) memset(x,y,sizeof(x));
using namespace std;
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
int V, E;
vector<int> adj_l[10005];
int par[10005], low[10005], disc[10005], _time;
bool vis[10005], AP[10005];
vector< pair<int, int> > bridge;
void input(int u, int v)
{
adj_l[u].push_back(v);
}
void init()
{
for(int i = 0; i < 10005; i++)
adj_l[i].clear();
bridge.clear();
mem(par, -1);
mem(low, 0);
mem(vis, false);
mem(AP, false);
mem(disc, 0);
_time = 0;
}
void dfs(int src)
{
vis[src] = true;
low[src] = disc[src] = (++_time);
for(int i = 0; i < adj_l[src].size(); i++)
{
int v = adj_l[src][i];
if(vis[v] == false)
{
par[v] = src;
dfs(v);
low[src] = min(low[src], low[v]);
if(low[v] > disc[src])
{
AP[src] = true;
bridge.push_back(make_pair(min(src, v), max(src, v)));
}
}
else if(v != par[src])
{
low[src] = min(low[src], disc[v]);
}
}
}
bool custom_sort(const pair<int,int> &a, const pair<int,int> &b)
{
if(a.first == b.first)
return (a.second < b.second);
return (a.first < b.first);
}
void Bridge(){
for(int i = 0; i < V; i++){
if(vis[i] == false)
dfs(i);
}
printf("%d critical links\n", bridge.size());
sort(bridge.begin(), bridge.end(), custom_sort);
for(int i = 0; i < bridge.size(); i++){
cout<<bridge[i].first<<" - "<<bridge[i].second<<endl;
}
init();
}
int main()
{
//READ("LightOJ_1026.txt");
int t, u, v, n, case_no = 0;
cin>>t;
for(int k = 0; k < t; k++){
cin>>V;
for(int i = 0; i < V; i++){
scanf("%d (%d)", &u, &n);
for(int j = 0; j < n; j++){
cin>>v;
input(u, v);
}
}
printf("Case %d:\n", ++case_no);
Bridge();
}
return 0;
}