#include <vector>
#include <iostream>
#include <utility>
#include <set>
#include <algorithm>
using namespace std;
#define v vector
#define pii pair<int,int>
#define fi first
#define se second
int N,M;
int anc=20;
v<v<int>> adj;
v<int> depth;
v<v<int>> par;
void dfs(int cur, int from){
if(depth[cur]!=-1) return;
depth[cur]=depth[from]+1;
par[cur][0]=from;
for(int it:adj[cur]) dfs(it, cur);
}
bool is_anc(int cur, int other){
for(int i=anc-1; i>=0; i--){
if(depth[par[cur][i]]>=depth[other]) cur=par[cur][i];
}
if(cur==other) return true;
return false;
}
int lca(int a, int b){
if(depth[a]<depth[b]) swap(a,b);
for(int i=anc-1; i>=0; i--){
if(depth[par[a][i]]>=depth[b]) a=par[a][i];
}
if(a==b) return a;
for(int i=anc-1; i>=0; i--){
if(par[a][i]!=par[b][i]){
a=par[a][i];
b=par[b][i];
}
}
return par[a][0];
}
v<bool> prevrec;
v<bool> vis;
bool find_cycle(int cur){
if(!vis[cur]){
vis[cur]=true;
prevrec[cur]=true;
for(int it:adj[cur]){
if(!vis[it] && find_cycle(it)){
return true;
}
else if(prevrec[it]){
return true;
}
}
}
prevrec[cur]=false;
return false;
}
int main(){
int Q;
cin>>Q;
for(int q=0;q<Q;q++){
cin>>N;
adj=v<v<int>>(N);
for(int i=0; i<N-1; i++){
int a,b;
cin>>a>>b;
a--;
b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
depth=v<int>(N,-1);
par=v<v<int>>(N,v<int>(anc,-1));
dfs(0,0);
for(int i=1; i<anc; i++){
for(int j=0; j<N; j++){
par[j][i]=par[par[j][i-1]][i-1];
}
}
cin>>M;
v<pii> rooms(M);
v<int> ps(N,-1);
v<int> pe(N,-1);
for(int i=0; i<M; i++){
cin>>rooms[i].fi>>rooms[i].se;
rooms[i].fi--;
rooms[i].se--;
ps[rooms[i].fi]=i;
pe[rooms[i].se]=i;
}
adj = v<v<int>>(M);
for(int i=0; i<M; i++){
int a=rooms[i].fi;
int b=rooms[i].se;
int l=lca(a,b);
while(true){
if(ps[a]!=-1 && ps[a]!=i) adj[ps[a]].push_back(i);
if(pe[a]!=-1 && pe[a]!=i) adj[i].push_back(pe[a]);
if(a==l) break;
a=par[a][0];
}
while(b!=l){
if(ps[b]!=-1 && ps[b]!=i) adj[ps[b]].push_back(i);
if(pe[b]!=-1 && pe[b]!=i) adj[i].push_back(pe[b]);
b=par[b][0];
}
}
bool cycle=false;
vis = v<bool>(M,false);
prevrec = v<bool>(M,false);
for(int i=0; i<M; i++){
if(!vis[i] && find_cycle(i)){
cycle=true;
break;
}
}
if(cycle) cout<<"No\n";
else cout<<"Yes\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
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 |
34 ms |
340 KB |
Output is correct |
5 |
Correct |
68 ms |
212 KB |
Output is correct |
6 |
Correct |
4 ms |
340 KB |
Output is correct |
7 |
Correct |
4 ms |
340 KB |
Output is correct |
8 |
Correct |
6 ms |
340 KB |
Output is correct |
9 |
Correct |
170 ms |
2736 KB |
Output is correct |
10 |
Correct |
760 ms |
26136 KB |
Output is correct |
11 |
Correct |
15 ms |
304 KB |
Output is correct |
12 |
Correct |
110 ms |
420 KB |
Output is correct |
13 |
Correct |
246 ms |
50828 KB |
Output is correct |
14 |
Correct |
274 ms |
64924 KB |
Output is correct |
15 |
Execution timed out |
5095 ms |
603052 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
4 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
340 KB |
Output is correct |
10 |
Correct |
4 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
4 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
340 KB |
Output is correct |
10 |
Correct |
4 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
340 KB |
Output is correct |
17 |
Correct |
4 ms |
340 KB |
Output is correct |
18 |
Correct |
3 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
3 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
340 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
4 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
3 ms |
340 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 |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
4 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
340 KB |
Output is correct |
10 |
Correct |
4 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
340 KB |
Output is correct |
17 |
Correct |
4 ms |
340 KB |
Output is correct |
18 |
Correct |
3 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
3 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
340 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
4 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
3 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
6 ms |
340 KB |
Output is correct |
30 |
Correct |
5 ms |
340 KB |
Output is correct |
31 |
Correct |
5 ms |
340 KB |
Output is correct |
32 |
Correct |
4 ms |
340 KB |
Output is correct |
33 |
Correct |
3 ms |
340 KB |
Output is correct |
34 |
Correct |
4 ms |
340 KB |
Output is correct |
35 |
Correct |
5 ms |
340 KB |
Output is correct |
36 |
Correct |
3 ms |
340 KB |
Output is correct |
37 |
Correct |
3 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
4 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
340 KB |
Output is correct |
10 |
Correct |
4 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
340 KB |
Output is correct |
17 |
Correct |
4 ms |
340 KB |
Output is correct |
18 |
Correct |
3 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
3 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
340 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
4 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
3 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
6 ms |
340 KB |
Output is correct |
30 |
Correct |
5 ms |
340 KB |
Output is correct |
31 |
Correct |
5 ms |
340 KB |
Output is correct |
32 |
Correct |
4 ms |
340 KB |
Output is correct |
33 |
Correct |
3 ms |
340 KB |
Output is correct |
34 |
Correct |
4 ms |
340 KB |
Output is correct |
35 |
Correct |
5 ms |
340 KB |
Output is correct |
36 |
Correct |
3 ms |
340 KB |
Output is correct |
37 |
Correct |
3 ms |
340 KB |
Output is correct |
38 |
Correct |
169 ms |
2748 KB |
Output is correct |
39 |
Correct |
727 ms |
26080 KB |
Output is correct |
40 |
Correct |
139 ms |
2068 KB |
Output is correct |
41 |
Correct |
88 ms |
2080 KB |
Output is correct |
42 |
Correct |
106 ms |
2208 KB |
Output is correct |
43 |
Correct |
79 ms |
2116 KB |
Output is correct |
44 |
Correct |
22 ms |
596 KB |
Output is correct |
45 |
Correct |
129 ms |
22348 KB |
Output is correct |
46 |
Correct |
131 ms |
22484 KB |
Output is correct |
47 |
Correct |
318 ms |
24140 KB |
Output is correct |
48 |
Correct |
320 ms |
24204 KB |
Output is correct |
49 |
Correct |
148 ms |
22476 KB |
Output is correct |
50 |
Correct |
142 ms |
22440 KB |
Output is correct |
51 |
Correct |
114 ms |
23012 KB |
Output is correct |
52 |
Correct |
111 ms |
22936 KB |
Output is correct |
53 |
Correct |
26 ms |
2156 KB |
Output is correct |
54 |
Correct |
150 ms |
22340 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
18 ms |
312 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 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 |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
4 ms |
372 KB |
Output is correct |
13 |
Correct |
58 ms |
320 KB |
Output is correct |
14 |
Correct |
84 ms |
340 KB |
Output is correct |
15 |
Correct |
70 ms |
340 KB |
Output is correct |
16 |
Correct |
151 ms |
22940 KB |
Output is correct |
17 |
Correct |
271 ms |
24384 KB |
Output is correct |
18 |
Correct |
538 ms |
43720 KB |
Output is correct |
19 |
Correct |
167 ms |
23132 KB |
Output is correct |
20 |
Correct |
165 ms |
23160 KB |
Output is correct |
21 |
Correct |
166 ms |
23108 KB |
Output is correct |
22 |
Correct |
235 ms |
24452 KB |
Output is correct |
23 |
Correct |
221 ms |
24396 KB |
Output is correct |
24 |
Correct |
219 ms |
24372 KB |
Output is correct |
25 |
Correct |
220 ms |
24492 KB |
Output is correct |
26 |
Correct |
221 ms |
24468 KB |
Output is correct |
27 |
Correct |
309 ms |
26520 KB |
Output is correct |
28 |
Correct |
295 ms |
30820 KB |
Output is correct |
29 |
Correct |
345 ms |
26780 KB |
Output is correct |
30 |
Correct |
213 ms |
24508 KB |
Output is correct |
31 |
Correct |
219 ms |
25164 KB |
Output is correct |
32 |
Correct |
225 ms |
24444 KB |
Output is correct |
33 |
Correct |
228 ms |
25204 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 |
34 ms |
340 KB |
Output is correct |
5 |
Correct |
68 ms |
212 KB |
Output is correct |
6 |
Correct |
4 ms |
340 KB |
Output is correct |
7 |
Correct |
4 ms |
340 KB |
Output is correct |
8 |
Correct |
6 ms |
340 KB |
Output is correct |
9 |
Correct |
170 ms |
2736 KB |
Output is correct |
10 |
Correct |
760 ms |
26136 KB |
Output is correct |
11 |
Correct |
15 ms |
304 KB |
Output is correct |
12 |
Correct |
110 ms |
420 KB |
Output is correct |
13 |
Correct |
246 ms |
50828 KB |
Output is correct |
14 |
Correct |
274 ms |
64924 KB |
Output is correct |
15 |
Execution timed out |
5095 ms |
603052 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |