/**
* 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;
template <class Fun>
class y_combinator_result {
Fun fun_;
public:
template <class T>
explicit y_combinator_result(T&& fun) : fun_(std::forward<T>(fun)) {}
template <class ...Args>
decltype(auto) operator()(Args&& ...args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template <class Fun>
decltype(auto) y_combinator(Fun&& fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
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);
}
int M;
cin >> M;
vector<int> S(M), T(M);
vector<int> whoS(N, -1), whoT(N, -1);
vector<vector<pair<int, int>>> qs(N);
for (int i = 0; i < M; ++i) {
cin >> S[i] >> T[i];
--S[i], --T[i];
whoS[S[i]] = whoT[T[i]] = i;
qs[S[i]].emplace_back(T[i], i);
qs[T[i]].emplace_back(S[i], i);
}
vector<int> LCA(M);
vector<bool> vis(N);
vector<int> repr(N), par(N);
auto find = y_combinator([&](auto self, int v) -> int {
return repr[v] == v ? v : repr[v] = self(repr[v]);
});
auto dfs = y_combinator([&](auto self, int v) -> void {
vis[v] = true;
repr[v] = v;
for (int u : t[v]) {
if (!vis[u]) {
par[u] = v;
self(u);
repr[find(u)] = v;
}
}
for (auto [u, i] : qs[v]) {
if (vis[u]) {
LCA[i] = find(u);
}
}
});
dfs(0);
vector<vector<int>> h(M);
for (int i = 0; i < M; ++i) {
auto [s, t] = pair(S[i], T[i]);
int lca = LCA[i];
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> seen(M);
bool fail = false;
auto check = y_combinator([&](auto self, int v) -> void {
seen[v] = 1;
for (int u : h[v]) {
if (!seen[u]) {
self(u);
} else if (seen[u] == 1) {
fail = true;
}
}
seen[v] = 2;
});
for (int i = 0; i < M; ++i) {
if (!seen[i]) {
check(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 |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
17 ms |
828 KB |
Output is correct |
5 |
Correct |
23 ms |
984 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
4 ms |
604 KB |
Output is correct |
9 |
Correct |
124 ms |
3804 KB |
Output is correct |
10 |
Correct |
482 ms |
26668 KB |
Output is correct |
11 |
Correct |
7 ms |
604 KB |
Output is correct |
12 |
Correct |
60 ms |
1244 KB |
Output is correct |
13 |
Correct |
130 ms |
58964 KB |
Output is correct |
14 |
Correct |
160 ms |
73096 KB |
Output is correct |
15 |
Runtime error |
4959 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
716 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
716 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
464 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 |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
524 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
716 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
464 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 |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
524 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
356 KB |
Output is correct |
29 |
Correct |
4 ms |
612 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
3 ms |
356 KB |
Output is correct |
32 |
Correct |
2 ms |
356 KB |
Output is correct |
33 |
Correct |
2 ms |
348 KB |
Output is correct |
34 |
Correct |
2 ms |
348 KB |
Output is correct |
35 |
Correct |
3 ms |
356 KB |
Output is correct |
36 |
Correct |
2 ms |
348 KB |
Output is correct |
37 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
716 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
464 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 |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
524 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
356 KB |
Output is correct |
29 |
Correct |
4 ms |
612 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
3 ms |
356 KB |
Output is correct |
32 |
Correct |
2 ms |
356 KB |
Output is correct |
33 |
Correct |
2 ms |
348 KB |
Output is correct |
34 |
Correct |
2 ms |
348 KB |
Output is correct |
35 |
Correct |
3 ms |
356 KB |
Output is correct |
36 |
Correct |
2 ms |
348 KB |
Output is correct |
37 |
Correct |
2 ms |
348 KB |
Output is correct |
38 |
Correct |
118 ms |
4156 KB |
Output is correct |
39 |
Correct |
477 ms |
27048 KB |
Output is correct |
40 |
Correct |
63 ms |
2812 KB |
Output is correct |
41 |
Correct |
29 ms |
2224 KB |
Output is correct |
42 |
Correct |
50 ms |
2812 KB |
Output is correct |
43 |
Correct |
25 ms |
2360 KB |
Output is correct |
44 |
Correct |
11 ms |
860 KB |
Output is correct |
45 |
Correct |
36 ms |
13288 KB |
Output is correct |
46 |
Correct |
34 ms |
13532 KB |
Output is correct |
47 |
Correct |
109 ms |
19312 KB |
Output is correct |
48 |
Correct |
109 ms |
19604 KB |
Output is correct |
49 |
Correct |
43 ms |
13496 KB |
Output is correct |
50 |
Correct |
37 ms |
13332 KB |
Output is correct |
51 |
Correct |
29 ms |
14940 KB |
Output is correct |
52 |
Correct |
30 ms |
14892 KB |
Output is correct |
53 |
Correct |
9 ms |
1624 KB |
Output is correct |
54 |
Correct |
40 ms |
13148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
7 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 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 |
504 KB |
Output is correct |
13 |
Correct |
28 ms |
856 KB |
Output is correct |
14 |
Correct |
37 ms |
1228 KB |
Output is correct |
15 |
Correct |
34 ms |
1108 KB |
Output is correct |
16 |
Correct |
43 ms |
14676 KB |
Output is correct |
17 |
Correct |
150 ms |
25424 KB |
Output is correct |
18 |
Correct |
342 ms |
51264 KB |
Output is correct |
19 |
Correct |
46 ms |
15440 KB |
Output is correct |
20 |
Correct |
83 ms |
15624 KB |
Output is correct |
21 |
Correct |
73 ms |
15440 KB |
Output is correct |
22 |
Correct |
105 ms |
26288 KB |
Output is correct |
23 |
Correct |
99 ms |
26184 KB |
Output is correct |
24 |
Correct |
84 ms |
26060 KB |
Output is correct |
25 |
Correct |
93 ms |
26188 KB |
Output is correct |
26 |
Correct |
104 ms |
26184 KB |
Output is correct |
27 |
Correct |
140 ms |
31436 KB |
Output is correct |
28 |
Correct |
109 ms |
32716 KB |
Output is correct |
29 |
Correct |
99 ms |
29900 KB |
Output is correct |
30 |
Correct |
89 ms |
24468 KB |
Output is correct |
31 |
Correct |
79 ms |
24784 KB |
Output is correct |
32 |
Correct |
115 ms |
24204 KB |
Output is correct |
33 |
Correct |
104 ms |
24924 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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
17 ms |
828 KB |
Output is correct |
5 |
Correct |
23 ms |
984 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
4 ms |
604 KB |
Output is correct |
9 |
Correct |
124 ms |
3804 KB |
Output is correct |
10 |
Correct |
482 ms |
26668 KB |
Output is correct |
11 |
Correct |
7 ms |
604 KB |
Output is correct |
12 |
Correct |
60 ms |
1244 KB |
Output is correct |
13 |
Correct |
130 ms |
58964 KB |
Output is correct |
14 |
Correct |
160 ms |
73096 KB |
Output is correct |
15 |
Runtime error |
4959 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |