forked from cacophonix/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBIA.cpp
91 lines (84 loc) · 1.79 KB
/
BIA.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
/*
USER: zobayer
TASK: BIA
ALGO: lenguar tarjan
*/
#include <cstdio>
#include <set>
#include <vector>
using namespace std;
#define MAX 5005
#define pb(x) push_back(x)
vector<int> adj[MAX], pred[MAX], bucket[MAX];
bool vi[MAX];
int n, cnt, num[MAX], par[MAX], ancestor[MAX], best[MAX], semi[MAX], idom[MAX], rnum[MAX];
void dfs(int x,int p) {
vi[x] = 1;
num[x] = cnt++;
rnum[cnt - 1] = x;
par[num[x]] = num[p];
int sz = adj[x].size(), i, y;
for(i = 0; i < sz; i++){
y = adj[x][i];
if(!vi[y]) dfs(y, x);
}
}
void compress(int x) {
int a = ancestor[x];
if(ancestor[a] == 0) return;
compress(a);
if(semi[best[x]] > semi[best[a]]) best[x] = best[a];
ancestor[x] = ancestor[a];
}
int eval(int x) {
if(ancestor[x] == 0) return x;
compress(x);
return best[x];
}
int main() {
int i, x, y, m, j, p;
while(scanf("%d %d", &n, &m)==2) {
for(i = 1; i <= n; i++) {
adj[i].clear(), pred[i].clear(), bucket[i].clear();
ancestor[i] = idom[i] = vi[i] = 0;
semi[i] = best[i] = i;
}
for(i = 0; i < m; i++) {
scanf("%d %d", &x, &y);
adj[x].pb(y);
pred[y].pb(x);
}
cnt = 1, num[0] = 0;
dfs(1, 0);
set<int> s;
set<int>::iterator it;
for(i = n; i > 1; i--) {
p = par[i];
int sz = pred[rnum[i]].size();
for(j = 0; j < sz; j++){
x = num[pred[rnum[i]][j]];
y = eval(x);
if(semi[i] > semi[y]) semi[i] = semi[y];
}
bucket[semi[i]].pb(i);
ancestor[i] = p;
sz = bucket[p].size();
for(j = 0; j < sz; j++) {
x = bucket[p][j];
y = eval(x);
if(semi[y] < p) idom[x] = y;
else idom[x] = p;
}
}
s.insert(1);
for(i = 2;i <= n;i++) {
if(idom[i] != semi[i]) idom[i] = idom[idom[i]];
s.insert(rnum[idom[i]]);
}
it = s.begin();
printf("%d\n%d", s.size(), *it);
for(it++; it != s.end(); it++) printf(" %d", *it);
puts("");
}
return 0;
}