#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 |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
9 ms |
460 KB |
Output is correct |
5 |
Correct |
17 ms |
344 KB |
Output is correct |
6 |
Correct |
2 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
23 ms |
816 KB |
Output is correct |
10 |
Correct |
25 ms |
7004 KB |
Output is correct |
11 |
Correct |
4 ms |
348 KB |
Output is correct |
12 |
Correct |
26 ms |
348 KB |
Output is correct |
13 |
Correct |
47 ms |
12016 KB |
Output is correct |
14 |
Correct |
46 ms |
11856 KB |
Output is correct |
15 |
Correct |
59 ms |
13600 KB |
Output is correct |
16 |
Correct |
116 ms |
20024 KB |
Output is correct |
17 |
Correct |
49 ms |
15208 KB |
Output is correct |
18 |
Correct |
70 ms |
16372 KB |
Output is correct |
19 |
Correct |
58 ms |
15260 KB |
Output is correct |
20 |
Correct |
49 ms |
15028 KB |
Output is correct |
21 |
Correct |
51 ms |
15184 KB |
Output is correct |
22 |
Correct |
51 ms |
15128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
544 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
544 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
14 |
Correct |
1 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 |
2 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 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 |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
544 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
14 |
Correct |
1 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 |
2 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 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 |
2 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
344 KB |
Output is correct |
32 |
Correct |
2 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
2 ms |
348 KB |
Output is correct |
35 |
Correct |
4 ms |
348 KB |
Output is correct |
36 |
Correct |
2 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
544 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
14 |
Correct |
1 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 |
2 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 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 |
2 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
344 KB |
Output is correct |
32 |
Correct |
2 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
2 ms |
348 KB |
Output is correct |
35 |
Correct |
4 ms |
348 KB |
Output is correct |
36 |
Correct |
2 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
344 KB |
Output is correct |
38 |
Correct |
24 ms |
608 KB |
Output is correct |
39 |
Correct |
25 ms |
6908 KB |
Output is correct |
40 |
Correct |
58 ms |
1992 KB |
Output is correct |
41 |
Correct |
25 ms |
1112 KB |
Output is correct |
42 |
Correct |
62 ms |
2388 KB |
Output is correct |
43 |
Correct |
21 ms |
860 KB |
Output is correct |
44 |
Correct |
8 ms |
604 KB |
Output is correct |
45 |
Correct |
32 ms |
8884 KB |
Output is correct |
46 |
Correct |
32 ms |
8788 KB |
Output is correct |
47 |
Correct |
92 ms |
13120 KB |
Output is correct |
48 |
Correct |
102 ms |
13108 KB |
Output is correct |
49 |
Correct |
35 ms |
8988 KB |
Output is correct |
50 |
Correct |
41 ms |
8836 KB |
Output is correct |
51 |
Correct |
35 ms |
9544 KB |
Output is correct |
52 |
Correct |
29 ms |
9508 KB |
Output is correct |
53 |
Correct |
8 ms |
1004 KB |
Output is correct |
54 |
Correct |
33 ms |
8692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
544 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
20 ms |
460 KB |
Output is correct |
14 |
Correct |
28 ms |
348 KB |
Output is correct |
15 |
Correct |
24 ms |
348 KB |
Output is correct |
16 |
Correct |
36 ms |
9820 KB |
Output is correct |
17 |
Correct |
84 ms |
20828 KB |
Output is correct |
18 |
Correct |
198 ms |
59056 KB |
Output is correct |
19 |
Correct |
40 ms |
9860 KB |
Output is correct |
20 |
Correct |
45 ms |
10188 KB |
Output is correct |
21 |
Correct |
44 ms |
9916 KB |
Output is correct |
22 |
Correct |
89 ms |
22252 KB |
Output is correct |
23 |
Correct |
73 ms |
22620 KB |
Output is correct |
24 |
Correct |
99 ms |
22356 KB |
Output is correct |
25 |
Correct |
73 ms |
22716 KB |
Output is correct |
26 |
Correct |
72 ms |
22592 KB |
Output is correct |
27 |
Correct |
72 ms |
20548 KB |
Output is correct |
28 |
Correct |
145 ms |
21352 KB |
Output is correct |
29 |
Correct |
71 ms |
19404 KB |
Output is correct |
30 |
Correct |
78 ms |
14796 KB |
Output is correct |
31 |
Correct |
60 ms |
15052 KB |
Output is correct |
32 |
Correct |
73 ms |
14724 KB |
Output is correct |
33 |
Correct |
68 ms |
15184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
9 ms |
460 KB |
Output is correct |
5 |
Correct |
17 ms |
344 KB |
Output is correct |
6 |
Correct |
2 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
23 ms |
816 KB |
Output is correct |
10 |
Correct |
25 ms |
7004 KB |
Output is correct |
11 |
Correct |
4 ms |
348 KB |
Output is correct |
12 |
Correct |
26 ms |
348 KB |
Output is correct |
13 |
Correct |
47 ms |
12016 KB |
Output is correct |
14 |
Correct |
46 ms |
11856 KB |
Output is correct |
15 |
Correct |
59 ms |
13600 KB |
Output is correct |
16 |
Correct |
116 ms |
20024 KB |
Output is correct |
17 |
Correct |
49 ms |
15208 KB |
Output is correct |
18 |
Correct |
70 ms |
16372 KB |
Output is correct |
19 |
Correct |
58 ms |
15260 KB |
Output is correct |
20 |
Correct |
49 ms |
15028 KB |
Output is correct |
21 |
Correct |
51 ms |
15184 KB |
Output is correct |
22 |
Correct |
51 ms |
15128 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
544 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
348 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 |
344 KB |
Output is correct |
40 |
Correct |
2 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
344 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
54 |
Correct |
2 ms |
348 KB |
Output is correct |
55 |
Correct |
1 ms |
348 KB |
Output is correct |
56 |
Correct |
2 ms |
348 KB |
Output is correct |
57 |
Correct |
4 ms |
348 KB |
Output is correct |
58 |
Correct |
2 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
344 KB |
Output is correct |
60 |
Correct |
24 ms |
608 KB |
Output is correct |
61 |
Correct |
25 ms |
6908 KB |
Output is correct |
62 |
Correct |
58 ms |
1992 KB |
Output is correct |
63 |
Correct |
25 ms |
1112 KB |
Output is correct |
64 |
Correct |
62 ms |
2388 KB |
Output is correct |
65 |
Correct |
21 ms |
860 KB |
Output is correct |
66 |
Correct |
8 ms |
604 KB |
Output is correct |
67 |
Correct |
32 ms |
8884 KB |
Output is correct |
68 |
Correct |
32 ms |
8788 KB |
Output is correct |
69 |
Correct |
92 ms |
13120 KB |
Output is correct |
70 |
Correct |
102 ms |
13108 KB |
Output is correct |
71 |
Correct |
35 ms |
8988 KB |
Output is correct |
72 |
Correct |
41 ms |
8836 KB |
Output is correct |
73 |
Correct |
35 ms |
9544 KB |
Output is correct |
74 |
Correct |
29 ms |
9508 KB |
Output is correct |
75 |
Correct |
8 ms |
1004 KB |
Output is correct |
76 |
Correct |
33 ms |
8692 KB |
Output is correct |
77 |
Correct |
1 ms |
344 KB |
Output is correct |
78 |
Correct |
0 ms |
344 KB |
Output is correct |
79 |
Correct |
1 ms |
544 KB |
Output is correct |
80 |
Correct |
1 ms |
344 KB |
Output is correct |
81 |
Correct |
4 ms |
348 KB |
Output is correct |
82 |
Correct |
1 ms |
348 KB |
Output is correct |
83 |
Correct |
1 ms |
344 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
1 ms |
348 KB |
Output is correct |
86 |
Correct |
1 ms |
348 KB |
Output is correct |
87 |
Correct |
1 ms |
348 KB |
Output is correct |
88 |
Correct |
1 ms |
348 KB |
Output is correct |
89 |
Correct |
20 ms |
460 KB |
Output is correct |
90 |
Correct |
28 ms |
348 KB |
Output is correct |
91 |
Correct |
24 ms |
348 KB |
Output is correct |
92 |
Correct |
36 ms |
9820 KB |
Output is correct |
93 |
Correct |
84 ms |
20828 KB |
Output is correct |
94 |
Correct |
198 ms |
59056 KB |
Output is correct |
95 |
Correct |
40 ms |
9860 KB |
Output is correct |
96 |
Correct |
45 ms |
10188 KB |
Output is correct |
97 |
Correct |
44 ms |
9916 KB |
Output is correct |
98 |
Correct |
89 ms |
22252 KB |
Output is correct |
99 |
Correct |
73 ms |
22620 KB |
Output is correct |
100 |
Correct |
99 ms |
22356 KB |
Output is correct |
101 |
Correct |
73 ms |
22716 KB |
Output is correct |
102 |
Correct |
72 ms |
22592 KB |
Output is correct |
103 |
Correct |
72 ms |
20548 KB |
Output is correct |
104 |
Correct |
145 ms |
21352 KB |
Output is correct |
105 |
Correct |
71 ms |
19404 KB |
Output is correct |
106 |
Correct |
78 ms |
14796 KB |
Output is correct |
107 |
Correct |
60 ms |
15052 KB |
Output is correct |
108 |
Correct |
73 ms |
14724 KB |
Output is correct |
109 |
Correct |
68 ms |
15184 KB |
Output is correct |
110 |
Correct |
38 ms |
1436 KB |
Output is correct |
111 |
Correct |
21 ms |
1116 KB |
Output is correct |
112 |
Runtime error |
3250 ms |
1048576 KB |
Execution killed with signal 9 |
113 |
Halted |
0 ms |
0 KB |
- |