#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 |
17 ms |
35672 KB |
Output is correct |
2 |
Correct |
16 ms |
35672 KB |
Output is correct |
3 |
Correct |
17 ms |
35676 KB |
Output is correct |
4 |
Correct |
23 ms |
35692 KB |
Output is correct |
5 |
Correct |
52 ms |
35676 KB |
Output is correct |
6 |
Correct |
17 ms |
35676 KB |
Output is correct |
7 |
Correct |
17 ms |
35676 KB |
Output is correct |
8 |
Correct |
83 ms |
35668 KB |
Output is correct |
9 |
Correct |
1039 ms |
38112 KB |
Output is correct |
10 |
Correct |
104 ms |
48076 KB |
Output is correct |
11 |
Correct |
39 ms |
35732 KB |
Output is correct |
12 |
Correct |
478 ms |
35768 KB |
Output is correct |
13 |
Execution timed out |
5088 ms |
48544 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
35676 KB |
Output is correct |
2 |
Correct |
16 ms |
35716 KB |
Output is correct |
3 |
Correct |
22 ms |
35676 KB |
Output is correct |
4 |
Correct |
17 ms |
35676 KB |
Output is correct |
5 |
Correct |
23 ms |
35668 KB |
Output is correct |
6 |
Correct |
18 ms |
35616 KB |
Output is correct |
7 |
Correct |
29 ms |
35676 KB |
Output is correct |
8 |
Correct |
16 ms |
35676 KB |
Output is correct |
9 |
Correct |
16 ms |
35676 KB |
Output is correct |
10 |
Correct |
16 ms |
35676 KB |
Output is correct |
11 |
Correct |
28 ms |
35676 KB |
Output is correct |
12 |
Correct |
28 ms |
35640 KB |
Output is correct |
13 |
Correct |
18 ms |
35672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
35676 KB |
Output is correct |
2 |
Correct |
16 ms |
35716 KB |
Output is correct |
3 |
Correct |
22 ms |
35676 KB |
Output is correct |
4 |
Correct |
17 ms |
35676 KB |
Output is correct |
5 |
Correct |
23 ms |
35668 KB |
Output is correct |
6 |
Correct |
18 ms |
35616 KB |
Output is correct |
7 |
Correct |
29 ms |
35676 KB |
Output is correct |
8 |
Correct |
16 ms |
35676 KB |
Output is correct |
9 |
Correct |
16 ms |
35676 KB |
Output is correct |
10 |
Correct |
16 ms |
35676 KB |
Output is correct |
11 |
Correct |
28 ms |
35676 KB |
Output is correct |
12 |
Correct |
28 ms |
35640 KB |
Output is correct |
13 |
Correct |
18 ms |
35672 KB |
Output is correct |
14 |
Correct |
21 ms |
35676 KB |
Output is correct |
15 |
Correct |
15 ms |
35544 KB |
Output is correct |
16 |
Correct |
16 ms |
35672 KB |
Output is correct |
17 |
Correct |
18 ms |
35676 KB |
Output is correct |
18 |
Correct |
17 ms |
35676 KB |
Output is correct |
19 |
Correct |
18 ms |
35672 KB |
Output is correct |
20 |
Correct |
15 ms |
35684 KB |
Output is correct |
21 |
Correct |
17 ms |
35676 KB |
Output is correct |
22 |
Correct |
19 ms |
35676 KB |
Output is correct |
23 |
Correct |
16 ms |
35512 KB |
Output is correct |
24 |
Correct |
17 ms |
35676 KB |
Output is correct |
25 |
Correct |
18 ms |
35676 KB |
Output is correct |
26 |
Correct |
18 ms |
35564 KB |
Output is correct |
27 |
Correct |
18 ms |
35676 KB |
Output is correct |
28 |
Correct |
16 ms |
35676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
35676 KB |
Output is correct |
2 |
Correct |
16 ms |
35716 KB |
Output is correct |
3 |
Correct |
22 ms |
35676 KB |
Output is correct |
4 |
Correct |
17 ms |
35676 KB |
Output is correct |
5 |
Correct |
23 ms |
35668 KB |
Output is correct |
6 |
Correct |
18 ms |
35616 KB |
Output is correct |
7 |
Correct |
29 ms |
35676 KB |
Output is correct |
8 |
Correct |
16 ms |
35676 KB |
Output is correct |
9 |
Correct |
16 ms |
35676 KB |
Output is correct |
10 |
Correct |
16 ms |
35676 KB |
Output is correct |
11 |
Correct |
28 ms |
35676 KB |
Output is correct |
12 |
Correct |
28 ms |
35640 KB |
Output is correct |
13 |
Correct |
18 ms |
35672 KB |
Output is correct |
14 |
Correct |
21 ms |
35676 KB |
Output is correct |
15 |
Correct |
15 ms |
35544 KB |
Output is correct |
16 |
Correct |
16 ms |
35672 KB |
Output is correct |
17 |
Correct |
18 ms |
35676 KB |
Output is correct |
18 |
Correct |
17 ms |
35676 KB |
Output is correct |
19 |
Correct |
18 ms |
35672 KB |
Output is correct |
20 |
Correct |
15 ms |
35684 KB |
Output is correct |
21 |
Correct |
17 ms |
35676 KB |
Output is correct |
22 |
Correct |
19 ms |
35676 KB |
Output is correct |
23 |
Correct |
16 ms |
35512 KB |
Output is correct |
24 |
Correct |
17 ms |
35676 KB |
Output is correct |
25 |
Correct |
18 ms |
35676 KB |
Output is correct |
26 |
Correct |
18 ms |
35564 KB |
Output is correct |
27 |
Correct |
18 ms |
35676 KB |
Output is correct |
28 |
Correct |
16 ms |
35676 KB |
Output is correct |
29 |
Correct |
52 ms |
35776 KB |
Output is correct |
30 |
Correct |
50 ms |
35764 KB |
Output is correct |
31 |
Correct |
40 ms |
35676 KB |
Output is correct |
32 |
Correct |
28 ms |
35676 KB |
Output is correct |
33 |
Correct |
18 ms |
35672 KB |
Output is correct |
34 |
Correct |
98 ms |
35768 KB |
Output is correct |
35 |
Correct |
87 ms |
35612 KB |
Output is correct |
36 |
Correct |
75 ms |
35748 KB |
Output is correct |
37 |
Correct |
48 ms |
35544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
35676 KB |
Output is correct |
2 |
Correct |
16 ms |
35716 KB |
Output is correct |
3 |
Correct |
22 ms |
35676 KB |
Output is correct |
4 |
Correct |
17 ms |
35676 KB |
Output is correct |
5 |
Correct |
23 ms |
35668 KB |
Output is correct |
6 |
Correct |
18 ms |
35616 KB |
Output is correct |
7 |
Correct |
29 ms |
35676 KB |
Output is correct |
8 |
Correct |
16 ms |
35676 KB |
Output is correct |
9 |
Correct |
16 ms |
35676 KB |
Output is correct |
10 |
Correct |
16 ms |
35676 KB |
Output is correct |
11 |
Correct |
28 ms |
35676 KB |
Output is correct |
12 |
Correct |
28 ms |
35640 KB |
Output is correct |
13 |
Correct |
18 ms |
35672 KB |
Output is correct |
14 |
Correct |
21 ms |
35676 KB |
Output is correct |
15 |
Correct |
15 ms |
35544 KB |
Output is correct |
16 |
Correct |
16 ms |
35672 KB |
Output is correct |
17 |
Correct |
18 ms |
35676 KB |
Output is correct |
18 |
Correct |
17 ms |
35676 KB |
Output is correct |
19 |
Correct |
18 ms |
35672 KB |
Output is correct |
20 |
Correct |
15 ms |
35684 KB |
Output is correct |
21 |
Correct |
17 ms |
35676 KB |
Output is correct |
22 |
Correct |
19 ms |
35676 KB |
Output is correct |
23 |
Correct |
16 ms |
35512 KB |
Output is correct |
24 |
Correct |
17 ms |
35676 KB |
Output is correct |
25 |
Correct |
18 ms |
35676 KB |
Output is correct |
26 |
Correct |
18 ms |
35564 KB |
Output is correct |
27 |
Correct |
18 ms |
35676 KB |
Output is correct |
28 |
Correct |
16 ms |
35676 KB |
Output is correct |
29 |
Correct |
52 ms |
35776 KB |
Output is correct |
30 |
Correct |
50 ms |
35764 KB |
Output is correct |
31 |
Correct |
40 ms |
35676 KB |
Output is correct |
32 |
Correct |
28 ms |
35676 KB |
Output is correct |
33 |
Correct |
18 ms |
35672 KB |
Output is correct |
34 |
Correct |
98 ms |
35768 KB |
Output is correct |
35 |
Correct |
87 ms |
35612 KB |
Output is correct |
36 |
Correct |
75 ms |
35748 KB |
Output is correct |
37 |
Correct |
48 ms |
35544 KB |
Output is correct |
38 |
Correct |
993 ms |
38124 KB |
Output is correct |
39 |
Correct |
106 ms |
48276 KB |
Output is correct |
40 |
Correct |
1722 ms |
36952 KB |
Output is correct |
41 |
Correct |
1774 ms |
35952 KB |
Output is correct |
42 |
Correct |
1695 ms |
37348 KB |
Output is correct |
43 |
Correct |
43 ms |
36096 KB |
Output is correct |
44 |
Correct |
1556 ms |
36044 KB |
Output is correct |
45 |
Correct |
213 ms |
39408 KB |
Output is correct |
46 |
Correct |
188 ms |
39508 KB |
Output is correct |
47 |
Correct |
183 ms |
43092 KB |
Output is correct |
48 |
Correct |
195 ms |
43240 KB |
Output is correct |
49 |
Correct |
214 ms |
39500 KB |
Output is correct |
50 |
Correct |
192 ms |
39660 KB |
Output is correct |
51 |
Correct |
64 ms |
40532 KB |
Output is correct |
52 |
Correct |
61 ms |
40760 KB |
Output is correct |
53 |
Correct |
1391 ms |
35920 KB |
Output is correct |
54 |
Correct |
158 ms |
39480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
35604 KB |
Output is correct |
2 |
Correct |
17 ms |
35692 KB |
Output is correct |
3 |
Correct |
17 ms |
35672 KB |
Output is correct |
4 |
Correct |
19 ms |
35672 KB |
Output is correct |
5 |
Correct |
37 ms |
35676 KB |
Output is correct |
6 |
Correct |
16 ms |
35672 KB |
Output is correct |
7 |
Correct |
17 ms |
35676 KB |
Output is correct |
8 |
Correct |
17 ms |
35676 KB |
Output is correct |
9 |
Correct |
15 ms |
35676 KB |
Output is correct |
10 |
Correct |
17 ms |
35680 KB |
Output is correct |
11 |
Correct |
19 ms |
35648 KB |
Output is correct |
12 |
Correct |
73 ms |
35688 KB |
Output is correct |
13 |
Correct |
639 ms |
35932 KB |
Output is correct |
14 |
Correct |
295 ms |
35920 KB |
Output is correct |
15 |
Correct |
501 ms |
35576 KB |
Output is correct |
16 |
Execution timed out |
5025 ms |
39700 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
35672 KB |
Output is correct |
2 |
Correct |
16 ms |
35672 KB |
Output is correct |
3 |
Correct |
17 ms |
35676 KB |
Output is correct |
4 |
Correct |
23 ms |
35692 KB |
Output is correct |
5 |
Correct |
52 ms |
35676 KB |
Output is correct |
6 |
Correct |
17 ms |
35676 KB |
Output is correct |
7 |
Correct |
17 ms |
35676 KB |
Output is correct |
8 |
Correct |
83 ms |
35668 KB |
Output is correct |
9 |
Correct |
1039 ms |
38112 KB |
Output is correct |
10 |
Correct |
104 ms |
48076 KB |
Output is correct |
11 |
Correct |
39 ms |
35732 KB |
Output is correct |
12 |
Correct |
478 ms |
35768 KB |
Output is correct |
13 |
Execution timed out |
5088 ms |
48544 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |