#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct staticLCA {
vector<int> depth, ord, tin;
vector<vector<int>> st;
int n{}, root = 0;
void dfs(int v, int p, vector<vector<int>> &g) {
if (v != root) {
depth[v] = depth[p] + 1;
}
tin[v] = ord.size();
ord.push_back(v);
for (int to: g[v]) {
if (to != p) {
dfs(to, v, g);
ord.push_back(v);
}
}
}
int func(int a, int b) {
return depth[a] < depth[b] ? a : b;
}
staticLCA(vector<vector<int>> &g, int _root = 0) {
init(g, _root);
}
staticLCA() = default;
void init(vector<vector<int>> &g, int _root = 0) {
n = g.size();
root = _root;
depth.assign(n, 0);
tin.resize(n);
dfs(root, -1, g);
int m = ord.size();
int logn = __lg(m) + 1;
st.resize(logn);
st[0] = ord;
for (int j = 1; j < logn; ++j) {
st[j].resize(m - (1 << j) + 1);
for (int i = 0; i <= m - (1 << j); ++i) {
st[j][i] = func(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
}
}
}
int lca(int a, int b) {
int L = min(tin[a], tin[b]), R = max(tin[a], tin[b]) + 1;
int lg = __lg(R - L);
return func(st[lg][L], st[lg][R - (1 << lg)]);
}
int lca(int a, int b, int c) {
return lca(a, b) ^ lca(a, c) ^ lca(b, c);
}
int dist(int a, int b) {
return depth[a] + depth[b] - depth[lca(a, b)] * 2;
}
};
void solve() {
int n;
cin >> n;
vector<vector<int>> adj(n);
for (int i = 1; i < n; ++i) {
int a, b;
cin >> a >> b;
a -= 1, b -= 1;
adj[a].push_back(b);
adj[b].push_back(a);
}
staticLCA tree(adj);
int m;
cin >> m;
vector<array<int, 2>> prisoner(m);
for (auto &[s, t] : prisoner) {
cin >> s >> t;
s -= 1, t -= 1;
}
vector<vector<int>> g(m);
for (int i = 0; i < m; ++i) {
auto [s, t] = prisoner[i];
for (int j = 0; j < m; ++j) {
if (j != i) {
auto [x, y] = prisoner[j];
if (tree.lca(s, t, x) == x) {
g[i].push_back(j);
}
if (tree.lca(s, t, y) == y) {
g[j].push_back(i);
}
}
}
}
bool yay = true;
vector<int> used(m);
auto dfs = [&](auto self, int v) -> void {
used[v] = 1;
for (int to : g[v]) {
if (!used[to]) {
self(self, to);
} else if (used[to] == 1) {
yay = false;
}
}
used[v] = 2;
};
for (int i = 0; i < m; ++i) {
if (!used[i]) {
dfs(dfs, i);
}
}
cout << (yay ? "Yes\n" : "No\n");
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int test = 1;
cin >> test;
while (test--) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
14 ms |
596 KB |
Output is correct |
5 |
Correct |
28 ms |
1064 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
8 ms |
536 KB |
Output is correct |
9 |
Correct |
188 ms |
4032 KB |
Output is correct |
10 |
Correct |
51 ms |
31648 KB |
Output is correct |
11 |
Correct |
9 ms |
432 KB |
Output is correct |
12 |
Correct |
117 ms |
1400 KB |
Output is correct |
13 |
Execution timed out |
5079 ms |
33552 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
392 KB |
Output is correct |
5 |
Correct |
2 ms |
396 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
392 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
392 KB |
Output is correct |
5 |
Correct |
2 ms |
396 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
392 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
472 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
396 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
392 KB |
Output is correct |
5 |
Correct |
2 ms |
396 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
392 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
472 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
396 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
9 ms |
456 KB |
Output is correct |
30 |
Correct |
5 ms |
340 KB |
Output is correct |
31 |
Correct |
5 ms |
332 KB |
Output is correct |
32 |
Correct |
3 ms |
400 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
34 |
Correct |
8 ms |
340 KB |
Output is correct |
35 |
Correct |
9 ms |
340 KB |
Output is correct |
36 |
Correct |
8 ms |
340 KB |
Output is correct |
37 |
Correct |
5 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
392 KB |
Output is correct |
5 |
Correct |
2 ms |
396 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
392 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
472 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
396 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
9 ms |
456 KB |
Output is correct |
30 |
Correct |
5 ms |
340 KB |
Output is correct |
31 |
Correct |
5 ms |
332 KB |
Output is correct |
32 |
Correct |
3 ms |
400 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
34 |
Correct |
8 ms |
340 KB |
Output is correct |
35 |
Correct |
9 ms |
340 KB |
Output is correct |
36 |
Correct |
8 ms |
340 KB |
Output is correct |
37 |
Correct |
5 ms |
340 KB |
Output is correct |
38 |
Correct |
194 ms |
4036 KB |
Output is correct |
39 |
Correct |
52 ms |
31680 KB |
Output is correct |
40 |
Correct |
238 ms |
3328 KB |
Output is correct |
41 |
Correct |
209 ms |
2508 KB |
Output is correct |
42 |
Correct |
217 ms |
3016 KB |
Output is correct |
43 |
Correct |
37 ms |
2556 KB |
Output is correct |
44 |
Correct |
139 ms |
748 KB |
Output is correct |
45 |
Correct |
71 ms |
26204 KB |
Output is correct |
46 |
Correct |
68 ms |
26172 KB |
Output is correct |
47 |
Correct |
56 ms |
28224 KB |
Output is correct |
48 |
Correct |
56 ms |
28256 KB |
Output is correct |
49 |
Correct |
60 ms |
26260 KB |
Output is correct |
50 |
Correct |
62 ms |
26200 KB |
Output is correct |
51 |
Correct |
46 ms |
26812 KB |
Output is correct |
52 |
Correct |
45 ms |
26872 KB |
Output is correct |
53 |
Correct |
146 ms |
1920 KB |
Output is correct |
54 |
Correct |
72 ms |
26108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
10 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
380 KB |
Output is correct |
13 |
Correct |
92 ms |
972 KB |
Output is correct |
14 |
Correct |
64 ms |
1084 KB |
Output is correct |
15 |
Correct |
78 ms |
960 KB |
Output is correct |
16 |
Execution timed out |
5078 ms |
26848 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
14 ms |
596 KB |
Output is correct |
5 |
Correct |
28 ms |
1064 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
8 ms |
536 KB |
Output is correct |
9 |
Correct |
188 ms |
4032 KB |
Output is correct |
10 |
Correct |
51 ms |
31648 KB |
Output is correct |
11 |
Correct |
9 ms |
432 KB |
Output is correct |
12 |
Correct |
117 ms |
1400 KB |
Output is correct |
13 |
Execution timed out |
5079 ms |
33552 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |