-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathReachability Queries.cpp
98 lines (85 loc) · 1.95 KB
/
Reachability Queries.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
#include <bits/stdc++.h>
using namespace std;
const int maxN = 5e4+5;
bool vis[maxN];
int N, M, Q, ds[maxN];
vector<int> order, component;
vector<int> G[maxN], H[maxN], SCC[maxN];
bitset<maxN> reachable[maxN];
int find(int u){
if(ds[u] < 0) return u;
ds[u] = find(ds[u]);
return ds[u];
}
bool merge(int u, int v){
u = find(u); v = find(v);
if(u == v) return false;
if(ds[u] < ds[v]){
ds[u] += ds[v];
ds[v] = u;
} else {
ds[v] += ds[u];
ds[u] = v;
}
return true;
}
void dfs1(int u){
vis[u] = true;
for(int v : G[u])
if(!vis[v])
dfs1(v);
order.push_back(u);
}
void dfs2(int u){
vis[u] = true;
component.push_back(u);
for(int v : H[u])
if(!vis[v])
dfs2(v);
}
void dfs3(int u){
vis[u] = true;
for(int v : SCC[u]){
if(!vis[v])
dfs3(v);
reachable[u] |= reachable[v];
}
}
int main(){
scanf("%d %d %d", &N, &M, &Q);
for(int i = 0, a, b; i < M; i++){
scanf("%d %d", &a, &b);
G[a].push_back(b);
H[b].push_back(a);
}
fill(vis+1, vis+N+1, false);
for(int i = 1; i <= N; i++)
if(!vis[i])
dfs1(i);
fill(ds+1, ds+N+1, -1);
fill(vis+1, vis+N+1, false);
for(int i = 1; i <= N; i++){
int u = order[N-i];
if(!vis[u]){
dfs2(u);
for(int j = 0; j+1 < (int) component.size(); j++)
merge(component[j], component[j+1]);
component.clear();
}
}
for(int i = 1; i <= N; i++){
int rep = find(i);
reachable[rep].set(i);
for(int v : G[i])
SCC[rep].push_back(find(v));
}
fill(vis+1, vis+N+1, false);
for(int i = 1; i <= N; i++)
if(!vis[i])
dfs3(i);
for(int i = 0, a, b; i < Q; i++){
scanf("%d %d", &a, &b);
a = find(a); b = find(b);
printf(reachable[a][b] ? "YES\n" : "NO\n");
}
}