#include <bits/stdc++.h>
using namespace std;
const int N = 120005;
// https://codeforces.com/blog/entry/110222
template <int N, int C>
struct graph {
int val[N + C], go[N + C];
int ptr, sz;
int size() {
return sz;
}
void init(int n) {
sz = n;
fill(val, val + n, -1);
fill(go, go + n, -1);
ptr = n;
}
void add_edge(int u, int v) {
if (val[u] != -1) {
val[ptr] = val[u];
go[ptr] = go[u];
go[u] = ptr++;
}
val[u] = v;
}
template <class UnaryFunction>
UnaryFunction walk(int u, UnaryFunction f) {
int current = u;
while (current != -1 && val[current] != -1) {
f(val[current]);
current = go[current];
}
return f;
}
};
graph<N, N> g;
struct LCA {
int n;
vector<int> depth;
vector<vector<int>> dp;
void dfs(int u) {
depth[u] = (u == 0 ? 0 : depth[dp[0][u]] + 1);
for (int i = 0; i + 1 < 20; i++)
dp[i + 1][u] = dp[i][dp[i][u]];
g.walk(u, [&](int v) {
if (v != dp[0][u]) {
dp[0][v] = u;
dfs(v);
}
});
}
LCA() {
n = g.size();
depth.resize(n);
dp.assign(20, vector<int>(n, 0));
dfs(0);
}
int lca(int u, int v) {
if (depth[u] < depth[v])
swap(u, v);
for (int i = 20 - 1; i >= 0; i--)
if (depth[u] - (1 << i) >= depth[v])
u = dp[i][u];
if (u == v)
return u;
for (int i = 20 - 1; i >= 0; i--)
if (dp[i][u] != dp[i][v]) {
u = dp[i][u];
v = dp[i][v];
}
return dp[0][u];
}
int dist(int u, int v) {
return depth[u] + depth[v] - 2 * depth[lca(u, v)];
}
};
void solve() {
int n;
cin >> n;
g.init(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
g.add_edge(u, v);
g.add_edge(v, u);
}
int m;
cin >> m;
vector<pair<int, int>> p(m);
for (auto &[a, b] : p) {
cin >> a >> b;
a--, b--;
}
if (n <= 250 && m <= 6) {
vector<int> order(m);
iota(order.begin(), order.end(), 0);
LCA l;
do {
vector<bool> occupied(n, false);
for (auto [a, b] : p)
occupied[a] = true;
bool bad = false;
for (int i : order) {
auto [a, b] = p[i];
int lca = l.lca(a, b);
vector<int> p1, p2;
while (a != lca) {
p1.push_back(a);
a = l.dp[0][a];
}
while (b != lca) {
p2.push_back(b);
b = l.dp[0][b];
}
p2.push_back(lca);
reverse(p2.begin(), p2.end());
vector<int> path;
for (int v : p1)
path.push_back(v);
for (int v : p2)
path.push_back(v);
int k = path.size();
for (int j = 0; j + 1 < k; j++)
if (occupied[path[j + 1]])
bad = true;
occupied[path[0]] = false;
occupied[path.back()] = true;
}
if (!bad) {
cout << "Yes\n";
return;
}
} while (next_permutation(order.begin(), order.end()));
cout << "No\n";
} else {
sort(p.begin(), p.end());
for (int i = 0; i + 1 < m; i++)
if (p[i + 1].second <= p[i].second) {
cout << "No\n";
return;
}
cout << "Yes\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int q;
cin >> q;
while (q--)
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
16 ms |
596 KB |
Output is correct |
5 |
Correct |
37 ms |
976 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
69 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
21 ms |
1120 KB |
Output is correct |
10 |
Correct |
20 ms |
2964 KB |
Output is correct |
11 |
Correct |
86 ms |
468 KB |
Output is correct |
12 |
Correct |
67 ms |
1012 KB |
Output is correct |
13 |
Correct |
26 ms |
3276 KB |
Output is correct |
14 |
Correct |
25 ms |
3256 KB |
Output is correct |
15 |
Correct |
33 ms |
3268 KB |
Output is correct |
16 |
Correct |
44 ms |
3796 KB |
Output is correct |
17 |
Correct |
36 ms |
3424 KB |
Output is correct |
18 |
Correct |
47 ms |
3900 KB |
Output is correct |
19 |
Correct |
28 ms |
3404 KB |
Output is correct |
20 |
Correct |
27 ms |
3440 KB |
Output is correct |
21 |
Correct |
30 ms |
3352 KB |
Output is correct |
22 |
Correct |
28 ms |
3404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
2 ms |
336 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 |
396 KB |
Output is correct |
10 |
Correct |
1 ms |
340 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 |
1 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 |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
2 ms |
336 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 |
396 KB |
Output is correct |
10 |
Correct |
1 ms |
340 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 |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
71 ms |
400 KB |
Output is correct |
17 |
Correct |
3 ms |
340 KB |
Output is correct |
18 |
Correct |
52 ms |
376 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
22 ms |
340 KB |
Output is correct |
21 |
Correct |
18 ms |
400 KB |
Output is correct |
22 |
Correct |
28 ms |
340 KB |
Output is correct |
23 |
Correct |
13 ms |
340 KB |
Output is correct |
24 |
Correct |
11 ms |
340 KB |
Output is correct |
25 |
Correct |
72 ms |
340 KB |
Output is correct |
26 |
Correct |
10 ms |
380 KB |
Output is correct |
27 |
Correct |
12 ms |
340 KB |
Output is correct |
28 |
Correct |
39 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
2 ms |
336 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 |
396 KB |
Output is correct |
10 |
Correct |
1 ms |
340 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 |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
71 ms |
400 KB |
Output is correct |
17 |
Correct |
3 ms |
340 KB |
Output is correct |
18 |
Correct |
52 ms |
376 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
22 ms |
340 KB |
Output is correct |
21 |
Correct |
18 ms |
400 KB |
Output is correct |
22 |
Correct |
28 ms |
340 KB |
Output is correct |
23 |
Correct |
13 ms |
340 KB |
Output is correct |
24 |
Correct |
11 ms |
340 KB |
Output is correct |
25 |
Correct |
72 ms |
340 KB |
Output is correct |
26 |
Correct |
10 ms |
380 KB |
Output is correct |
27 |
Correct |
12 ms |
340 KB |
Output is correct |
28 |
Correct |
39 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
2 ms |
336 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 |
396 KB |
Output is correct |
10 |
Correct |
1 ms |
340 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 |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
71 ms |
400 KB |
Output is correct |
17 |
Correct |
3 ms |
340 KB |
Output is correct |
18 |
Correct |
52 ms |
376 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
22 ms |
340 KB |
Output is correct |
21 |
Correct |
18 ms |
400 KB |
Output is correct |
22 |
Correct |
28 ms |
340 KB |
Output is correct |
23 |
Correct |
13 ms |
340 KB |
Output is correct |
24 |
Correct |
11 ms |
340 KB |
Output is correct |
25 |
Correct |
72 ms |
340 KB |
Output is correct |
26 |
Correct |
10 ms |
380 KB |
Output is correct |
27 |
Correct |
12 ms |
340 KB |
Output is correct |
28 |
Correct |
39 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
220 KB |
Output is correct |
2 |
Correct |
1 ms |
220 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
0 ms |
224 KB |
Output is correct |
5 |
Correct |
81 ms |
476 KB |
Output is correct |
6 |
Correct |
1 ms |
352 KB |
Output is correct |
7 |
Correct |
2 ms |
356 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
14 ms |
224 KB |
Output is correct |
10 |
Correct |
12 ms |
352 KB |
Output is correct |
11 |
Correct |
41 ms |
340 KB |
Output is correct |
12 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
16 ms |
596 KB |
Output is correct |
5 |
Correct |
37 ms |
976 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
69 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
21 ms |
1120 KB |
Output is correct |
10 |
Correct |
20 ms |
2964 KB |
Output is correct |
11 |
Correct |
86 ms |
468 KB |
Output is correct |
12 |
Correct |
67 ms |
1012 KB |
Output is correct |
13 |
Correct |
26 ms |
3276 KB |
Output is correct |
14 |
Correct |
25 ms |
3256 KB |
Output is correct |
15 |
Correct |
33 ms |
3268 KB |
Output is correct |
16 |
Correct |
44 ms |
3796 KB |
Output is correct |
17 |
Correct |
36 ms |
3424 KB |
Output is correct |
18 |
Correct |
47 ms |
3900 KB |
Output is correct |
19 |
Correct |
28 ms |
3404 KB |
Output is correct |
20 |
Correct |
27 ms |
3440 KB |
Output is correct |
21 |
Correct |
30 ms |
3352 KB |
Output is correct |
22 |
Correct |
28 ms |
3404 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
2 ms |
336 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
396 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
71 ms |
400 KB |
Output is correct |
39 |
Correct |
3 ms |
340 KB |
Output is correct |
40 |
Correct |
52 ms |
376 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
22 ms |
340 KB |
Output is correct |
43 |
Correct |
18 ms |
400 KB |
Output is correct |
44 |
Correct |
28 ms |
340 KB |
Output is correct |
45 |
Correct |
13 ms |
340 KB |
Output is correct |
46 |
Correct |
11 ms |
340 KB |
Output is correct |
47 |
Correct |
72 ms |
340 KB |
Output is correct |
48 |
Correct |
10 ms |
380 KB |
Output is correct |
49 |
Correct |
12 ms |
340 KB |
Output is correct |
50 |
Correct |
39 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
344 KB |
Output is correct |
52 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
53 |
Halted |
0 ms |
0 KB |
- |