#include <iostream>
#include <vector>
using namespace std;
int n, m, sz, c;
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 <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;
}
}
/*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 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 >> v1[i] >> v2[i];
for (int j = 0; j < i; j++) {
if (on_path(v1[i], v2[i], v1[j])) g2[j].push_back(i);
if (on_path(v1[i], v2[i], v2[j])) g2[i].push_back(j);
swap(i, j);
if (on_path(v1[i], v2[i], v1[j])) g2[j].push_back(i);
if (on_path(v1[i], v2[i], v2[j])) g2[i].push_back(j);
swap(i, j);
}
}
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 |
18 ms |
35676 KB |
Output is correct |
2 |
Correct |
15 ms |
35676 KB |
Output is correct |
3 |
Correct |
18 ms |
35676 KB |
Output is correct |
4 |
Correct |
23 ms |
35732 KB |
Output is correct |
5 |
Correct |
32 ms |
35580 KB |
Output is correct |
6 |
Correct |
18 ms |
35676 KB |
Output is correct |
7 |
Correct |
18 ms |
35676 KB |
Output is correct |
8 |
Correct |
53 ms |
35812 KB |
Output is correct |
9 |
Correct |
985 ms |
38228 KB |
Output is correct |
10 |
Correct |
104 ms |
48208 KB |
Output is correct |
11 |
Correct |
35 ms |
35672 KB |
Output is correct |
12 |
Correct |
465 ms |
35744 KB |
Output is correct |
13 |
Execution timed out |
5030 ms |
48464 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
35672 KB |
Output is correct |
2 |
Correct |
17 ms |
35672 KB |
Output is correct |
3 |
Correct |
17 ms |
35676 KB |
Output is correct |
4 |
Correct |
16 ms |
35656 KB |
Output is correct |
5 |
Correct |
16 ms |
35636 KB |
Output is correct |
6 |
Correct |
18 ms |
35676 KB |
Output is correct |
7 |
Correct |
22 ms |
35676 KB |
Output is correct |
8 |
Correct |
18 ms |
35628 KB |
Output is correct |
9 |
Correct |
17 ms |
35708 KB |
Output is correct |
10 |
Correct |
19 ms |
35676 KB |
Output is correct |
11 |
Correct |
17 ms |
35676 KB |
Output is correct |
12 |
Correct |
17 ms |
35676 KB |
Output is correct |
13 |
Correct |
16 ms |
35676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
35672 KB |
Output is correct |
2 |
Correct |
17 ms |
35672 KB |
Output is correct |
3 |
Correct |
17 ms |
35676 KB |
Output is correct |
4 |
Correct |
16 ms |
35656 KB |
Output is correct |
5 |
Correct |
16 ms |
35636 KB |
Output is correct |
6 |
Correct |
18 ms |
35676 KB |
Output is correct |
7 |
Correct |
22 ms |
35676 KB |
Output is correct |
8 |
Correct |
18 ms |
35628 KB |
Output is correct |
9 |
Correct |
17 ms |
35708 KB |
Output is correct |
10 |
Correct |
19 ms |
35676 KB |
Output is correct |
11 |
Correct |
17 ms |
35676 KB |
Output is correct |
12 |
Correct |
17 ms |
35676 KB |
Output is correct |
13 |
Correct |
16 ms |
35676 KB |
Output is correct |
14 |
Correct |
17 ms |
35676 KB |
Output is correct |
15 |
Correct |
18 ms |
35676 KB |
Output is correct |
16 |
Correct |
17 ms |
35668 KB |
Output is correct |
17 |
Correct |
17 ms |
35676 KB |
Output is correct |
18 |
Correct |
16 ms |
35764 KB |
Output is correct |
19 |
Correct |
15 ms |
35676 KB |
Output is correct |
20 |
Correct |
17 ms |
35676 KB |
Output is correct |
21 |
Correct |
17 ms |
35676 KB |
Output is correct |
22 |
Correct |
16 ms |
35676 KB |
Output is correct |
23 |
Correct |
17 ms |
35720 KB |
Output is correct |
24 |
Correct |
17 ms |
35676 KB |
Output is correct |
25 |
Correct |
18 ms |
35656 KB |
Output is correct |
26 |
Correct |
16 ms |
35676 KB |
Output is correct |
27 |
Correct |
18 ms |
35672 KB |
Output is correct |
28 |
Correct |
16 ms |
35672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
35672 KB |
Output is correct |
2 |
Correct |
17 ms |
35672 KB |
Output is correct |
3 |
Correct |
17 ms |
35676 KB |
Output is correct |
4 |
Correct |
16 ms |
35656 KB |
Output is correct |
5 |
Correct |
16 ms |
35636 KB |
Output is correct |
6 |
Correct |
18 ms |
35676 KB |
Output is correct |
7 |
Correct |
22 ms |
35676 KB |
Output is correct |
8 |
Correct |
18 ms |
35628 KB |
Output is correct |
9 |
Correct |
17 ms |
35708 KB |
Output is correct |
10 |
Correct |
19 ms |
35676 KB |
Output is correct |
11 |
Correct |
17 ms |
35676 KB |
Output is correct |
12 |
Correct |
17 ms |
35676 KB |
Output is correct |
13 |
Correct |
16 ms |
35676 KB |
Output is correct |
14 |
Correct |
17 ms |
35676 KB |
Output is correct |
15 |
Correct |
18 ms |
35676 KB |
Output is correct |
16 |
Correct |
17 ms |
35668 KB |
Output is correct |
17 |
Correct |
17 ms |
35676 KB |
Output is correct |
18 |
Correct |
16 ms |
35764 KB |
Output is correct |
19 |
Correct |
15 ms |
35676 KB |
Output is correct |
20 |
Correct |
17 ms |
35676 KB |
Output is correct |
21 |
Correct |
17 ms |
35676 KB |
Output is correct |
22 |
Correct |
16 ms |
35676 KB |
Output is correct |
23 |
Correct |
17 ms |
35720 KB |
Output is correct |
24 |
Correct |
17 ms |
35676 KB |
Output is correct |
25 |
Correct |
18 ms |
35656 KB |
Output is correct |
26 |
Correct |
16 ms |
35676 KB |
Output is correct |
27 |
Correct |
18 ms |
35672 KB |
Output is correct |
28 |
Correct |
16 ms |
35672 KB |
Output is correct |
29 |
Correct |
55 ms |
35672 KB |
Output is correct |
30 |
Correct |
46 ms |
35632 KB |
Output is correct |
31 |
Correct |
40 ms |
35676 KB |
Output is correct |
32 |
Correct |
33 ms |
35768 KB |
Output is correct |
33 |
Correct |
19 ms |
35676 KB |
Output is correct |
34 |
Correct |
81 ms |
35676 KB |
Output is correct |
35 |
Correct |
84 ms |
35676 KB |
Output is correct |
36 |
Correct |
73 ms |
35672 KB |
Output is correct |
37 |
Correct |
52 ms |
35672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
35672 KB |
Output is correct |
2 |
Correct |
17 ms |
35672 KB |
Output is correct |
3 |
Correct |
17 ms |
35676 KB |
Output is correct |
4 |
Correct |
16 ms |
35656 KB |
Output is correct |
5 |
Correct |
16 ms |
35636 KB |
Output is correct |
6 |
Correct |
18 ms |
35676 KB |
Output is correct |
7 |
Correct |
22 ms |
35676 KB |
Output is correct |
8 |
Correct |
18 ms |
35628 KB |
Output is correct |
9 |
Correct |
17 ms |
35708 KB |
Output is correct |
10 |
Correct |
19 ms |
35676 KB |
Output is correct |
11 |
Correct |
17 ms |
35676 KB |
Output is correct |
12 |
Correct |
17 ms |
35676 KB |
Output is correct |
13 |
Correct |
16 ms |
35676 KB |
Output is correct |
14 |
Correct |
17 ms |
35676 KB |
Output is correct |
15 |
Correct |
18 ms |
35676 KB |
Output is correct |
16 |
Correct |
17 ms |
35668 KB |
Output is correct |
17 |
Correct |
17 ms |
35676 KB |
Output is correct |
18 |
Correct |
16 ms |
35764 KB |
Output is correct |
19 |
Correct |
15 ms |
35676 KB |
Output is correct |
20 |
Correct |
17 ms |
35676 KB |
Output is correct |
21 |
Correct |
17 ms |
35676 KB |
Output is correct |
22 |
Correct |
16 ms |
35676 KB |
Output is correct |
23 |
Correct |
17 ms |
35720 KB |
Output is correct |
24 |
Correct |
17 ms |
35676 KB |
Output is correct |
25 |
Correct |
18 ms |
35656 KB |
Output is correct |
26 |
Correct |
16 ms |
35676 KB |
Output is correct |
27 |
Correct |
18 ms |
35672 KB |
Output is correct |
28 |
Correct |
16 ms |
35672 KB |
Output is correct |
29 |
Correct |
55 ms |
35672 KB |
Output is correct |
30 |
Correct |
46 ms |
35632 KB |
Output is correct |
31 |
Correct |
40 ms |
35676 KB |
Output is correct |
32 |
Correct |
33 ms |
35768 KB |
Output is correct |
33 |
Correct |
19 ms |
35676 KB |
Output is correct |
34 |
Correct |
81 ms |
35676 KB |
Output is correct |
35 |
Correct |
84 ms |
35676 KB |
Output is correct |
36 |
Correct |
73 ms |
35672 KB |
Output is correct |
37 |
Correct |
52 ms |
35672 KB |
Output is correct |
38 |
Correct |
1052 ms |
38140 KB |
Output is correct |
39 |
Correct |
109 ms |
48212 KB |
Output is correct |
40 |
Correct |
1692 ms |
36956 KB |
Output is correct |
41 |
Correct |
1719 ms |
35932 KB |
Output is correct |
42 |
Correct |
1690 ms |
37400 KB |
Output is correct |
43 |
Correct |
46 ms |
35928 KB |
Output is correct |
44 |
Correct |
1541 ms |
35776 KB |
Output is correct |
45 |
Correct |
184 ms |
39500 KB |
Output is correct |
46 |
Correct |
194 ms |
39528 KB |
Output is correct |
47 |
Correct |
187 ms |
43100 KB |
Output is correct |
48 |
Correct |
186 ms |
43240 KB |
Output is correct |
49 |
Correct |
192 ms |
39508 KB |
Output is correct |
50 |
Correct |
178 ms |
39508 KB |
Output is correct |
51 |
Correct |
56 ms |
40528 KB |
Output is correct |
52 |
Correct |
56 ms |
40768 KB |
Output is correct |
53 |
Correct |
1384 ms |
35920 KB |
Output is correct |
54 |
Correct |
156 ms |
39248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
35672 KB |
Output is correct |
2 |
Correct |
18 ms |
35676 KB |
Output is correct |
3 |
Correct |
19 ms |
35672 KB |
Output is correct |
4 |
Correct |
15 ms |
35672 KB |
Output is correct |
5 |
Correct |
31 ms |
35740 KB |
Output is correct |
6 |
Correct |
17 ms |
35676 KB |
Output is correct |
7 |
Correct |
16 ms |
35676 KB |
Output is correct |
8 |
Correct |
18 ms |
35676 KB |
Output is correct |
9 |
Correct |
18 ms |
35676 KB |
Output is correct |
10 |
Correct |
16 ms |
35712 KB |
Output is correct |
11 |
Correct |
15 ms |
35676 KB |
Output is correct |
12 |
Correct |
77 ms |
35676 KB |
Output is correct |
13 |
Correct |
632 ms |
35676 KB |
Output is correct |
14 |
Correct |
297 ms |
35676 KB |
Output is correct |
15 |
Correct |
498 ms |
35756 KB |
Output is correct |
16 |
Execution timed out |
5077 ms |
39764 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
35676 KB |
Output is correct |
2 |
Correct |
15 ms |
35676 KB |
Output is correct |
3 |
Correct |
18 ms |
35676 KB |
Output is correct |
4 |
Correct |
23 ms |
35732 KB |
Output is correct |
5 |
Correct |
32 ms |
35580 KB |
Output is correct |
6 |
Correct |
18 ms |
35676 KB |
Output is correct |
7 |
Correct |
18 ms |
35676 KB |
Output is correct |
8 |
Correct |
53 ms |
35812 KB |
Output is correct |
9 |
Correct |
985 ms |
38228 KB |
Output is correct |
10 |
Correct |
104 ms |
48208 KB |
Output is correct |
11 |
Correct |
35 ms |
35672 KB |
Output is correct |
12 |
Correct |
465 ms |
35744 KB |
Output is correct |
13 |
Execution timed out |
5030 ms |
48464 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |