#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 120'001;
const int K = 20;
vector<int> g[MAXN], g2[MAXN];
int tin[MAXN], tout[MAXN], s[MAXN], e[MAXN], col[MAXN], hv[MAXN], up[MAXN][K], timer;
bool ok;
void dfs(int u, int p = -1) {
tin[u] = timer++;
up[u][0] = p == -1 ? 0 : p;
for (int v : g[u]) {
if (v != p) dfs(v, u);
}
tout[u] = timer++;
}
void dfs2(int u, stack<int> &top) {
col[u] = 1;
for (int v : g2[u]) {
if (col[v] == 0) dfs2(v, top);
else if (col[v] == 1) ok = false;
}
col[u] = 2;
top.push(u);
}
bool is_ancestor(int u, int v) {
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
int get_next(int u, int v) {
for (int i = K-1; i >= 0; i--) {
if (up[v][i] && !is_ancestor(up[v][i], u) && up[v][i] != u) v = up[v][i];
}
return v;
}
void solve() {
int n; cin >> n;
ok = true;
fill(s, s+n+1, 0);
fill(e, e+n+1, 0);
fill(col, col+n+1, 0);
fill(hv, hv+n+1, 0);
timer = 1;
for (int i = 1; i <= n; i++) {
g[i].clear();
g2[i].clear();
}
for (int i = 0; i < n-1; i++) {
int u, v; cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
dfs(1);
for (int u = 1; u <= n; u++) {
for (int i = 1; i < K; i++) {
up[u][i] = up[up[u][i-1]][i-1];
}
}
int m; cin >> m;
vector<pair<int, int>> edges(m+1);
for (int i = 1; i <= m; i++) {
cin >> edges[i].first >> edges[i].second;
auto [u, v] = edges[i];
s[u] = e[v] = i;
hv[edges[i].first] = i;
}
auto nxt = [&](int u, int v) {
if (u == v) return -1;
if (is_ancestor(u, v)) {
for (int i : g[u]) {
if (up[u][0] != i && is_ancestor(i, v)) {
return i;
}
}
return get_next(u, v);
} else {
return up[u][0];
}
};
for (int i = 1; i <= m; i++) {
auto [u, v] = edges[i];
while (u != -1) {
if (e[u] && e[u] != i) {
g2[i].emplace_back(e[u]);
}
if (s[u] && s[u] != i) {
g2[s[u]].emplace_back(i);
}
u = nxt(u, v);
}
}
function<void(int,int)> mv = [&](int idx, int p) {
if (!ok) return;
int v = nxt(edges[idx].first, edges[idx].second);
if (v == -1 || v == p) {
ok = false;
return;
}
if (hv[v]) {
mv(hv[v], edges[idx].first);
}
hv[edges[idx].first] = 0;
hv[v] = idx;
edges[idx].first = v;
};
stack<int> top;
for (int i = 1; i <= m; i++) {
if (col[i] == 0) dfs2(i, top);
}
while (!top.empty() && ok) {
int i = top.top(); top.pop();
while (edges[i].first != edges[i].second && ok) {
mv(i, -1);
}
}
cout << (ok?"Yes\n":"No\n");
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int t; cin >> t;
while (t--) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9816 KB |
Output is correct |
2 |
Correct |
2 ms |
9820 KB |
Output is correct |
3 |
Correct |
2 ms |
9820 KB |
Output is correct |
4 |
Correct |
11 ms |
9936 KB |
Output is correct |
5 |
Correct |
22 ms |
9944 KB |
Output is correct |
6 |
Correct |
3 ms |
9816 KB |
Output is correct |
7 |
Correct |
3 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
113 ms |
14552 KB |
Output is correct |
10 |
Correct |
745 ms |
29012 KB |
Output is correct |
11 |
Correct |
6 ms |
9820 KB |
Output is correct |
12 |
Correct |
35 ms |
10040 KB |
Output is correct |
13 |
Correct |
163 ms |
57940 KB |
Output is correct |
14 |
Correct |
116 ms |
71760 KB |
Output is correct |
15 |
Runtime error |
4219 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9816 KB |
Output is correct |
2 |
Correct |
2 ms |
9820 KB |
Output is correct |
3 |
Correct |
3 ms |
9884 KB |
Output is correct |
4 |
Correct |
2 ms |
9820 KB |
Output is correct |
5 |
Correct |
2 ms |
9880 KB |
Output is correct |
6 |
Correct |
3 ms |
9816 KB |
Output is correct |
7 |
Correct |
2 ms |
9820 KB |
Output is correct |
8 |
Correct |
3 ms |
9820 KB |
Output is correct |
9 |
Correct |
2 ms |
9820 KB |
Output is correct |
10 |
Correct |
3 ms |
10072 KB |
Output is correct |
11 |
Correct |
3 ms |
9816 KB |
Output is correct |
12 |
Correct |
2 ms |
9820 KB |
Output is correct |
13 |
Correct |
2 ms |
9904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9816 KB |
Output is correct |
2 |
Correct |
2 ms |
9820 KB |
Output is correct |
3 |
Correct |
3 ms |
9884 KB |
Output is correct |
4 |
Correct |
2 ms |
9820 KB |
Output is correct |
5 |
Correct |
2 ms |
9880 KB |
Output is correct |
6 |
Correct |
3 ms |
9816 KB |
Output is correct |
7 |
Correct |
2 ms |
9820 KB |
Output is correct |
8 |
Correct |
3 ms |
9820 KB |
Output is correct |
9 |
Correct |
2 ms |
9820 KB |
Output is correct |
10 |
Correct |
3 ms |
10072 KB |
Output is correct |
11 |
Correct |
3 ms |
9816 KB |
Output is correct |
12 |
Correct |
2 ms |
9820 KB |
Output is correct |
13 |
Correct |
2 ms |
9904 KB |
Output is correct |
14 |
Correct |
2 ms |
9820 KB |
Output is correct |
15 |
Correct |
2 ms |
9820 KB |
Output is correct |
16 |
Correct |
2 ms |
9820 KB |
Output is correct |
17 |
Correct |
3 ms |
9948 KB |
Output is correct |
18 |
Correct |
3 ms |
9816 KB |
Output is correct |
19 |
Correct |
2 ms |
9820 KB |
Output is correct |
20 |
Correct |
3 ms |
9820 KB |
Output is correct |
21 |
Correct |
3 ms |
9820 KB |
Output is correct |
22 |
Correct |
3 ms |
9820 KB |
Output is correct |
23 |
Correct |
2 ms |
9820 KB |
Output is correct |
24 |
Correct |
2 ms |
9816 KB |
Output is correct |
25 |
Correct |
3 ms |
9948 KB |
Output is correct |
26 |
Correct |
2 ms |
9820 KB |
Output is correct |
27 |
Correct |
3 ms |
9820 KB |
Output is correct |
28 |
Correct |
2 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9816 KB |
Output is correct |
2 |
Correct |
2 ms |
9820 KB |
Output is correct |
3 |
Correct |
3 ms |
9884 KB |
Output is correct |
4 |
Correct |
2 ms |
9820 KB |
Output is correct |
5 |
Correct |
2 ms |
9880 KB |
Output is correct |
6 |
Correct |
3 ms |
9816 KB |
Output is correct |
7 |
Correct |
2 ms |
9820 KB |
Output is correct |
8 |
Correct |
3 ms |
9820 KB |
Output is correct |
9 |
Correct |
2 ms |
9820 KB |
Output is correct |
10 |
Correct |
3 ms |
10072 KB |
Output is correct |
11 |
Correct |
3 ms |
9816 KB |
Output is correct |
12 |
Correct |
2 ms |
9820 KB |
Output is correct |
13 |
Correct |
2 ms |
9904 KB |
Output is correct |
14 |
Correct |
2 ms |
9820 KB |
Output is correct |
15 |
Correct |
2 ms |
9820 KB |
Output is correct |
16 |
Correct |
2 ms |
9820 KB |
Output is correct |
17 |
Correct |
3 ms |
9948 KB |
Output is correct |
18 |
Correct |
3 ms |
9816 KB |
Output is correct |
19 |
Correct |
2 ms |
9820 KB |
Output is correct |
20 |
Correct |
3 ms |
9820 KB |
Output is correct |
21 |
Correct |
3 ms |
9820 KB |
Output is correct |
22 |
Correct |
3 ms |
9820 KB |
Output is correct |
23 |
Correct |
2 ms |
9820 KB |
Output is correct |
24 |
Correct |
2 ms |
9816 KB |
Output is correct |
25 |
Correct |
3 ms |
9948 KB |
Output is correct |
26 |
Correct |
2 ms |
9820 KB |
Output is correct |
27 |
Correct |
3 ms |
9820 KB |
Output is correct |
28 |
Correct |
2 ms |
9820 KB |
Output is correct |
29 |
Correct |
4 ms |
9820 KB |
Output is correct |
30 |
Correct |
3 ms |
9820 KB |
Output is correct |
31 |
Correct |
3 ms |
9820 KB |
Output is correct |
32 |
Correct |
4 ms |
9820 KB |
Output is correct |
33 |
Correct |
2 ms |
9820 KB |
Output is correct |
34 |
Correct |
3 ms |
9820 KB |
Output is correct |
35 |
Correct |
6 ms |
9820 KB |
Output is correct |
36 |
Correct |
3 ms |
9820 KB |
Output is correct |
37 |
Correct |
2 ms |
9964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9816 KB |
Output is correct |
2 |
Correct |
2 ms |
9820 KB |
Output is correct |
3 |
Correct |
3 ms |
9884 KB |
Output is correct |
4 |
Correct |
2 ms |
9820 KB |
Output is correct |
5 |
Correct |
2 ms |
9880 KB |
Output is correct |
6 |
Correct |
3 ms |
9816 KB |
Output is correct |
7 |
Correct |
2 ms |
9820 KB |
Output is correct |
8 |
Correct |
3 ms |
9820 KB |
Output is correct |
9 |
Correct |
2 ms |
9820 KB |
Output is correct |
10 |
Correct |
3 ms |
10072 KB |
Output is correct |
11 |
Correct |
3 ms |
9816 KB |
Output is correct |
12 |
Correct |
2 ms |
9820 KB |
Output is correct |
13 |
Correct |
2 ms |
9904 KB |
Output is correct |
14 |
Correct |
2 ms |
9820 KB |
Output is correct |
15 |
Correct |
2 ms |
9820 KB |
Output is correct |
16 |
Correct |
2 ms |
9820 KB |
Output is correct |
17 |
Correct |
3 ms |
9948 KB |
Output is correct |
18 |
Correct |
3 ms |
9816 KB |
Output is correct |
19 |
Correct |
2 ms |
9820 KB |
Output is correct |
20 |
Correct |
3 ms |
9820 KB |
Output is correct |
21 |
Correct |
3 ms |
9820 KB |
Output is correct |
22 |
Correct |
3 ms |
9820 KB |
Output is correct |
23 |
Correct |
2 ms |
9820 KB |
Output is correct |
24 |
Correct |
2 ms |
9816 KB |
Output is correct |
25 |
Correct |
3 ms |
9948 KB |
Output is correct |
26 |
Correct |
2 ms |
9820 KB |
Output is correct |
27 |
Correct |
3 ms |
9820 KB |
Output is correct |
28 |
Correct |
2 ms |
9820 KB |
Output is correct |
29 |
Correct |
4 ms |
9820 KB |
Output is correct |
30 |
Correct |
3 ms |
9820 KB |
Output is correct |
31 |
Correct |
3 ms |
9820 KB |
Output is correct |
32 |
Correct |
4 ms |
9820 KB |
Output is correct |
33 |
Correct |
2 ms |
9820 KB |
Output is correct |
34 |
Correct |
3 ms |
9820 KB |
Output is correct |
35 |
Correct |
6 ms |
9820 KB |
Output is correct |
36 |
Correct |
3 ms |
9820 KB |
Output is correct |
37 |
Correct |
2 ms |
9964 KB |
Output is correct |
38 |
Correct |
107 ms |
14480 KB |
Output is correct |
39 |
Correct |
798 ms |
29012 KB |
Output is correct |
40 |
Correct |
77 ms |
13396 KB |
Output is correct |
41 |
Correct |
33 ms |
12380 KB |
Output is correct |
42 |
Correct |
57 ms |
13648 KB |
Output is correct |
43 |
Correct |
27 ms |
12376 KB |
Output is correct |
44 |
Correct |
8 ms |
10076 KB |
Output is correct |
45 |
Correct |
46 ms |
22060 KB |
Output is correct |
46 |
Correct |
43 ms |
21852 KB |
Output is correct |
47 |
Correct |
194 ms |
24920 KB |
Output is correct |
48 |
Correct |
202 ms |
24656 KB |
Output is correct |
49 |
Correct |
50 ms |
22108 KB |
Output is correct |
50 |
Correct |
70 ms |
22104 KB |
Output is correct |
51 |
Correct |
34 ms |
22876 KB |
Output is correct |
52 |
Correct |
32 ms |
22872 KB |
Output is correct |
53 |
Correct |
13 ms |
12380 KB |
Output is correct |
54 |
Correct |
50 ms |
21888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9820 KB |
Output is correct |
2 |
Correct |
2 ms |
9820 KB |
Output is correct |
3 |
Correct |
2 ms |
9820 KB |
Output is correct |
4 |
Correct |
2 ms |
9820 KB |
Output is correct |
5 |
Correct |
7 ms |
9820 KB |
Output is correct |
6 |
Correct |
2 ms |
9820 KB |
Output is correct |
7 |
Correct |
2 ms |
9820 KB |
Output is correct |
8 |
Correct |
2 ms |
9816 KB |
Output is correct |
9 |
Correct |
2 ms |
9824 KB |
Output is correct |
10 |
Correct |
2 ms |
9820 KB |
Output is correct |
11 |
Correct |
2 ms |
9820 KB |
Output is correct |
12 |
Correct |
3 ms |
9820 KB |
Output is correct |
13 |
Correct |
20 ms |
9948 KB |
Output is correct |
14 |
Correct |
30 ms |
9816 KB |
Output is correct |
15 |
Correct |
24 ms |
9820 KB |
Output is correct |
16 |
Correct |
55 ms |
22396 KB |
Output is correct |
17 |
Correct |
115 ms |
29520 KB |
Output is correct |
18 |
Correct |
211 ms |
54204 KB |
Output is correct |
19 |
Correct |
60 ms |
22516 KB |
Output is correct |
20 |
Correct |
58 ms |
22608 KB |
Output is correct |
21 |
Correct |
65 ms |
22608 KB |
Output is correct |
22 |
Correct |
95 ms |
28892 KB |
Output is correct |
23 |
Correct |
70 ms |
28980 KB |
Output is correct |
24 |
Correct |
94 ms |
28636 KB |
Output is correct |
25 |
Correct |
74 ms |
28748 KB |
Output is correct |
26 |
Correct |
92 ms |
28772 KB |
Output is correct |
27 |
Execution timed out |
5056 ms |
25284 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9816 KB |
Output is correct |
2 |
Correct |
2 ms |
9820 KB |
Output is correct |
3 |
Correct |
2 ms |
9820 KB |
Output is correct |
4 |
Correct |
11 ms |
9936 KB |
Output is correct |
5 |
Correct |
22 ms |
9944 KB |
Output is correct |
6 |
Correct |
3 ms |
9816 KB |
Output is correct |
7 |
Correct |
3 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
113 ms |
14552 KB |
Output is correct |
10 |
Correct |
745 ms |
29012 KB |
Output is correct |
11 |
Correct |
6 ms |
9820 KB |
Output is correct |
12 |
Correct |
35 ms |
10040 KB |
Output is correct |
13 |
Correct |
163 ms |
57940 KB |
Output is correct |
14 |
Correct |
116 ms |
71760 KB |
Output is correct |
15 |
Runtime error |
4219 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |