#include<bits/stdc++.h>
using namespace std;
bool f; int T;
void dfs(int n, vector<int> g[], int p, int d[], int pa[]){
pa[n] = p;
d[n] = d[p]+1;
for(int c : g[n]){
if(c == p) continue;
dfs(c, g, n, d, pa);
}
}
struct node {
vector<node*> l;
bool v = false, c = false;
};
bool ans;
void dfs(node* n){
n->v = n->c = true;
for(node* c : n->l){
if(!ans) break;
if(c->c){
ans = false;
break;
}
if(!c->v) dfs(c);
}
n->c = false;
}
void solve(){
int n,m;cin >> n;
vector<int> g[n+1];
for(int i = 1;i < n;i++){
int u, v;cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
int d[n+1], p[n+1];d[0] = 0;
dfs(1, g, 0, d, p);
cin >> m;
int s[m], t[m];
int sn[n+1], tn[n+1];
fill(sn+1, sn+n+1, -1);fill(tn+1, tn+n+1, -1);
for(int i = 0;i < m;i++){
cin >> s[i] >> t[i];
sn[s[i]] = i;
tn[t[i]] = i;
}
node mg[m];
for(int i = 0;i < m;i++){
vector<int> path;
if(d[s[i]] < d[t[i]]) swap(s[i], t[i]);
while(d[s[i]] > d[t[i]]){
path.push_back(s[i]);
s[i] = p[s[i]];
}
while(true){
path.push_back(s[i]);
if(s[i] == t[i]) break;
path.push_back(t[i]);
s[i] = p[s[i]];t[i] = p[t[i]];
}
for(int _ : path){
if(sn[_] != -1 && sn[_] != i) mg[i].l.push_back(&mg[sn[_]]);
if(tn[_] != -1 && tn[_] != i) mg[tn[_]].l.push_back(&mg[i]);
}
}
ans = true;
for(int i = 0;i < m && ans;i++){
if(!mg[i].v) dfs(&mg[i]);
}
if(ans)cout << "Yes\n";
else cout << "No\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;cin >> t;
while(t--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
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 |
8 ms |
348 KB |
Output is correct |
5 |
Correct |
18 ms |
684 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
604 KB |
Output is correct |
9 |
Correct |
109 ms |
3968 KB |
Output is correct |
10 |
Correct |
477 ms |
20992 KB |
Output is correct |
11 |
Correct |
6 ms |
344 KB |
Output is correct |
12 |
Correct |
45 ms |
592 KB |
Output is correct |
13 |
Correct |
140 ms |
75848 KB |
Output is correct |
14 |
Correct |
166 ms |
105880 KB |
Output is correct |
15 |
Runtime error |
3371 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 |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
472 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
472 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
472 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
3 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
420 KB |
Output is correct |
31 |
Correct |
2 ms |
348 KB |
Output is correct |
32 |
Correct |
2 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
2 ms |
344 KB |
Output is correct |
35 |
Correct |
3 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
472 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
3 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
420 KB |
Output is correct |
31 |
Correct |
2 ms |
348 KB |
Output is correct |
32 |
Correct |
2 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
2 ms |
344 KB |
Output is correct |
35 |
Correct |
3 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
117 ms |
4308 KB |
Output is correct |
39 |
Correct |
474 ms |
21072 KB |
Output is correct |
40 |
Correct |
57 ms |
2060 KB |
Output is correct |
41 |
Correct |
24 ms |
1116 KB |
Output is correct |
42 |
Correct |
49 ms |
2396 KB |
Output is correct |
43 |
Correct |
19 ms |
860 KB |
Output is correct |
44 |
Correct |
8 ms |
648 KB |
Output is correct |
45 |
Correct |
30 ms |
8796 KB |
Output is correct |
46 |
Correct |
30 ms |
8840 KB |
Output is correct |
47 |
Correct |
90 ms |
13084 KB |
Output is correct |
48 |
Correct |
99 ms |
13348 KB |
Output is correct |
49 |
Correct |
33 ms |
8788 KB |
Output is correct |
50 |
Correct |
34 ms |
8984 KB |
Output is correct |
51 |
Correct |
27 ms |
9476 KB |
Output is correct |
52 |
Correct |
27 ms |
9564 KB |
Output is correct |
53 |
Correct |
7 ms |
860 KB |
Output is correct |
54 |
Correct |
32 ms |
8784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
5 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 |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
600 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
20 ms |
512 KB |
Output is correct |
14 |
Correct |
26 ms |
464 KB |
Output is correct |
15 |
Correct |
24 ms |
348 KB |
Output is correct |
16 |
Correct |
35 ms |
9820 KB |
Output is correct |
17 |
Correct |
84 ms |
22888 KB |
Output is correct |
18 |
Correct |
211 ms |
62156 KB |
Output is correct |
19 |
Correct |
39 ms |
11628 KB |
Output is correct |
20 |
Correct |
38 ms |
11604 KB |
Output is correct |
21 |
Correct |
41 ms |
11632 KB |
Output is correct |
22 |
Correct |
85 ms |
24136 KB |
Output is correct |
23 |
Correct |
65 ms |
24896 KB |
Output is correct |
24 |
Correct |
69 ms |
24524 KB |
Output is correct |
25 |
Correct |
69 ms |
24772 KB |
Output is correct |
26 |
Correct |
68 ms |
24696 KB |
Output is correct |
27 |
Correct |
74 ms |
23240 KB |
Output is correct |
28 |
Correct |
73 ms |
24212 KB |
Output is correct |
29 |
Correct |
68 ms |
22220 KB |
Output is correct |
30 |
Correct |
54 ms |
16848 KB |
Output is correct |
31 |
Correct |
54 ms |
17152 KB |
Output is correct |
32 |
Correct |
49 ms |
16848 KB |
Output is correct |
33 |
Correct |
67 ms |
17128 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 |
8 ms |
348 KB |
Output is correct |
5 |
Correct |
18 ms |
684 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
604 KB |
Output is correct |
9 |
Correct |
109 ms |
3968 KB |
Output is correct |
10 |
Correct |
477 ms |
20992 KB |
Output is correct |
11 |
Correct |
6 ms |
344 KB |
Output is correct |
12 |
Correct |
45 ms |
592 KB |
Output is correct |
13 |
Correct |
140 ms |
75848 KB |
Output is correct |
14 |
Correct |
166 ms |
105880 KB |
Output is correct |
15 |
Runtime error |
3371 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |