-
Notifications
You must be signed in to change notification settings - Fork 34
/
1026-Critical-Links.cpp
121 lines (85 loc) · 1.65 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
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <map>
#define SIZE 10006
using namespace std;
vector < vector <int> > g;
map < pair <int, int>, int > mp;
int vis[SIZE];
int dur[SIZE];
int low[SIZE];
int art[SIZE];
int counter;
int parent[SIZE];
int find_articulation(int x)
{
int child;
int y;
counter++;
vis[x] = 1;
child = 0;
dur[x] = low[x] = counter;
for (int i = 0; i < g[x].size(); i++) {
y = g[x][i];
child++;
if(vis[y] == 0) {
parent[y] = x;
find_articulation(y);
low[x] = min(low[x], low[y]);
if(low[y] > dur[x]) {
if(mp.count(make_pair(min(x, y), max(x, y))) == 0) {
mp[make_pair(min(x, y), max(x, y))] = 1;
}
}
}
else {
if(y != parent[x]) {
low[x] = min(low[x], dur[y]);
}
}
}
}
int main()
{
int t;
int n;
int m;
int x;
int y;
char c;
scanf("%d", &t);
for (int cs = 1; cs <= t; cs++) {
scanf("%d", &n);
vector < vector <int> > tg(n + 2);
map < pair <int, int>, int > tmp;
swap(mp, tmp);
swap(tg, g);
memset(parent, -1, sizeof parent);
memset(vis, 0, sizeof vis);
memset(art, 0, sizeof art);
counter = 0;
for (int i = 0; i < n; i++) {
scanf("%d (%d)", &x, &m);
for (int j = 0; j < m; j++) {
scanf("%d", &y);
if(x < y) {
g[x].push_back(y);
g[y].push_back(x);
}
}
}
for (int i = 0; i <= n; i++) {
if(!vis[i]) {
find_articulation(i);
}
}
map< pair <int, int>, int >::iterator it;
printf("Case %d:\n", cs);
printf("%d critical links\n", mp.size());
for(it = mp.begin(); it != mp.end(); it++) {
printf("%d - %d\n", it->first.first, it->first.second);
}
}
}