#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(){
bool s1 = true;
int n,m;cin >> n;
vector<int> g[n+1];
for(int i = 1;i < n;i++){
int u, v;cin >> u >> v;
s1 &= (u == i) && (v == i+1);
g[u].push_back(v);
g[v].push_back(u);
}
if(s1){
cin >> m;
set<int> fore, bace;
pair<int, int> ra[m];
bool ok = true;
for(int i = 0;i < m;i++){
cin >> ra[i].first >> ra[i].second;
if(ra[i].first < ra[i].second) {
fore.insert(ra[i].second);
fore.insert(ra[i].first);
auto p = bace.lower_bound(ra[i].first);
if(p != bace.end() && *p <= ra[i].second) ok = false;
}
else {
bace.insert(ra[i].second);
bace.insert(ra[i].first);
swap(ra[i].first, ra[i].second);
auto p = fore.lower_bound(ra[i].first);
if(p != fore.end() && *p <= ra[i].second) ok = false;
}
}
if(!ok){
cout << "No\n";
return;
}
sort(ra, ra+m, [](const auto& a, const auto& b){
if(a.first != b.first) return a.first < b.first;
return a.second > b.second;
});
int R = -1;
for(auto p : ra){
if(R >= p.second){
cout << "No\n";
return;
}
R = max(R, p.second);
}
cout << "Yes\n";
return;
}
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 |
1 ms |
504 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 |
604 KB |
Output is correct |
5 |
Correct |
17 ms |
1208 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
356 KB |
Output is correct |
8 |
Correct |
3 ms |
348 KB |
Output is correct |
9 |
Correct |
22 ms |
1880 KB |
Output is correct |
10 |
Correct |
26 ms |
8280 KB |
Output is correct |
11 |
Correct |
5 ms |
612 KB |
Output is correct |
12 |
Correct |
25 ms |
1428 KB |
Output is correct |
13 |
Correct |
47 ms |
13980 KB |
Output is correct |
14 |
Correct |
49 ms |
13904 KB |
Output is correct |
15 |
Correct |
57 ms |
13768 KB |
Output is correct |
16 |
Correct |
130 ms |
20184 KB |
Output is correct |
17 |
Correct |
57 ms |
15188 KB |
Output is correct |
18 |
Correct |
72 ms |
16320 KB |
Output is correct |
19 |
Correct |
50 ms |
15188 KB |
Output is correct |
20 |
Correct |
49 ms |
15180 KB |
Output is correct |
21 |
Correct |
52 ms |
15184 KB |
Output is correct |
22 |
Correct |
57 ms |
15252 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 |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
344 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 |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 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 |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
456 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
408 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
352 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 |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 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 |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
456 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
408 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
352 KB |
Output is correct |
29 |
Correct |
2 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
356 KB |
Output is correct |
32 |
Correct |
2 ms |
360 KB |
Output is correct |
33 |
Correct |
2 ms |
356 KB |
Output is correct |
34 |
Correct |
2 ms |
356 KB |
Output is correct |
35 |
Correct |
3 ms |
612 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
2 ms |
860 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 |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 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 |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
456 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
408 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
352 KB |
Output is correct |
29 |
Correct |
2 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
356 KB |
Output is correct |
32 |
Correct |
2 ms |
360 KB |
Output is correct |
33 |
Correct |
2 ms |
356 KB |
Output is correct |
34 |
Correct |
2 ms |
356 KB |
Output is correct |
35 |
Correct |
3 ms |
612 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
2 ms |
860 KB |
Output is correct |
38 |
Correct |
22 ms |
1848 KB |
Output is correct |
39 |
Correct |
25 ms |
8296 KB |
Output is correct |
40 |
Correct |
57 ms |
3184 KB |
Output is correct |
41 |
Correct |
27 ms |
1880 KB |
Output is correct |
42 |
Correct |
54 ms |
2880 KB |
Output is correct |
43 |
Correct |
21 ms |
2140 KB |
Output is correct |
44 |
Correct |
9 ms |
864 KB |
Output is correct |
45 |
Correct |
31 ms |
10208 KB |
Output is correct |
46 |
Correct |
31 ms |
10336 KB |
Output is correct |
47 |
Correct |
97 ms |
14564 KB |
Output is correct |
48 |
Correct |
100 ms |
14704 KB |
Output is correct |
49 |
Correct |
36 ms |
10320 KB |
Output is correct |
50 |
Correct |
47 ms |
10320 KB |
Output is correct |
51 |
Correct |
36 ms |
10812 KB |
Output is correct |
52 |
Correct |
29 ms |
10912 KB |
Output is correct |
53 |
Correct |
9 ms |
1116 KB |
Output is correct |
54 |
Correct |
45 ms |
10500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
5 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
21 ms |
836 KB |
Output is correct |
14 |
Correct |
27 ms |
1116 KB |
Output is correct |
15 |
Correct |
25 ms |
976 KB |
Output is correct |
16 |
Correct |
42 ms |
11088 KB |
Output is correct |
17 |
Correct |
86 ms |
22744 KB |
Output is correct |
18 |
Correct |
208 ms |
61932 KB |
Output is correct |
19 |
Correct |
39 ms |
11600 KB |
Output is correct |
20 |
Correct |
40 ms |
11600 KB |
Output is correct |
21 |
Correct |
43 ms |
11604 KB |
Output is correct |
22 |
Correct |
89 ms |
24264 KB |
Output is correct |
23 |
Correct |
70 ms |
24776 KB |
Output is correct |
24 |
Correct |
69 ms |
24524 KB |
Output is correct |
25 |
Correct |
67 ms |
24768 KB |
Output is correct |
26 |
Correct |
74 ms |
24536 KB |
Output is correct |
27 |
Correct |
75 ms |
23256 KB |
Output is correct |
28 |
Correct |
90 ms |
24264 KB |
Output is correct |
29 |
Correct |
76 ms |
22212 KB |
Output is correct |
30 |
Correct |
50 ms |
16820 KB |
Output is correct |
31 |
Correct |
55 ms |
17360 KB |
Output is correct |
32 |
Correct |
52 ms |
16848 KB |
Output is correct |
33 |
Correct |
52 ms |
17128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
504 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 |
604 KB |
Output is correct |
5 |
Correct |
17 ms |
1208 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
356 KB |
Output is correct |
8 |
Correct |
3 ms |
348 KB |
Output is correct |
9 |
Correct |
22 ms |
1880 KB |
Output is correct |
10 |
Correct |
26 ms |
8280 KB |
Output is correct |
11 |
Correct |
5 ms |
612 KB |
Output is correct |
12 |
Correct |
25 ms |
1428 KB |
Output is correct |
13 |
Correct |
47 ms |
13980 KB |
Output is correct |
14 |
Correct |
49 ms |
13904 KB |
Output is correct |
15 |
Correct |
57 ms |
13768 KB |
Output is correct |
16 |
Correct |
130 ms |
20184 KB |
Output is correct |
17 |
Correct |
57 ms |
15188 KB |
Output is correct |
18 |
Correct |
72 ms |
16320 KB |
Output is correct |
19 |
Correct |
50 ms |
15188 KB |
Output is correct |
20 |
Correct |
49 ms |
15180 KB |
Output is correct |
21 |
Correct |
52 ms |
15184 KB |
Output is correct |
22 |
Correct |
57 ms |
15252 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 |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
2 ms |
724 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
456 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
2 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
344 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
408 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
1 ms |
352 KB |
Output is correct |
51 |
Correct |
2 ms |
348 KB |
Output is correct |
52 |
Correct |
2 ms |
348 KB |
Output is correct |
53 |
Correct |
2 ms |
356 KB |
Output is correct |
54 |
Correct |
2 ms |
360 KB |
Output is correct |
55 |
Correct |
2 ms |
356 KB |
Output is correct |
56 |
Correct |
2 ms |
356 KB |
Output is correct |
57 |
Correct |
3 ms |
612 KB |
Output is correct |
58 |
Correct |
1 ms |
348 KB |
Output is correct |
59 |
Correct |
2 ms |
860 KB |
Output is correct |
60 |
Correct |
22 ms |
1848 KB |
Output is correct |
61 |
Correct |
25 ms |
8296 KB |
Output is correct |
62 |
Correct |
57 ms |
3184 KB |
Output is correct |
63 |
Correct |
27 ms |
1880 KB |
Output is correct |
64 |
Correct |
54 ms |
2880 KB |
Output is correct |
65 |
Correct |
21 ms |
2140 KB |
Output is correct |
66 |
Correct |
9 ms |
864 KB |
Output is correct |
67 |
Correct |
31 ms |
10208 KB |
Output is correct |
68 |
Correct |
31 ms |
10336 KB |
Output is correct |
69 |
Correct |
97 ms |
14564 KB |
Output is correct |
70 |
Correct |
100 ms |
14704 KB |
Output is correct |
71 |
Correct |
36 ms |
10320 KB |
Output is correct |
72 |
Correct |
47 ms |
10320 KB |
Output is correct |
73 |
Correct |
36 ms |
10812 KB |
Output is correct |
74 |
Correct |
29 ms |
10912 KB |
Output is correct |
75 |
Correct |
9 ms |
1116 KB |
Output is correct |
76 |
Correct |
45 ms |
10500 KB |
Output is correct |
77 |
Correct |
1 ms |
348 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
1 ms |
348 KB |
Output is correct |
80 |
Correct |
0 ms |
348 KB |
Output is correct |
81 |
Correct |
5 ms |
604 KB |
Output is correct |
82 |
Correct |
1 ms |
348 KB |
Output is correct |
83 |
Correct |
1 ms |
348 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
0 ms |
348 KB |
Output is correct |
86 |
Correct |
0 ms |
348 KB |
Output is correct |
87 |
Correct |
1 ms |
348 KB |
Output is correct |
88 |
Correct |
1 ms |
468 KB |
Output is correct |
89 |
Correct |
21 ms |
836 KB |
Output is correct |
90 |
Correct |
27 ms |
1116 KB |
Output is correct |
91 |
Correct |
25 ms |
976 KB |
Output is correct |
92 |
Correct |
42 ms |
11088 KB |
Output is correct |
93 |
Correct |
86 ms |
22744 KB |
Output is correct |
94 |
Correct |
208 ms |
61932 KB |
Output is correct |
95 |
Correct |
39 ms |
11600 KB |
Output is correct |
96 |
Correct |
40 ms |
11600 KB |
Output is correct |
97 |
Correct |
43 ms |
11604 KB |
Output is correct |
98 |
Correct |
89 ms |
24264 KB |
Output is correct |
99 |
Correct |
70 ms |
24776 KB |
Output is correct |
100 |
Correct |
69 ms |
24524 KB |
Output is correct |
101 |
Correct |
67 ms |
24768 KB |
Output is correct |
102 |
Correct |
74 ms |
24536 KB |
Output is correct |
103 |
Correct |
75 ms |
23256 KB |
Output is correct |
104 |
Correct |
90 ms |
24264 KB |
Output is correct |
105 |
Correct |
76 ms |
22212 KB |
Output is correct |
106 |
Correct |
50 ms |
16820 KB |
Output is correct |
107 |
Correct |
55 ms |
17360 KB |
Output is correct |
108 |
Correct |
52 ms |
16848 KB |
Output is correct |
109 |
Correct |
52 ms |
17128 KB |
Output is correct |
110 |
Correct |
33 ms |
1360 KB |
Output is correct |
111 |
Correct |
20 ms |
1112 KB |
Output is correct |
112 |
Runtime error |
3109 ms |
1048576 KB |
Execution killed with signal 9 |
113 |
Halted |
0 ms |
0 KB |
- |