#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds
const int N = 120005;
int p[N][20];
vector<int> side[N];
vector<pair<int,int> > qu;
pair<int,int> dep[N];
int n;
int t;
bool vis[N];
int dtime[N];
int t2;
void dfs(int cur){
dep[cur].first=++t;
for(auto i : side[cur]) {
if(p[i][0]) continue;
p[i][0]=cur;
dfs(i);
}
dep[cur].second=++t;
}
vector<int> fe[N];
bool isanc(int a,int b){
return (dep[a].first<dep[b].first && dep[a].second>dep[b].second);
}
int lca(int a,int b){
if(isanc(a,b)) return a;
if(isanc(b,a)) return b;
for(int j=19;j>=0;j--){
if(!isanc(p[b][j],a)) b = p[b][j];
}
return p[b][0];
}
bool inpath(int a,int b,int u){
int c = lca(a,b);
if(a==u || b==u || c==u) return 1;
if((isanc(u,b)||isanc(u,a)) && isanc(c,u)) return 1;
return 0;
}
void builtdep(int cur){
vis[cur]=1;
for(auto i : fe[cur]){
if(!vis[i]) builtdep(i);
}
dtime[cur]=++t2;
}
void solve(){
cin>>n;
//clean up
for(int i=1;i<=n;i++){
side[i].clear();
fe[i].clear();
for(int j=0;j<20;j++) p[i][j]=0;
qu.clear();
dep[i] = {0,0};
t = 0 ;
t2=0;
vis[i]=0;
dtime[i]=0;
}
// built tree
for(int i=0;i<n-1;i++){
int a,b;cin>>a>>b;
side[a].push_back(b);
side[b].push_back(a);
}
for(int j=0;j<20;j++) p[1][j]=1;
dfs(1);
for(int j=1;j<20;j++){
for(int i=2;i<=n;i++){
p[i][j] = p[p[i][j-1]][j-1];
}
}
int m;
cin>>m;
qu.resize(m+1);
for(int i=1;i<=m;i++) cin>>qu[i].first>>qu[i].second;
for(int i=1;i<=m;i++){
for(int j=1;j<=m;j++){
if(i==j) continue;
if(inpath(qu[i].first,qu[i].second,qu[j].first)) fe[j].push_back(i);
if(inpath(qu[i].first,qu[i].second,qu[j].second)) fe[i].push_back(j);
}
}
for(int i=1;i<=m;i++){
if(!vis[i]) builtdep(i);
}
for(int i=1;i<=m;i++){
for(auto e : fe[i]){
if(dtime[e]>dtime[i]){
cout<<"No\n";
return ;
}
}
}
cout<<"Yes\n";
return ;
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int q;cin>>q;
while(q--){
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8952 KB |
Output is correct |
4 |
Correct |
8 ms |
8856 KB |
Output is correct |
5 |
Correct |
15 ms |
8852 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
5 ms |
8796 KB |
Output is correct |
9 |
Correct |
84 ms |
11316 KB |
Output is correct |
10 |
Correct |
39 ms |
29520 KB |
Output is correct |
11 |
Correct |
6 ms |
8796 KB |
Output is correct |
12 |
Correct |
58 ms |
8936 KB |
Output is correct |
13 |
Execution timed out |
5094 ms |
33768 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
3 ms |
8796 KB |
Output is correct |
5 |
Correct |
3 ms |
8796 KB |
Output is correct |
6 |
Correct |
3 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
3 ms |
8796 KB |
Output is correct |
5 |
Correct |
3 ms |
8796 KB |
Output is correct |
6 |
Correct |
3 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
1 ms |
8792 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
2 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
3 ms |
8796 KB |
Output is correct |
21 |
Correct |
2 ms |
8796 KB |
Output is correct |
22 |
Correct |
3 ms |
8796 KB |
Output is correct |
23 |
Correct |
2 ms |
8796 KB |
Output is correct |
24 |
Correct |
2 ms |
8796 KB |
Output is correct |
25 |
Correct |
2 ms |
8796 KB |
Output is correct |
26 |
Correct |
2 ms |
8796 KB |
Output is correct |
27 |
Correct |
2 ms |
8796 KB |
Output is correct |
28 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
3 ms |
8796 KB |
Output is correct |
5 |
Correct |
3 ms |
8796 KB |
Output is correct |
6 |
Correct |
3 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
1 ms |
8792 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
2 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
3 ms |
8796 KB |
Output is correct |
21 |
Correct |
2 ms |
8796 KB |
Output is correct |
22 |
Correct |
3 ms |
8796 KB |
Output is correct |
23 |
Correct |
2 ms |
8796 KB |
Output is correct |
24 |
Correct |
2 ms |
8796 KB |
Output is correct |
25 |
Correct |
2 ms |
8796 KB |
Output is correct |
26 |
Correct |
2 ms |
8796 KB |
Output is correct |
27 |
Correct |
2 ms |
8796 KB |
Output is correct |
28 |
Correct |
2 ms |
8796 KB |
Output is correct |
29 |
Correct |
5 ms |
8796 KB |
Output is correct |
30 |
Correct |
20 ms |
8912 KB |
Output is correct |
31 |
Correct |
13 ms |
8860 KB |
Output is correct |
32 |
Correct |
10 ms |
8900 KB |
Output is correct |
33 |
Correct |
3 ms |
8796 KB |
Output is correct |
34 |
Correct |
40 ms |
8904 KB |
Output is correct |
35 |
Correct |
41 ms |
8944 KB |
Output is correct |
36 |
Correct |
40 ms |
8796 KB |
Output is correct |
37 |
Correct |
21 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
3 ms |
8796 KB |
Output is correct |
5 |
Correct |
3 ms |
8796 KB |
Output is correct |
6 |
Correct |
3 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
1 ms |
8792 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
2 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
3 ms |
8796 KB |
Output is correct |
21 |
Correct |
2 ms |
8796 KB |
Output is correct |
22 |
Correct |
3 ms |
8796 KB |
Output is correct |
23 |
Correct |
2 ms |
8796 KB |
Output is correct |
24 |
Correct |
2 ms |
8796 KB |
Output is correct |
25 |
Correct |
2 ms |
8796 KB |
Output is correct |
26 |
Correct |
2 ms |
8796 KB |
Output is correct |
27 |
Correct |
2 ms |
8796 KB |
Output is correct |
28 |
Correct |
2 ms |
8796 KB |
Output is correct |
29 |
Correct |
5 ms |
8796 KB |
Output is correct |
30 |
Correct |
20 ms |
8912 KB |
Output is correct |
31 |
Correct |
13 ms |
8860 KB |
Output is correct |
32 |
Correct |
10 ms |
8900 KB |
Output is correct |
33 |
Correct |
3 ms |
8796 KB |
Output is correct |
34 |
Correct |
40 ms |
8904 KB |
Output is correct |
35 |
Correct |
41 ms |
8944 KB |
Output is correct |
36 |
Correct |
40 ms |
8796 KB |
Output is correct |
37 |
Correct |
21 ms |
8796 KB |
Output is correct |
38 |
Correct |
83 ms |
12372 KB |
Output is correct |
39 |
Correct |
41 ms |
31020 KB |
Output is correct |
40 |
Correct |
822 ms |
11260 KB |
Output is correct |
41 |
Correct |
1008 ms |
10352 KB |
Output is correct |
42 |
Correct |
929 ms |
11736 KB |
Output is correct |
43 |
Correct |
21 ms |
10320 KB |
Output is correct |
44 |
Correct |
931 ms |
9300 KB |
Output is correct |
45 |
Correct |
87 ms |
22200 KB |
Output is correct |
46 |
Correct |
87 ms |
22224 KB |
Output is correct |
47 |
Correct |
86 ms |
25936 KB |
Output is correct |
48 |
Correct |
85 ms |
25940 KB |
Output is correct |
49 |
Correct |
84 ms |
22380 KB |
Output is correct |
50 |
Correct |
93 ms |
22152 KB |
Output is correct |
51 |
Correct |
40 ms |
23376 KB |
Output is correct |
52 |
Correct |
40 ms |
23244 KB |
Output is correct |
53 |
Correct |
874 ms |
9296 KB |
Output is correct |
54 |
Correct |
96 ms |
22104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
6 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8856 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8848 KB |
Output is correct |
12 |
Correct |
38 ms |
8796 KB |
Output is correct |
13 |
Correct |
359 ms |
9356 KB |
Output is correct |
14 |
Correct |
172 ms |
9552 KB |
Output is correct |
15 |
Correct |
290 ms |
9412 KB |
Output is correct |
16 |
Execution timed out |
5022 ms |
22604 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8952 KB |
Output is correct |
4 |
Correct |
8 ms |
8856 KB |
Output is correct |
5 |
Correct |
15 ms |
8852 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
5 ms |
8796 KB |
Output is correct |
9 |
Correct |
84 ms |
11316 KB |
Output is correct |
10 |
Correct |
39 ms |
29520 KB |
Output is correct |
11 |
Correct |
6 ms |
8796 KB |
Output is correct |
12 |
Correct |
58 ms |
8936 KB |
Output is correct |
13 |
Execution timed out |
5094 ms |
33768 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |