-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcses_2143.cpp
More file actions
103 lines (94 loc) · 1.91 KB
/
cses_2143.cpp
File metadata and controls
103 lines (94 loc) · 1.91 KB
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
#include <bits/extc++.h>
using namespace std;
const int MAXN = 5e4 + 87;
int c, t; // 新圖的點數
bitset<MAXN> dp[MAXN];
vector<int> G[MAXN], nG[MAXN];
int low[MAXN], deep[MAXN], table[MAXN];
bool visited[MAXN], inst[MAXN];
stack<int> st;
void tarjan(int u)
{
visited[u] = true;
low[u] = deep[u] = ++t;
st.push(u);
inst[u] = true;
for (auto v : G[u])
{
if (!visited[v])
{
tarjan(v);
}
if (inst[v])
{
low[u] = min(low[u], low[v]);
}
}
if (deep[u] == low[u])
{
c++;
int v;
do
{
v = st.top();
st.pop();
inst[v] = false;
table[v] = c;
} while (v != u);
}
return;
}
void dfs(int u)
{
visited[u] = true;
dp[u].set(u);
for (auto v : nG[u])
{
if (!visited[v])
dfs(v);
dp[u] |= dp[v];
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
while (m--)
{
int u, v;
cin >> u >> v;
G[u - 1].emplace_back(v - 1);
}
// 縮點
for (int i = 0; i < n; i++)
if (!visited[i])
tarjan(i);
// 建新的圖
for (int u = 0; u < n; u++)
for (auto v : G[u])
if (table[u] != table[v])
nG[table[u]].emplace_back(table[v]);
for (int i = 1; i <= c; i++)
{
sort(nG[i].begin(), nG[i].end());
nG[i].resize(unique(nG[i].begin(), nG[i].end()) - nG[i].begin());
}
for (int i = 1; i <= c; i++)
visited[i] = false;
for (int i = 1; i <= c; i++)
if (!visited[i])
dfs(i);
while (q--)
{
int u, v;
cin >> u >> v;
u = table[u - 1], v = table[v - 1];
if (u == v || dp[u].test(v))
cout << "YES\n";
else
cout << "NO\n";
}
return 0;
}