-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy path1008_Airline Routes (35).cpp
54 lines (51 loc) · 1.18 KB
/
1008_Airline Routes (35).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
#include <bits/stdc++.h>
using namespace std;
/*
强连通分量,Tarjan算法
*/
const int N = 10000;
vector<int> edges[N*6+5];
stack<int> s;
int dfn[N+5], low[N+5], scc[N+5];
bool inStack[N+5];
int dfnIndex = 0, sccIndex = 0;
void tarjan(int u) {
dfn[u] = low[u] = ++dfnIndex;
inStack[u] = true;
s.push(u);
for (auto &v : edges[u]) {
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[v], low[u]);
} else if (inStack[v] && dfn[v] < low[u]) {
low[u] = dfn[v];
}
}
if (low[u] == dfn[u]) {
sccIndex++;
int tmp = 0;
while (tmp != u) {
tmp = s.top(); s.pop();
scc[tmp] = sccIndex;
inStack[tmp] = false;
}
}
}
int main() {
int n, m, k, u, v;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
edges[u].push_back(v);
}
memset(inStack, false, sizeof(inStack));
for (int i = 1; i <= n; i++) {
if (!dfn[i]) tarjan(i);
}
scanf("%d", &k);
for (int i = 0; i < k; i++) {
scanf("%d%d", &u, &v);
printf("%s\n", scc[u] == scc[v] ? "Yes" : "No");
}
return 0;
}