#include <bits/stdc++.h>
using namespace std;
#define int int64_t
#define pii pair<int, int>
#define st first
#define nd second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
vector<int> G[120009];
vector<int> G2[120009];
int tin[120009], tout[120009], T;
int jp2[120009][20];
int deg[120009];
pii tab[120009];
void dfs(int x, int y) {
tin[x] = ++T;
jp2[x][0] = y;
for(int i=1;i<20;i++) jp2[x][i] = jp2[jp2[x][i-1]][i-1];
for(int z:G[x]) if(z!=y) dfs(z, x);
tout[x] = ++T;
}
bool is_ancestor(int x, int y) {
return tin[x]<=tin[y]&&tout[x]>=tout[y];
}
int lca(int x, int y) {
if(is_ancestor(x, y)) return x;
if(is_ancestor(y, x)) return y;
for(int i=19;i>=0;i--) {
if(!is_ancestor(jp2[x][i], y)) {
x = jp2[x][i];
}
}
return jp2[x][0];
}
bool is_on_path(int x, int a, int b) {
return is_ancestor(a, x)&&is_ancestor(x, b);
}
bool f(int x, int a, int b) {
int y = lca(a, b);
//cerr << "lca: " << a << " " << b << " " << y << "\n";
//cerr << "checking: " << x << " " << a << " " << b << " " << (is_on_path(x, y, a)||is_on_path(x, y, b)) << "\n";
return is_on_path(x, y, a)||is_on_path(x, y, b);
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int q=1;
cin >> q;
while(q--) {
int n, m;
cin >> n;
for(int i=1;i<=n;i++) G[i].clear();
for(int i=0;i<n-1;i++) {
int a, b;
cin >> a >> b;
G[a].pb(b);
G[b].pb(a);
}
tin[0] = T = 0;
dfs(1, 0);
tout[0] = T;
cin >> m;
for(int i=0;i<m;i++) {
G2[i].clear();
deg[i] = 0;
}
for(int i=0;i<m;i++) {
cin >> tab[i].st >> tab[i].nd;
}
for(int i=0;i<m;i++) {
for(int j=0;j<m;j++) {
if(i==j) continue;
if(f(tab[j].st, tab[i].st, tab[i].nd)) {
//cerr << "adding: " << i << " " << j << "\n";
G2[i].pb(j);
deg[j]++;
}
if(f(tab[j].nd, tab[i].st, tab[i].nd)) {
//cerr << "adding: " << j << " " << i << "\n";
G2[j].pb(i);
deg[i]++;
}
}
}
int cnt = 0;
queue<int> Q;
for(int i=0;i<m;i++) {
if(deg[i]==0) {
Q.push(i);
}
}
while(sz(Q)) {
cnt++;
int x = Q.front();
Q.pop();
for(int y:G2[x]) {
deg[y]--;
if(deg[y]==0) Q.push(y);
}
}
if(cnt==m) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
2 ms |
5972 KB |
Output is correct |
4 |
Correct |
11 ms |
5972 KB |
Output is correct |
5 |
Correct |
21 ms |
6024 KB |
Output is correct |
6 |
Correct |
3 ms |
5972 KB |
Output is correct |
7 |
Correct |
3 ms |
5972 KB |
Output is correct |
8 |
Correct |
5 ms |
6228 KB |
Output is correct |
9 |
Correct |
67 ms |
11260 KB |
Output is correct |
10 |
Correct |
51 ms |
38604 KB |
Output is correct |
11 |
Correct |
7 ms |
5972 KB |
Output is correct |
12 |
Correct |
52 ms |
6168 KB |
Output is correct |
13 |
Execution timed out |
5062 ms |
53608 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5892 KB |
Output is correct |
3 |
Correct |
3 ms |
5972 KB |
Output is correct |
4 |
Correct |
4 ms |
5972 KB |
Output is correct |
5 |
Correct |
3 ms |
5972 KB |
Output is correct |
6 |
Correct |
4 ms |
5972 KB |
Output is correct |
7 |
Correct |
3 ms |
5972 KB |
Output is correct |
8 |
Correct |
4 ms |
5972 KB |
Output is correct |
9 |
Correct |
4 ms |
5972 KB |
Output is correct |
10 |
Correct |
3 ms |
5972 KB |
Output is correct |
11 |
Correct |
3 ms |
5972 KB |
Output is correct |
12 |
Correct |
4 ms |
5972 KB |
Output is correct |
13 |
Correct |
3 ms |
5972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5892 KB |
Output is correct |
3 |
Correct |
3 ms |
5972 KB |
Output is correct |
4 |
Correct |
4 ms |
5972 KB |
Output is correct |
5 |
Correct |
3 ms |
5972 KB |
Output is correct |
6 |
Correct |
4 ms |
5972 KB |
Output is correct |
7 |
Correct |
3 ms |
5972 KB |
Output is correct |
8 |
Correct |
4 ms |
5972 KB |
Output is correct |
9 |
Correct |
4 ms |
5972 KB |
Output is correct |
10 |
Correct |
3 ms |
5972 KB |
Output is correct |
11 |
Correct |
3 ms |
5972 KB |
Output is correct |
12 |
Correct |
4 ms |
5972 KB |
Output is correct |
13 |
Correct |
3 ms |
5972 KB |
Output is correct |
14 |
Correct |
3 ms |
5972 KB |
Output is correct |
15 |
Correct |
3 ms |
5972 KB |
Output is correct |
16 |
Correct |
4 ms |
6048 KB |
Output is correct |
17 |
Correct |
4 ms |
5972 KB |
Output is correct |
18 |
Correct |
4 ms |
5972 KB |
Output is correct |
19 |
Correct |
3 ms |
5972 KB |
Output is correct |
20 |
Correct |
4 ms |
5972 KB |
Output is correct |
21 |
Correct |
3 ms |
5972 KB |
Output is correct |
22 |
Correct |
3 ms |
5972 KB |
Output is correct |
23 |
Correct |
3 ms |
5972 KB |
Output is correct |
24 |
Correct |
3 ms |
5972 KB |
Output is correct |
25 |
Correct |
4 ms |
5972 KB |
Output is correct |
26 |
Correct |
3 ms |
5972 KB |
Output is correct |
27 |
Correct |
4 ms |
5972 KB |
Output is correct |
28 |
Correct |
3 ms |
5972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5892 KB |
Output is correct |
3 |
Correct |
3 ms |
5972 KB |
Output is correct |
4 |
Correct |
4 ms |
5972 KB |
Output is correct |
5 |
Correct |
3 ms |
5972 KB |
Output is correct |
6 |
Correct |
4 ms |
5972 KB |
Output is correct |
7 |
Correct |
3 ms |
5972 KB |
Output is correct |
8 |
Correct |
4 ms |
5972 KB |
Output is correct |
9 |
Correct |
4 ms |
5972 KB |
Output is correct |
10 |
Correct |
3 ms |
5972 KB |
Output is correct |
11 |
Correct |
3 ms |
5972 KB |
Output is correct |
12 |
Correct |
4 ms |
5972 KB |
Output is correct |
13 |
Correct |
3 ms |
5972 KB |
Output is correct |
14 |
Correct |
3 ms |
5972 KB |
Output is correct |
15 |
Correct |
3 ms |
5972 KB |
Output is correct |
16 |
Correct |
4 ms |
6048 KB |
Output is correct |
17 |
Correct |
4 ms |
5972 KB |
Output is correct |
18 |
Correct |
4 ms |
5972 KB |
Output is correct |
19 |
Correct |
3 ms |
5972 KB |
Output is correct |
20 |
Correct |
4 ms |
5972 KB |
Output is correct |
21 |
Correct |
3 ms |
5972 KB |
Output is correct |
22 |
Correct |
3 ms |
5972 KB |
Output is correct |
23 |
Correct |
3 ms |
5972 KB |
Output is correct |
24 |
Correct |
3 ms |
5972 KB |
Output is correct |
25 |
Correct |
4 ms |
5972 KB |
Output is correct |
26 |
Correct |
3 ms |
5972 KB |
Output is correct |
27 |
Correct |
4 ms |
5972 KB |
Output is correct |
28 |
Correct |
3 ms |
5972 KB |
Output is correct |
29 |
Correct |
6 ms |
6228 KB |
Output is correct |
30 |
Correct |
16 ms |
6068 KB |
Output is correct |
31 |
Correct |
10 ms |
6100 KB |
Output is correct |
32 |
Correct |
10 ms |
5972 KB |
Output is correct |
33 |
Correct |
4 ms |
5972 KB |
Output is correct |
34 |
Correct |
23 ms |
5972 KB |
Output is correct |
35 |
Correct |
28 ms |
6148 KB |
Output is correct |
36 |
Correct |
22 ms |
6024 KB |
Output is correct |
37 |
Correct |
13 ms |
5972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5892 KB |
Output is correct |
3 |
Correct |
3 ms |
5972 KB |
Output is correct |
4 |
Correct |
4 ms |
5972 KB |
Output is correct |
5 |
Correct |
3 ms |
5972 KB |
Output is correct |
6 |
Correct |
4 ms |
5972 KB |
Output is correct |
7 |
Correct |
3 ms |
5972 KB |
Output is correct |
8 |
Correct |
4 ms |
5972 KB |
Output is correct |
9 |
Correct |
4 ms |
5972 KB |
Output is correct |
10 |
Correct |
3 ms |
5972 KB |
Output is correct |
11 |
Correct |
3 ms |
5972 KB |
Output is correct |
12 |
Correct |
4 ms |
5972 KB |
Output is correct |
13 |
Correct |
3 ms |
5972 KB |
Output is correct |
14 |
Correct |
3 ms |
5972 KB |
Output is correct |
15 |
Correct |
3 ms |
5972 KB |
Output is correct |
16 |
Correct |
4 ms |
6048 KB |
Output is correct |
17 |
Correct |
4 ms |
5972 KB |
Output is correct |
18 |
Correct |
4 ms |
5972 KB |
Output is correct |
19 |
Correct |
3 ms |
5972 KB |
Output is correct |
20 |
Correct |
4 ms |
5972 KB |
Output is correct |
21 |
Correct |
3 ms |
5972 KB |
Output is correct |
22 |
Correct |
3 ms |
5972 KB |
Output is correct |
23 |
Correct |
3 ms |
5972 KB |
Output is correct |
24 |
Correct |
3 ms |
5972 KB |
Output is correct |
25 |
Correct |
4 ms |
5972 KB |
Output is correct |
26 |
Correct |
3 ms |
5972 KB |
Output is correct |
27 |
Correct |
4 ms |
5972 KB |
Output is correct |
28 |
Correct |
3 ms |
5972 KB |
Output is correct |
29 |
Correct |
6 ms |
6228 KB |
Output is correct |
30 |
Correct |
16 ms |
6068 KB |
Output is correct |
31 |
Correct |
10 ms |
6100 KB |
Output is correct |
32 |
Correct |
10 ms |
5972 KB |
Output is correct |
33 |
Correct |
4 ms |
5972 KB |
Output is correct |
34 |
Correct |
23 ms |
5972 KB |
Output is correct |
35 |
Correct |
28 ms |
6148 KB |
Output is correct |
36 |
Correct |
22 ms |
6024 KB |
Output is correct |
37 |
Correct |
13 ms |
5972 KB |
Output is correct |
38 |
Correct |
70 ms |
11292 KB |
Output is correct |
39 |
Correct |
58 ms |
38664 KB |
Output is correct |
40 |
Correct |
497 ms |
9560 KB |
Output is correct |
41 |
Correct |
570 ms |
7552 KB |
Output is correct |
42 |
Correct |
518 ms |
9784 KB |
Output is correct |
43 |
Correct |
29 ms |
7476 KB |
Output is correct |
44 |
Correct |
485 ms |
6696 KB |
Output is correct |
45 |
Correct |
108 ms |
30924 KB |
Output is correct |
46 |
Correct |
103 ms |
30980 KB |
Output is correct |
47 |
Correct |
83 ms |
33688 KB |
Output is correct |
48 |
Correct |
80 ms |
33708 KB |
Output is correct |
49 |
Correct |
89 ms |
31076 KB |
Output is correct |
50 |
Correct |
100 ms |
30988 KB |
Output is correct |
51 |
Correct |
51 ms |
31848 KB |
Output is correct |
52 |
Correct |
53 ms |
31856 KB |
Output is correct |
53 |
Correct |
461 ms |
7860 KB |
Output is correct |
54 |
Correct |
110 ms |
31648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5972 KB |
Output is correct |
2 |
Correct |
2 ms |
5972 KB |
Output is correct |
3 |
Correct |
3 ms |
5972 KB |
Output is correct |
4 |
Correct |
2 ms |
5972 KB |
Output is correct |
5 |
Correct |
9 ms |
5972 KB |
Output is correct |
6 |
Correct |
3 ms |
5936 KB |
Output is correct |
7 |
Correct |
3 ms |
5972 KB |
Output is correct |
8 |
Correct |
3 ms |
5972 KB |
Output is correct |
9 |
Correct |
3 ms |
5972 KB |
Output is correct |
10 |
Correct |
3 ms |
5972 KB |
Output is correct |
11 |
Correct |
3 ms |
5872 KB |
Output is correct |
12 |
Correct |
25 ms |
6048 KB |
Output is correct |
13 |
Correct |
215 ms |
6152 KB |
Output is correct |
14 |
Correct |
111 ms |
6020 KB |
Output is correct |
15 |
Correct |
174 ms |
6048 KB |
Output is correct |
16 |
Execution timed out |
5088 ms |
31744 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
2 ms |
5972 KB |
Output is correct |
4 |
Correct |
11 ms |
5972 KB |
Output is correct |
5 |
Correct |
21 ms |
6024 KB |
Output is correct |
6 |
Correct |
3 ms |
5972 KB |
Output is correct |
7 |
Correct |
3 ms |
5972 KB |
Output is correct |
8 |
Correct |
5 ms |
6228 KB |
Output is correct |
9 |
Correct |
67 ms |
11260 KB |
Output is correct |
10 |
Correct |
51 ms |
38604 KB |
Output is correct |
11 |
Correct |
7 ms |
5972 KB |
Output is correct |
12 |
Correct |
52 ms |
6168 KB |
Output is correct |
13 |
Execution timed out |
5062 ms |
53608 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |