-
Notifications
You must be signed in to change notification settings - Fork 0
/
1026 - Critical Links.cpp
104 lines (91 loc) · 2.37 KB
/
1026 - Critical Links.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
103
104
/**
@author mhhrakib
*/
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <algorithm>
#define MS(array, value) memset(array, value, sizeof array)
#define MX 10001
#define pi pair<int,int>
using namespace std;
bool compare(pi pi1,pi pi2){
if(pi1.first == pi2.first)
return pi1.second<pi2.second;
return pi1.first<pi2.first;
}
struct graph
{
vector <int> adj[MX];
vector <pi> bridge;
bool visited[MX];
int dis_time[MX], parent[MX], low[MX], time,cnt;
void init()
{
MS(visited,false);
MS(parent,-1);
MS(dis_time,0);
MS(low,0);
time = cnt = 0;
}
void Find_Bridge(int source)
{
int u = source;
visited[u] = true;
dis_time[u] = low[u] = ++time;
for(int i = 0; i<adj[u].size(); i++)
{
int node = adj[u][i];
if(parent[u] == node) continue;
else if(visited[node] == true){
low[u] = min(low[u], dis_time[node]);
}
else if(visited[node] == false){
parent[node] = u;
Find_Bridge(node);
low[u] = min(low[node],low[u]);
if(dis_time[u] < low[node]){
if(u>node)
bridge.push_back(make_pair(node,u));
else bridge.push_back(make_pair(u,node));
cnt++;
}
}
}
time++;
return;
}
void Sort()
{
sort(bridge.begin(),bridge.end(),compare);
}
};
int main(int argc, char **argv)
{
int t;
scanf("%d",&t);
for(int tc = 1; tc<=t; tc++){
graph g;
g.init();
int nodes, edges, x, y;
scanf("%d",&nodes);
y = nodes;
while(nodes--){
int u,k;
scanf("%d (%d)",&u,&k);
while(k--){
scanf("%d",&x);
g.adj[u].push_back(x);
}
}
for(int j = 0; j<y; j++)
if(g.visited[j] == false)
g.Find_Bridge(j);
g.Sort();
printf("Case %d:\n%d critical links\n",tc,g.cnt);
for(int i = 0; i<g.cnt; i++)
cout<<g.bridge[i].first<<" - "<<g.bridge[i].second<<endl;
}
}