-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path2143.cpp
68 lines (64 loc) · 1.39 KB
/
2143.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
#pragma GCC target("popcnt")
#include <bits/stdc++.h>
using namespace std;
const int N = 5e4 + 5;
int n, nn, m, q, Time, num[N], low[N], cmp[N];
vector<int> s, adj[N], nadj[N];
bool vis[N];
bitset<N> f[N];
int tarjan(int u) {
num[u] = low[u] = ++Time;
s.emplace_back(u);
for (int v: adj[u]) {
if (!cmp[v]) {
low[u] = min(low[u], num[v] ?: tarjan(v));
}
}
if (low[u] == num[u]) {
int v; ++nn;
do {
v = s.back(); s.pop_back();
cmp[v] = nn;
} while (v != u);
}
return low[u];
}
void dfs(int u) {
vis[u] = true;
f[u].set(u);
for (int v: nadj[u]) {
if (!vis[v]) dfs(v);
f[u] |= f[v];
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m >> q;
for (int i = 1, u, v; i <= m; ++i) {
cin >> u >> v;
adj[u].emplace_back(v);
}
for (int i = 1; i <= n; ++i) {
if (!num[i]) tarjan(i);
}
for (int u = 1; u <= n; ++u) {
for (int v: adj[u]) {
if (cmp[u] != cmp[v]) {
nadj[cmp[u]].emplace_back(cmp[v]);
}
}
}
for (int i = 1; i <= nn; ++i) {
sort(nadj[i].begin(), nadj[i].end());
nadj[i].resize(unique(nadj[i].begin(), nadj[i].end()) - nadj[i].begin());
}
for (int i = 1; i <= nn; ++i) {
if (!vis[i]) dfs(i);
}
while (q--) {
int u, v; cin >> u >> v;
u = cmp[u]; v = cmp[v];
cout << (u == v || f[u].test(v) ? "YES\n" : "NO\n");
}
return 0;
}