-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathReachability Queries.cpp
119 lines (108 loc) · 2.43 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include<bits/stdc++.h>
using namespace std;
#define INF 2047483647
#define INFL 9223372036854775807
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define ull unsigned long long
#define M 1000000007
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
#define take(x) scanf("%d",&x)
#define DE(x) printf("\ndebug %d\n",x);
#define vout(x) for(int i=0;i<x.size();i++) printf("%d ",x[i]);
#define pie acos(-1)
#define MOD 998244353
const int N = 5e4+5;
bitset<N> bset[N];
int parent[N];
vector<int> adj_rev[N], adj[N];
vector<bool> used;
vector<int> order, component;
void dfs1(int v) {
used[v] = true;
for (auto u : adj_rev[v])
if (!used[u])
dfs1(u);
order.push_back(v);
}
void dfs2(int v,int p) {
parent[v] = p;
used[v] = true;
for (auto u : adj[v])
if (!used[u])
dfs2(u,p);
}
void dfs3(int u) {
used[u] = true;
for (auto v : adj[u])
if (!used[v])
dfs3(v);
order.push_back(u);
}
void dfs4(int u){
used[u] = true;
bset[u].set(u);
for(auto v:adj[u]){
if( !used[v] )
dfs4(v);
bset[u] |=bset[v];
}
}
int main() {
int n,m,q;
cin>>n>>m>>q;
vector<pii> edges;
for (int i=0;i<m;i++) {
int a, b;
cin>>a>>b;
a--;b--;
edges.push_back({a,b});
adj[a].push_back(b);
adj_rev[b].push_back(a);
}
used.assign(n, false);
for (int i = 0; i < n; i++)
if (!used[i])
dfs1(i);
used.assign(n, false);
reverse(order.begin(), order.end());
for (auto v : order)
if (!used[v]) {
dfs2 (v,v);
}
map<pii,int> taken;
for(int i=0;i<n;i++) adj[i].clear();
for(auto [u,v]:edges){
if( parent[u] == parent[v] ) continue;
u = parent[u];
v = parent[v];
if( taken[{u,v}] ) continue;
taken[{u,v}] = 1;
adj[u].push_back(v);
}
used.assign(n, false);
order.clear();
for(int i=0;i<n;i++){
int u = parent[i];
if( !used[u] )
dfs3(u);
}
used.assign(n, false);
for(auto u:order){
if( !used[u] )
dfs4(u);
}
for(int i=0;i<q;i++){
int u,v;
cin>>u>>v;
u--;v--;
u = parent[u];
v = parent[v];
if( bset[u][v] ) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}