#include <iostream>
#include <vector>
using namespace std;
int n, m, sz;
bool ans;
vector <vector <int>> g(2e5);
vector <int> dep(2e5, 0);
vector <vector <int>> lift(2e5, vector <int>(20, 0));
vector <int> v1(2e5, 0);
vector <int> v2(2e5, 0);
vector <int> kez(2e5, 0);
vector <int> veg(2e5, 0);
vector <vector <int>> g2(2e5);
vector <bool> be(2e5);
vector <bool> ki(2e5);
//vector <bool> del(2e5);
void reset() {
ans = true;
for (int i = 0; i <= n; i++) {
g[i].clear();
dep[i] = 0;
fill(lift[i].begin(), lift[i].end(), 0);
g2[i].clear();
be[i] = ki[i] = false;
kez[i] = veg[i] = -1;
}
}
/*int get_c(int x) {
if (del[x]) return 0;
del[x] = true;
int mx = 0, sum = 1;
for (int i : g[x]) {
}
return sum;
}
void cd(int x) {
sz = get_c(x);
get_c(x);
}*/
void dfs(int x) {
for (int i : g[x]) {
if (i == lift[x][0]) continue;
dep[i] = dep[x] + 1;
lift[i][0] = x;
dfs(i);
}
}
int lca(int a, int b) {
if (dep[a] < dep[b]) swap(a, b);
for (int j = 19; j >= 0; j--) {
if (dep[lift[a][j]] >= dep[b]) a = lift[a][j];
}
if (a == b) return a;
for (int j = 19; j >= 0; j--) {
if (lift[a][j] == lift[b][j]) continue;
a = lift[a][j];
b = lift[b][j];
}
return lift[a][0];
}
int par(int a, int k) {
if (k < 0) return 0;
for (int j = 0; j < 20; j++) {
if (k & (1 << j)) a = lift[a][j];
}
return a;
}
bool on_path(int a, int b, int c) {
int l = lca(a, b);
if (dep[c] < dep[l]) return false;
if (par(a, dep[a] - dep[c]) == c) return true;
if (par(b, dep[b] - dep[c]) == c) return true;
return false;
}
void get_path(int i, int a, int c) {
while (true) {
if (kez[a] > -1 && kez[a] != i) g2[kez[a]].push_back(i);
if (veg[a] > -1 && veg[a] != i) g2[i].push_back(veg[a]);
if (a == c) break;
a = lift[a][0];
}
}
void tp(int x) {
if (be[x] && !ki[x]) ans = false;
if (be[x]) return;
be[x] = true;
for (int i : g2[x]) tp(i);
ki[x] = true;
}
void solve() {
cin >> n;
reset();
int a, b;
for (int i = 0; i < n - 1; i++) {
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(1);
for (int j = 1; j < 20; j++) {
for (int i = 1; i <= n; i++) {
lift[i][j] = lift[lift[i][j - 1]][j - 1];
}
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> a >> b;
v1[i] = a; v2[i] = b;
kez[a] = i;
veg[b] = i;
}
for (int i = 0; i < m; i++) {
a = v1[i]; b = v2[i];
int c = lca(a, b);
get_path(i, a, c);
get_path(i, b, c);
}
for (int i = 0; i < m; i++) tp(i);
cout << (ans ? "Yes\n" : "No\n");
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) solve();
}
/*
1
4
1 2
1 3
1 4
3
2 3
3 4
4 2
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
37212 KB |
Output is correct |
2 |
Correct |
18 ms |
37212 KB |
Output is correct |
3 |
Correct |
17 ms |
37212 KB |
Output is correct |
4 |
Correct |
31 ms |
37332 KB |
Output is correct |
5 |
Correct |
30 ms |
37212 KB |
Output is correct |
6 |
Correct |
17 ms |
37336 KB |
Output is correct |
7 |
Correct |
18 ms |
37268 KB |
Output is correct |
8 |
Correct |
25 ms |
37200 KB |
Output is correct |
9 |
Correct |
107 ms |
39760 KB |
Output is correct |
10 |
Correct |
334 ms |
49912 KB |
Output is correct |
11 |
Correct |
21 ms |
37208 KB |
Output is correct |
12 |
Correct |
48 ms |
37212 KB |
Output is correct |
13 |
Correct |
144 ms |
78160 KB |
Output is correct |
14 |
Correct |
179 ms |
92108 KB |
Output is correct |
15 |
Runtime error |
4906 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
37204 KB |
Output is correct |
2 |
Correct |
17 ms |
37212 KB |
Output is correct |
3 |
Correct |
22 ms |
37208 KB |
Output is correct |
4 |
Correct |
20 ms |
37212 KB |
Output is correct |
5 |
Correct |
22 ms |
37200 KB |
Output is correct |
6 |
Correct |
20 ms |
37208 KB |
Output is correct |
7 |
Correct |
18 ms |
37208 KB |
Output is correct |
8 |
Correct |
19 ms |
37332 KB |
Output is correct |
9 |
Correct |
18 ms |
37212 KB |
Output is correct |
10 |
Correct |
18 ms |
37128 KB |
Output is correct |
11 |
Correct |
21 ms |
37292 KB |
Output is correct |
12 |
Correct |
20 ms |
37216 KB |
Output is correct |
13 |
Correct |
19 ms |
37212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
37204 KB |
Output is correct |
2 |
Correct |
17 ms |
37212 KB |
Output is correct |
3 |
Correct |
22 ms |
37208 KB |
Output is correct |
4 |
Correct |
20 ms |
37212 KB |
Output is correct |
5 |
Correct |
22 ms |
37200 KB |
Output is correct |
6 |
Correct |
20 ms |
37208 KB |
Output is correct |
7 |
Correct |
18 ms |
37208 KB |
Output is correct |
8 |
Correct |
19 ms |
37332 KB |
Output is correct |
9 |
Correct |
18 ms |
37212 KB |
Output is correct |
10 |
Correct |
18 ms |
37128 KB |
Output is correct |
11 |
Correct |
21 ms |
37292 KB |
Output is correct |
12 |
Correct |
20 ms |
37216 KB |
Output is correct |
13 |
Correct |
19 ms |
37212 KB |
Output is correct |
14 |
Correct |
17 ms |
37212 KB |
Output is correct |
15 |
Correct |
27 ms |
37072 KB |
Output is correct |
16 |
Correct |
19 ms |
37264 KB |
Output is correct |
17 |
Correct |
30 ms |
37212 KB |
Output is correct |
18 |
Correct |
20 ms |
37212 KB |
Output is correct |
19 |
Correct |
18 ms |
37208 KB |
Output is correct |
20 |
Correct |
17 ms |
37212 KB |
Output is correct |
21 |
Correct |
17 ms |
37224 KB |
Output is correct |
22 |
Correct |
17 ms |
37212 KB |
Output is correct |
23 |
Correct |
17 ms |
37212 KB |
Output is correct |
24 |
Correct |
19 ms |
37208 KB |
Output is correct |
25 |
Correct |
20 ms |
37212 KB |
Output is correct |
26 |
Correct |
20 ms |
37172 KB |
Output is correct |
27 |
Correct |
20 ms |
37236 KB |
Output is correct |
28 |
Correct |
26 ms |
37208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
37204 KB |
Output is correct |
2 |
Correct |
17 ms |
37212 KB |
Output is correct |
3 |
Correct |
22 ms |
37208 KB |
Output is correct |
4 |
Correct |
20 ms |
37212 KB |
Output is correct |
5 |
Correct |
22 ms |
37200 KB |
Output is correct |
6 |
Correct |
20 ms |
37208 KB |
Output is correct |
7 |
Correct |
18 ms |
37208 KB |
Output is correct |
8 |
Correct |
19 ms |
37332 KB |
Output is correct |
9 |
Correct |
18 ms |
37212 KB |
Output is correct |
10 |
Correct |
18 ms |
37128 KB |
Output is correct |
11 |
Correct |
21 ms |
37292 KB |
Output is correct |
12 |
Correct |
20 ms |
37216 KB |
Output is correct |
13 |
Correct |
19 ms |
37212 KB |
Output is correct |
14 |
Correct |
17 ms |
37212 KB |
Output is correct |
15 |
Correct |
27 ms |
37072 KB |
Output is correct |
16 |
Correct |
19 ms |
37264 KB |
Output is correct |
17 |
Correct |
30 ms |
37212 KB |
Output is correct |
18 |
Correct |
20 ms |
37212 KB |
Output is correct |
19 |
Correct |
18 ms |
37208 KB |
Output is correct |
20 |
Correct |
17 ms |
37212 KB |
Output is correct |
21 |
Correct |
17 ms |
37224 KB |
Output is correct |
22 |
Correct |
17 ms |
37212 KB |
Output is correct |
23 |
Correct |
17 ms |
37212 KB |
Output is correct |
24 |
Correct |
19 ms |
37208 KB |
Output is correct |
25 |
Correct |
20 ms |
37212 KB |
Output is correct |
26 |
Correct |
20 ms |
37172 KB |
Output is correct |
27 |
Correct |
20 ms |
37236 KB |
Output is correct |
28 |
Correct |
26 ms |
37208 KB |
Output is correct |
29 |
Correct |
25 ms |
37212 KB |
Output is correct |
30 |
Correct |
19 ms |
37208 KB |
Output is correct |
31 |
Correct |
22 ms |
37212 KB |
Output is correct |
32 |
Correct |
19 ms |
37212 KB |
Output is correct |
33 |
Correct |
18 ms |
37208 KB |
Output is correct |
34 |
Correct |
20 ms |
37212 KB |
Output is correct |
35 |
Correct |
21 ms |
37208 KB |
Output is correct |
36 |
Correct |
19 ms |
37212 KB |
Output is correct |
37 |
Correct |
20 ms |
37208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
37204 KB |
Output is correct |
2 |
Correct |
17 ms |
37212 KB |
Output is correct |
3 |
Correct |
22 ms |
37208 KB |
Output is correct |
4 |
Correct |
20 ms |
37212 KB |
Output is correct |
5 |
Correct |
22 ms |
37200 KB |
Output is correct |
6 |
Correct |
20 ms |
37208 KB |
Output is correct |
7 |
Correct |
18 ms |
37208 KB |
Output is correct |
8 |
Correct |
19 ms |
37332 KB |
Output is correct |
9 |
Correct |
18 ms |
37212 KB |
Output is correct |
10 |
Correct |
18 ms |
37128 KB |
Output is correct |
11 |
Correct |
21 ms |
37292 KB |
Output is correct |
12 |
Correct |
20 ms |
37216 KB |
Output is correct |
13 |
Correct |
19 ms |
37212 KB |
Output is correct |
14 |
Correct |
17 ms |
37212 KB |
Output is correct |
15 |
Correct |
27 ms |
37072 KB |
Output is correct |
16 |
Correct |
19 ms |
37264 KB |
Output is correct |
17 |
Correct |
30 ms |
37212 KB |
Output is correct |
18 |
Correct |
20 ms |
37212 KB |
Output is correct |
19 |
Correct |
18 ms |
37208 KB |
Output is correct |
20 |
Correct |
17 ms |
37212 KB |
Output is correct |
21 |
Correct |
17 ms |
37224 KB |
Output is correct |
22 |
Correct |
17 ms |
37212 KB |
Output is correct |
23 |
Correct |
17 ms |
37212 KB |
Output is correct |
24 |
Correct |
19 ms |
37208 KB |
Output is correct |
25 |
Correct |
20 ms |
37212 KB |
Output is correct |
26 |
Correct |
20 ms |
37172 KB |
Output is correct |
27 |
Correct |
20 ms |
37236 KB |
Output is correct |
28 |
Correct |
26 ms |
37208 KB |
Output is correct |
29 |
Correct |
25 ms |
37212 KB |
Output is correct |
30 |
Correct |
19 ms |
37208 KB |
Output is correct |
31 |
Correct |
22 ms |
37212 KB |
Output is correct |
32 |
Correct |
19 ms |
37212 KB |
Output is correct |
33 |
Correct |
18 ms |
37208 KB |
Output is correct |
34 |
Correct |
20 ms |
37212 KB |
Output is correct |
35 |
Correct |
21 ms |
37208 KB |
Output is correct |
36 |
Correct |
19 ms |
37212 KB |
Output is correct |
37 |
Correct |
20 ms |
37208 KB |
Output is correct |
38 |
Correct |
103 ms |
39768 KB |
Output is correct |
39 |
Correct |
332 ms |
49744 KB |
Output is correct |
40 |
Correct |
104 ms |
38736 KB |
Output is correct |
41 |
Correct |
71 ms |
37456 KB |
Output is correct |
42 |
Correct |
85 ms |
38992 KB |
Output is correct |
43 |
Correct |
40 ms |
37464 KB |
Output is correct |
44 |
Correct |
39 ms |
37468 KB |
Output is correct |
45 |
Correct |
98 ms |
40960 KB |
Output is correct |
46 |
Correct |
74 ms |
41072 KB |
Output is correct |
47 |
Correct |
149 ms |
44784 KB |
Output is correct |
48 |
Correct |
124 ms |
44636 KB |
Output is correct |
49 |
Correct |
79 ms |
41040 KB |
Output is correct |
50 |
Correct |
88 ms |
41012 KB |
Output is correct |
51 |
Correct |
62 ms |
42224 KB |
Output is correct |
52 |
Correct |
54 ms |
42320 KB |
Output is correct |
53 |
Correct |
31 ms |
37620 KB |
Output is correct |
54 |
Correct |
89 ms |
40908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
37164 KB |
Output is correct |
2 |
Correct |
21 ms |
37324 KB |
Output is correct |
3 |
Correct |
21 ms |
37212 KB |
Output is correct |
4 |
Correct |
21 ms |
37296 KB |
Output is correct |
5 |
Correct |
29 ms |
37124 KB |
Output is correct |
6 |
Correct |
24 ms |
37288 KB |
Output is correct |
7 |
Correct |
19 ms |
37268 KB |
Output is correct |
8 |
Correct |
26 ms |
37136 KB |
Output is correct |
9 |
Correct |
18 ms |
37212 KB |
Output is correct |
10 |
Correct |
18 ms |
37136 KB |
Output is correct |
11 |
Correct |
17 ms |
37212 KB |
Output is correct |
12 |
Correct |
18 ms |
37212 KB |
Output is correct |
13 |
Correct |
37 ms |
37212 KB |
Output is correct |
14 |
Correct |
65 ms |
37308 KB |
Output is correct |
15 |
Correct |
41 ms |
37336 KB |
Output is correct |
16 |
Correct |
74 ms |
41376 KB |
Output is correct |
17 |
Correct |
182 ms |
47592 KB |
Output is correct |
18 |
Correct |
367 ms |
71324 KB |
Output is correct |
19 |
Correct |
88 ms |
41300 KB |
Output is correct |
20 |
Correct |
85 ms |
41300 KB |
Output is correct |
21 |
Correct |
102 ms |
41296 KB |
Output is correct |
22 |
Correct |
164 ms |
47560 KB |
Output is correct |
23 |
Correct |
171 ms |
47564 KB |
Output is correct |
24 |
Correct |
146 ms |
47104 KB |
Output is correct |
25 |
Correct |
198 ms |
47192 KB |
Output is correct |
26 |
Correct |
153 ms |
48840 KB |
Output is correct |
27 |
Correct |
198 ms |
48508 KB |
Output is correct |
28 |
Correct |
201 ms |
52596 KB |
Output is correct |
29 |
Correct |
203 ms |
49524 KB |
Output is correct |
30 |
Correct |
125 ms |
46976 KB |
Output is correct |
31 |
Correct |
129 ms |
47292 KB |
Output is correct |
32 |
Correct |
123 ms |
45900 KB |
Output is correct |
33 |
Correct |
139 ms |
47324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
37212 KB |
Output is correct |
2 |
Correct |
18 ms |
37212 KB |
Output is correct |
3 |
Correct |
17 ms |
37212 KB |
Output is correct |
4 |
Correct |
31 ms |
37332 KB |
Output is correct |
5 |
Correct |
30 ms |
37212 KB |
Output is correct |
6 |
Correct |
17 ms |
37336 KB |
Output is correct |
7 |
Correct |
18 ms |
37268 KB |
Output is correct |
8 |
Correct |
25 ms |
37200 KB |
Output is correct |
9 |
Correct |
107 ms |
39760 KB |
Output is correct |
10 |
Correct |
334 ms |
49912 KB |
Output is correct |
11 |
Correct |
21 ms |
37208 KB |
Output is correct |
12 |
Correct |
48 ms |
37212 KB |
Output is correct |
13 |
Correct |
144 ms |
78160 KB |
Output is correct |
14 |
Correct |
179 ms |
92108 KB |
Output is correct |
15 |
Runtime error |
4906 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |