forked from Jonathan-Uy/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCritical Cities.cpp
79 lines (66 loc) · 1.84 KB
/
Critical Cities.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
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5+1;
int N, M, p[maxN], sdom[maxN], idom[maxN], label[maxN];
int timer, tin[maxN], inv[maxN], dsu[maxN];
vector<int> ans, G[maxN], GR[maxN], DT[maxN], bucket[maxN];
void dfs(int u = 1){
tin[u] = ++timer;
inv[timer] = u;
label[timer] = sdom[timer] = dsu[timer] = timer;
for(int v : G[u]){
if(!tin[v]){
dfs(v);
p[tin[v]] = tin[u];
}
GR[tin[v]].push_back(tin[u]);
}
}
int find(int u, bool x = false){
if(u == dsu[u]) return x ? -1 : u;
int v = find(dsu[u], true);
if(v < 0) return u;
if(sdom[label[dsu[u]]] < sdom[label[u]])
label[u] = label[dsu[u]];
dsu[u] = v;
return x ? v : label[u];
}
void build_dominator_tree(){
dfs();
for(int u = N; u > 0; u--){
for(int v : GR[u])
sdom[u] = min(sdom[u], sdom[find(v)]);
if(u > 1) bucket[sdom[u]].push_back(u);
for(int v : bucket[u])
idom[v] = (sdom[find(v)] == sdom[v] ? sdom[v] : find(v));
if(u > 1) dsu[u] = p[u];
}
for(int u = 2; u <= N; u++){
if(idom[u] != sdom[u])
idom[u] = idom[idom[u]];
DT[inv[u]].push_back(inv[idom[u]]);
DT[inv[idom[u]]].push_back(inv[u]);
}
}
bool dfs_dominator_tree(int u = 1, int p = -1){
bool good = (u == N);
for(int v : DT[u])
if(v != p)
good |= dfs_dominator_tree(v, u);
if(good) ans.push_back(u);
return good;
}
int main(){
scanf("%d %d", &N, &M);
for(int i = 0, a, b; i < M; i++){
scanf("%d %d", &a, &b);
G[a].push_back(b);
}
build_dominator_tree();
dfs_dominator_tree();
sort(ans.begin(), ans.end());
int K = (int) ans.size();
printf("%d\n", K);
for(int i = 0; i < K; i++)
printf("%d%c", ans[i], (" \n")[i==K-1]);
}