#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define se second
const int maxn = 120'000;
const int logn = 17;
int n, m;
vector<int> graph[1 + maxn];
int lift[1 + maxn][logn];
int dep[1 + maxn];
vector<int> dag[1 + maxn];
bool vis_dag[1 + maxn];
vector<int> top;
int pos[1 + maxn];
int in[1 + maxn];
int out[1 + maxn];
int timer;
void dfs_lift(int cur) {
timer++;
in[cur] = timer;
for(int nei : graph[cur]) {
if(nei != lift[cur][0]) {
lift[nei][0] = cur;
dep[nei] = dep[cur] + 1;
dfs_lift(nei);
}
}
timer++;
out[cur] = timer;
}
int lca(int a, int b) {
if(dep[a] > dep[b]) {
swap(a, b);
}
for(int j = logn - 1; j >= 0; j--) {
if(dep[b] - (1 << j) >= dep[a]) {
b = lift[b][j];
}
}
if(a == b) {
return a;
}
for(int j = logn - 1; j >= 0; j--) {
if(lift[a][j] != lift[b][j]) {
a = lift[a][j];
b = lift[b][j];
}
}
return lift[b][0];
}
void add_edge(int a, int b) {
dag[a].pb(b);
}
void dfs_top(int cur) {
vis_dag[cur] = true;
for(int nei : dag[cur]) {
if(!vis_dag[nei]) {
dfs_top(nei);
}
}
top.pb(cur);
}
#define ancestor(a, b) (in[a] <= in[b] && out[a] >= out[b])
bool on_path(int a, int b, int c) {
int ab = lca(a, b);
return ancestor(ab, c) && (ancestor(c, a) || ancestor(c, b));
}
bool solve() {
cin >> n;
for(int i = 1; i <= n - 1; i++) {
int a, b;
cin >> a >> b;
graph[a].pb(b);
graph[b].pb(a);
}
dfs_lift(1);
lift[1][0] = 1;
for(int j = 1; j < logn; j++) {
for(int i = 1; i <= n; i++) {
lift[i][j] = lift[lift[i][j - 1]][j - 1];
}
}
cin >> m;
vector<pii > pairs;
for(int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
pairs.pb(mp(a, b));
}
for(int i = 0; i < m; i++) {
for(int j = 0; j < m; j++) {
if(i != j) {
if(on_path(pairs[i].fi, pairs[i].se, pairs[j].fi)) {
add_edge(j + 1, i + 1);
}
if(on_path(pairs[i].fi, pairs[i].se, pairs[j].se)) {
add_edge(i + 1, j + 1);
}
}
}
}
for(int i = 1; i <= m; i++) {
if(!vis_dag[i]) {
dfs_top(i);
}
}
reverse(top.begin(), top.end());
int idx = 0;
for(int x : top) {
idx++;
pos[x] = idx;
}
for(int a = 1; a <= m; a++) {
for(int b : dag[a]) {
if(pos[b] < pos[a]) {
return false;
}
}
}
return true;
}
/*
int n, m;
vector<int> graph[1 + maxn];
int lift[1 + maxn][logn];
int dep[1 + maxn];
vector<int> dag[1 + maxn];
bool vis_dag[1 + maxn];
vector<int> top;
int pos[1 + maxn];
int in[1 + maxn];
int out[1 + maxn];
int timer;
*/
void reset() {
for(int i = 1; i <= max(n, m); i++) {
graph[i].clear();
for(int j = 0; j < logn; j++) {
lift[i][j] = 0;
}
dep[i] = 0;
dag[i].clear();
vis_dag[i] = false;
pos[i] = 0;
in[i] = 0;
out[i] = 0;
}
top.clear();
timer = 0;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
cin >> T;
while(T--) {
cout << (solve() ? "Yes" : "No") << "\n";
reset();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9560 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
7 ms |
9564 KB |
Output is correct |
5 |
Correct |
13 ms |
9748 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9756 KB |
Output is correct |
8 |
Correct |
10 ms |
9820 KB |
Output is correct |
9 |
Correct |
214 ms |
12176 KB |
Output is correct |
10 |
Correct |
52 ms |
28496 KB |
Output is correct |
11 |
Correct |
8 ms |
9564 KB |
Output is correct |
12 |
Correct |
112 ms |
9756 KB |
Output is correct |
13 |
Execution timed out |
5026 ms |
28268 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
3 ms |
9560 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
3 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
2 ms |
9564 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
3 ms |
9560 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
3 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
2 ms |
9564 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9564 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
2 ms |
9564 KB |
Output is correct |
18 |
Correct |
3 ms |
9564 KB |
Output is correct |
19 |
Correct |
3 ms |
9564 KB |
Output is correct |
20 |
Correct |
3 ms |
9564 KB |
Output is correct |
21 |
Correct |
3 ms |
9564 KB |
Output is correct |
22 |
Correct |
2 ms |
9564 KB |
Output is correct |
23 |
Correct |
2 ms |
9564 KB |
Output is correct |
24 |
Correct |
2 ms |
9564 KB |
Output is correct |
25 |
Correct |
2 ms |
9564 KB |
Output is correct |
26 |
Correct |
2 ms |
9564 KB |
Output is correct |
27 |
Correct |
2 ms |
9564 KB |
Output is correct |
28 |
Correct |
2 ms |
9564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
3 ms |
9560 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
3 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
2 ms |
9564 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9564 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
2 ms |
9564 KB |
Output is correct |
18 |
Correct |
3 ms |
9564 KB |
Output is correct |
19 |
Correct |
3 ms |
9564 KB |
Output is correct |
20 |
Correct |
3 ms |
9564 KB |
Output is correct |
21 |
Correct |
3 ms |
9564 KB |
Output is correct |
22 |
Correct |
2 ms |
9564 KB |
Output is correct |
23 |
Correct |
2 ms |
9564 KB |
Output is correct |
24 |
Correct |
2 ms |
9564 KB |
Output is correct |
25 |
Correct |
2 ms |
9564 KB |
Output is correct |
26 |
Correct |
2 ms |
9564 KB |
Output is correct |
27 |
Correct |
2 ms |
9564 KB |
Output is correct |
28 |
Correct |
2 ms |
9564 KB |
Output is correct |
29 |
Correct |
10 ms |
9816 KB |
Output is correct |
30 |
Correct |
10 ms |
9764 KB |
Output is correct |
31 |
Correct |
8 ms |
9564 KB |
Output is correct |
32 |
Correct |
6 ms |
9712 KB |
Output is correct |
33 |
Correct |
2 ms |
9564 KB |
Output is correct |
34 |
Correct |
17 ms |
9564 KB |
Output is correct |
35 |
Correct |
19 ms |
9564 KB |
Output is correct |
36 |
Correct |
16 ms |
9564 KB |
Output is correct |
37 |
Correct |
10 ms |
9564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
3 ms |
9560 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
3 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
2 ms |
9564 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9564 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
2 ms |
9564 KB |
Output is correct |
18 |
Correct |
3 ms |
9564 KB |
Output is correct |
19 |
Correct |
3 ms |
9564 KB |
Output is correct |
20 |
Correct |
3 ms |
9564 KB |
Output is correct |
21 |
Correct |
3 ms |
9564 KB |
Output is correct |
22 |
Correct |
2 ms |
9564 KB |
Output is correct |
23 |
Correct |
2 ms |
9564 KB |
Output is correct |
24 |
Correct |
2 ms |
9564 KB |
Output is correct |
25 |
Correct |
2 ms |
9564 KB |
Output is correct |
26 |
Correct |
2 ms |
9564 KB |
Output is correct |
27 |
Correct |
2 ms |
9564 KB |
Output is correct |
28 |
Correct |
2 ms |
9564 KB |
Output is correct |
29 |
Correct |
10 ms |
9816 KB |
Output is correct |
30 |
Correct |
10 ms |
9764 KB |
Output is correct |
31 |
Correct |
8 ms |
9564 KB |
Output is correct |
32 |
Correct |
6 ms |
9712 KB |
Output is correct |
33 |
Correct |
2 ms |
9564 KB |
Output is correct |
34 |
Correct |
17 ms |
9564 KB |
Output is correct |
35 |
Correct |
19 ms |
9564 KB |
Output is correct |
36 |
Correct |
16 ms |
9564 KB |
Output is correct |
37 |
Correct |
10 ms |
9564 KB |
Output is correct |
38 |
Correct |
212 ms |
12212 KB |
Output is correct |
39 |
Correct |
45 ms |
28632 KB |
Output is correct |
40 |
Correct |
423 ms |
11000 KB |
Output is correct |
41 |
Correct |
425 ms |
9816 KB |
Output is correct |
42 |
Correct |
480 ms |
11224 KB |
Output is correct |
43 |
Correct |
20 ms |
10328 KB |
Output is correct |
44 |
Correct |
370 ms |
9820 KB |
Output is correct |
45 |
Correct |
63 ms |
19804 KB |
Output is correct |
46 |
Correct |
52 ms |
19792 KB |
Output is correct |
47 |
Correct |
54 ms |
23564 KB |
Output is correct |
48 |
Correct |
56 ms |
23388 KB |
Output is correct |
49 |
Correct |
54 ms |
19800 KB |
Output is correct |
50 |
Correct |
50 ms |
19800 KB |
Output is correct |
51 |
Correct |
30 ms |
21084 KB |
Output is correct |
52 |
Correct |
29 ms |
20936 KB |
Output is correct |
53 |
Correct |
348 ms |
10060 KB |
Output is correct |
54 |
Correct |
56 ms |
19796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9560 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
1 ms |
9564 KB |
Output is correct |
4 |
Correct |
1 ms |
9564 KB |
Output is correct |
5 |
Correct |
7 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
3 ms |
9560 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
16 ms |
9760 KB |
Output is correct |
13 |
Correct |
171 ms |
9772 KB |
Output is correct |
14 |
Correct |
87 ms |
9740 KB |
Output is correct |
15 |
Correct |
127 ms |
9560 KB |
Output is correct |
16 |
Execution timed out |
5065 ms |
20060 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9560 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
7 ms |
9564 KB |
Output is correct |
5 |
Correct |
13 ms |
9748 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9756 KB |
Output is correct |
8 |
Correct |
10 ms |
9820 KB |
Output is correct |
9 |
Correct |
214 ms |
12176 KB |
Output is correct |
10 |
Correct |
52 ms |
28496 KB |
Output is correct |
11 |
Correct |
8 ms |
9564 KB |
Output is correct |
12 |
Correct |
112 ms |
9756 KB |
Output is correct |
13 |
Execution timed out |
5026 ms |
28268 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |