/**
* author: NimaAryan
* created: 2023-10-12 11:20:40
**/
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,unroll-loops")
#ifdef LOCAL
#include "algo/debug.h"
#endif
using i64 = long long;
void solve() {
int N;
cin >> N;
vector<vector<int>> t(N);
for (int i = 0; i < N - 1; ++i) {
int A, B;
cin >> A >> B;
--A, --B;
t[A].push_back(B);
t[B].push_back(A);
}
vector up(20, vector<int>(N, -1));
vector<int> par(N);
vector<int> dep(N);
auto reroot = [&](auto self, int v, int p) -> void {
up[0][v] = par[v] = p;
for (int k = 1; k < 20; ++k) {
int mid = up[k - 1][v];
if (mid != -1) {
up[k][v] = up[k - 1][mid];
}
}
for (int u : t[v]) {
if (u != p) {
dep[u] = dep[v] + 1;
self(self, u, v);
}
}
};
reroot(reroot, 0, -1);
int M;
cin >> M;
vector<int> S(M), T(M);
vector<int> whoS(N, -1), whoT(N, -1);
for (int i = 0; i < M; ++i) {
cin >> S[i] >> T[i];
--S[i], --T[i];
whoS[S[i]] = whoT[T[i]] = i;
}
auto jump = [&](int a, int x) {
int at = a;
for (int k = 0; k < 20; ++k) {
if (x >> k & 1) {
at = up[k][at];
if (at == -1) {
break;
}
}
}
return at;
};
auto LCA = [&](int a, int b) {
if (dep[a] < dep[b]) {
swap(a, b);
}
a = jump(a, dep[a] - dep[b]);
if (a == b) {
return a;
}
for (int k = 20 - 1; k >= 0; --k) {
int na = up[k][a];
int nb = up[k][b];
if (na != nb) {
a = na;
b = nb;
}
}
return par[a];
};
vector<vector<int>> h(M);
for (int i = 0; i < M; ++i) {
auto [s, t] = pair(S[i], T[i]);
int lca = LCA(s, t);
vector<int> path{lca};
while (s != lca) {
path.push_back(s);
s = par[s];
}
while (t != lca) {
path.push_back(t);
t = par[t];
}
for (int x : path) {
if (whoS[x] != -1 && whoS[x] != i) {
h[i].push_back(whoS[x]);
}
if (whoT[x] != -1 && whoT[x] != i) {
h[whoT[x]].push_back(i);
}
}
}
vector<int> vis(M);
bool fail = false;
auto dfs = [&](auto self, int v) -> void {
vis[v] = 1;
for (int u : h[v]) {
if (!vis[u]) {
self(self, u);
} else if (vis[u] == 1) {
fail = true;
}
}
vis[v] = 2;
};
for (int i = 0; i < M; ++i) {
if (!vis[i]) {
dfs(dfs, i);
}
}
cout << (fail ? "No" : "Yes") << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int Q;
cin >> Q;
while (Q--) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
12 ms |
604 KB |
Output is correct |
5 |
Correct |
26 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
4 ms |
604 KB |
Output is correct |
9 |
Correct |
118 ms |
4288 KB |
Output is correct |
10 |
Correct |
520 ms |
28164 KB |
Output is correct |
11 |
Correct |
7 ms |
600 KB |
Output is correct |
12 |
Correct |
68 ms |
1372 KB |
Output is correct |
13 |
Correct |
160 ms |
57720 KB |
Output is correct |
14 |
Correct |
197 ms |
71944 KB |
Output is correct |
15 |
Runtime error |
4491 ms |
1048580 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
352 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
464 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
472 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
352 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
464 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
472 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
352 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
352 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
464 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
472 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
352 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
352 KB |
Output is correct |
29 |
Correct |
3 ms |
612 KB |
Output is correct |
30 |
Correct |
2 ms |
356 KB |
Output is correct |
31 |
Correct |
3 ms |
352 KB |
Output is correct |
32 |
Correct |
2 ms |
360 KB |
Output is correct |
33 |
Correct |
2 ms |
552 KB |
Output is correct |
34 |
Correct |
2 ms |
552 KB |
Output is correct |
35 |
Correct |
3 ms |
468 KB |
Output is correct |
36 |
Correct |
2 ms |
464 KB |
Output is correct |
37 |
Correct |
1 ms |
352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
352 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
464 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
472 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
352 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
352 KB |
Output is correct |
29 |
Correct |
3 ms |
612 KB |
Output is correct |
30 |
Correct |
2 ms |
356 KB |
Output is correct |
31 |
Correct |
3 ms |
352 KB |
Output is correct |
32 |
Correct |
2 ms |
360 KB |
Output is correct |
33 |
Correct |
2 ms |
552 KB |
Output is correct |
34 |
Correct |
2 ms |
552 KB |
Output is correct |
35 |
Correct |
3 ms |
468 KB |
Output is correct |
36 |
Correct |
2 ms |
464 KB |
Output is correct |
37 |
Correct |
1 ms |
352 KB |
Output is correct |
38 |
Correct |
114 ms |
4308 KB |
Output is correct |
39 |
Correct |
538 ms |
28220 KB |
Output is correct |
40 |
Correct |
66 ms |
3108 KB |
Output is correct |
41 |
Correct |
32 ms |
2488 KB |
Output is correct |
42 |
Correct |
51 ms |
3232 KB |
Output is correct |
43 |
Correct |
27 ms |
2496 KB |
Output is correct |
44 |
Correct |
9 ms |
860 KB |
Output is correct |
45 |
Correct |
70 ms |
19676 KB |
Output is correct |
46 |
Correct |
57 ms |
19832 KB |
Output is correct |
47 |
Correct |
116 ms |
23160 KB |
Output is correct |
48 |
Correct |
117 ms |
23164 KB |
Output is correct |
49 |
Correct |
55 ms |
20032 KB |
Output is correct |
50 |
Correct |
55 ms |
19804 KB |
Output is correct |
51 |
Correct |
55 ms |
20664 KB |
Output is correct |
52 |
Correct |
55 ms |
20640 KB |
Output is correct |
53 |
Correct |
9 ms |
1804 KB |
Output is correct |
54 |
Correct |
71 ms |
19628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
7 ms |
616 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
28 ms |
856 KB |
Output is correct |
14 |
Correct |
36 ms |
1108 KB |
Output is correct |
15 |
Correct |
33 ms |
1116 KB |
Output is correct |
16 |
Correct |
66 ms |
20604 KB |
Output is correct |
17 |
Correct |
160 ms |
28796 KB |
Output is correct |
18 |
Correct |
382 ms |
52916 KB |
Output is correct |
19 |
Correct |
70 ms |
21068 KB |
Output is correct |
20 |
Correct |
84 ms |
21176 KB |
Output is correct |
21 |
Correct |
70 ms |
20860 KB |
Output is correct |
22 |
Correct |
133 ms |
28948 KB |
Output is correct |
23 |
Correct |
98 ms |
28792 KB |
Output is correct |
24 |
Correct |
112 ms |
28792 KB |
Output is correct |
25 |
Correct |
101 ms |
29052 KB |
Output is correct |
26 |
Correct |
110 ms |
29012 KB |
Output is correct |
27 |
Correct |
150 ms |
33132 KB |
Output is correct |
28 |
Correct |
183 ms |
34424 KB |
Output is correct |
29 |
Correct |
200 ms |
31868 KB |
Output is correct |
30 |
Correct |
105 ms |
26488 KB |
Output is correct |
31 |
Correct |
96 ms |
27008 KB |
Output is correct |
32 |
Correct |
98 ms |
26356 KB |
Output is correct |
33 |
Correct |
107 ms |
27184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
12 ms |
604 KB |
Output is correct |
5 |
Correct |
26 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
4 ms |
604 KB |
Output is correct |
9 |
Correct |
118 ms |
4288 KB |
Output is correct |
10 |
Correct |
520 ms |
28164 KB |
Output is correct |
11 |
Correct |
7 ms |
600 KB |
Output is correct |
12 |
Correct |
68 ms |
1372 KB |
Output is correct |
13 |
Correct |
160 ms |
57720 KB |
Output is correct |
14 |
Correct |
197 ms |
71944 KB |
Output is correct |
15 |
Runtime error |
4491 ms |
1048580 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |