#include <bits/stdc++.h>
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define cd complex<double>
#define p_q priority_queue
using namespace std;
struct TOPO{
vector<vector<int> > e;
int n;
vector<int> in;
void init(int x){
e.resize(x);
in.resize(x);
n=x;
}
void add(int x,int y){
e[x].push_back(y);
in[y]++;
}
bool solve(){
int cnt=0;
queue<int> q;
for(int i=0;i<n;i++){
if(in[i]==0){
q.push(i);
}
}
while(q.size()){
int a=q.front();
q.pop();
cnt++;
for(auto h:e[a]){
in[h]--;
if(in[h]==0){
q.push(h);
}
}
}
return cnt==n;
}
};
struct no{
vector<int> ch;
int dep=-1;
int fa=-1;
vector<int> s,t;
};
vector<no> v;
void dfs(int r,int f){
v[r].dep=v[f].dep+1;
v[r].fa=f;
for(auto h:v[r].ch){
if(h!=f){
dfs(h,r);
}
}
}
int main(){
AquA;
int q;
cin >> q;
while(q--){
int n;
cin >> n;
v.clear();
v.resize(n);
for(int i=1;i<n;i++){
int a,b;
cin >> a >> b;
a--;
b--;
v[a].ch.push_back(b);
v[b].ch.push_back(a);
}
dfs(0,0);
int m;
cin >> m;
vector<vector<int> > f(m);
vector<pair<int,int> > t;
for(int i=0;i<m;i++){
int a,b;
cin >> a >> b;
a--;
b--;
v[a].s.push_back(i);
v[b].t.push_back(i);
vector<int> c,d;
while(v[a].dep>v[b].dep){
c.push_back(a);
a=v[a].fa;
}
while(v[b].dep>v[a].dep){
d.push_back(b);
b=v[b].fa;
}
if(a==b){
c.push_back(a);
for(auto h:c){
f[i].push_back(h);
}
reverse(d.begin(),d.end());
for(auto h:d){
f[i].push_back(h);
}
continue;
}
while(a!=b){
c.push_back(a);
d.push_back(b);
a=v[a].fa;
b=v[b].fa;
}
c.push_back(a);
for(auto h:c){
f[i].push_back(h);
}
reverse(d.begin(),d.end());
for(auto h:d){
f[i].push_back(h);
}
}
TOPO tp;
tp.init(m);
for(int i=0;i<m;i++){
auto h=f[i];
for(auto y:h){
for(auto z:v[y].s){
if(z!=i){
tp.add(z,i);
}
}
for(auto z:v[y].t){
if(z!=i){
tp.add(i,z);
}
}
}
}
if(tp.solve()){
cout << "Yes\n";
}
else{
cout << "No\n";
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
11 ms |
340 KB |
Output is correct |
5 |
Correct |
24 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
6 ms |
500 KB |
Output is correct |
9 |
Correct |
257 ms |
17004 KB |
Output is correct |
10 |
Correct |
1184 ms |
257060 KB |
Output is correct |
11 |
Correct |
8 ms |
344 KB |
Output is correct |
12 |
Correct |
84 ms |
392 KB |
Output is correct |
13 |
Correct |
284 ms |
91092 KB |
Output is correct |
14 |
Correct |
341 ms |
118840 KB |
Output is correct |
15 |
Runtime error |
3264 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
388 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
388 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
6 ms |
588 KB |
Output is correct |
30 |
Correct |
4 ms |
340 KB |
Output is correct |
31 |
Correct |
4 ms |
416 KB |
Output is correct |
32 |
Correct |
4 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
34 |
Correct |
4 ms |
340 KB |
Output is correct |
35 |
Correct |
6 ms |
444 KB |
Output is correct |
36 |
Correct |
3 ms |
340 KB |
Output is correct |
37 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
388 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
6 ms |
588 KB |
Output is correct |
30 |
Correct |
4 ms |
340 KB |
Output is correct |
31 |
Correct |
4 ms |
416 KB |
Output is correct |
32 |
Correct |
4 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
34 |
Correct |
4 ms |
340 KB |
Output is correct |
35 |
Correct |
6 ms |
444 KB |
Output is correct |
36 |
Correct |
3 ms |
340 KB |
Output is correct |
37 |
Correct |
2 ms |
340 KB |
Output is correct |
38 |
Correct |
248 ms |
17004 KB |
Output is correct |
39 |
Correct |
1234 ms |
257004 KB |
Output is correct |
40 |
Correct |
130 ms |
4116 KB |
Output is correct |
41 |
Correct |
38 ms |
1108 KB |
Output is correct |
42 |
Correct |
95 ms |
4924 KB |
Output is correct |
43 |
Correct |
26 ms |
1108 KB |
Output is correct |
44 |
Correct |
16 ms |
580 KB |
Output is correct |
45 |
Correct |
49 ms |
13948 KB |
Output is correct |
46 |
Correct |
53 ms |
13928 KB |
Output is correct |
47 |
Correct |
256 ms |
59100 KB |
Output is correct |
48 |
Correct |
267 ms |
61320 KB |
Output is correct |
49 |
Correct |
57 ms |
16432 KB |
Output is correct |
50 |
Correct |
60 ms |
16460 KB |
Output is correct |
51 |
Correct |
42 ms |
14412 KB |
Output is correct |
52 |
Correct |
37 ms |
14372 KB |
Output is correct |
53 |
Correct |
13 ms |
1456 KB |
Output is correct |
54 |
Correct |
66 ms |
13532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
9 ms |
372 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
37 ms |
364 KB |
Output is correct |
14 |
Correct |
41 ms |
344 KB |
Output is correct |
15 |
Correct |
45 ms |
340 KB |
Output is correct |
16 |
Correct |
67 ms |
16224 KB |
Output is correct |
17 |
Correct |
230 ms |
32300 KB |
Output is correct |
18 |
Correct |
616 ms |
65556 KB |
Output is correct |
19 |
Correct |
92 ms |
18604 KB |
Output is correct |
20 |
Correct |
84 ms |
18636 KB |
Output is correct |
21 |
Correct |
95 ms |
18628 KB |
Output is correct |
22 |
Correct |
189 ms |
33608 KB |
Output is correct |
23 |
Correct |
154 ms |
33656 KB |
Output is correct |
24 |
Correct |
177 ms |
34180 KB |
Output is correct |
25 |
Correct |
165 ms |
34420 KB |
Output is correct |
26 |
Correct |
164 ms |
34376 KB |
Output is correct |
27 |
Correct |
233 ms |
35096 KB |
Output is correct |
28 |
Correct |
210 ms |
35072 KB |
Output is correct |
29 |
Correct |
213 ms |
35084 KB |
Output is correct |
30 |
Correct |
131 ms |
24196 KB |
Output is correct |
31 |
Correct |
141 ms |
24212 KB |
Output is correct |
32 |
Correct |
133 ms |
24144 KB |
Output is correct |
33 |
Correct |
137 ms |
24264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
11 ms |
340 KB |
Output is correct |
5 |
Correct |
24 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
6 ms |
500 KB |
Output is correct |
9 |
Correct |
257 ms |
17004 KB |
Output is correct |
10 |
Correct |
1184 ms |
257060 KB |
Output is correct |
11 |
Correct |
8 ms |
344 KB |
Output is correct |
12 |
Correct |
84 ms |
392 KB |
Output is correct |
13 |
Correct |
284 ms |
91092 KB |
Output is correct |
14 |
Correct |
341 ms |
118840 KB |
Output is correct |
15 |
Runtime error |
3264 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |