#include <bits/stdc++.h>
using namespace std;
#define int long long
const int modulo = 1e9+7;
void solve(){
int n;
cin >> n;
vector<vector<int>> adj(n+1);
for(int i=1;i<n;i++){
int a,b;cin>>a>>b;
adj[a].emplace_back(b);
adj[b].emplace_back(a);
}
vector<int> depth(n+1);
vector<vector<int>> p(n+1,vector<int>(17));
function<void(int,int,int)> dfs = [&](int x,int par,int dep){
depth[x]=dep;
p[x][0]=par;
for(int&i:adj[x])if(i!=par)dfs(i,x,dep+1);
};
dfs(1,0,1);
for(int bit=1;bit<17;bit++){
for(int i=1;i<=n;i++){
p[i][bit] = p[p[i][bit-1]][bit-1];
}
}
auto lift = [&](int x,int tar){
for(int bit=0;bit<17;bit++)if(tar&(1<<bit))x=p[x][bit];
return x;
};
int m;
cin >> m;
vector<vector<int>> secadj(m+1);
vector<int> s(m+1);
vector<int> t(m+1);
auto collides = [&](int curr,int v){
int a = s[curr];
int b = t[curr];
if(a==v or b==v)return true;
bool worksa = false;
if(depth[a]>=depth[v] and lift(a,depth[a]-depth[v])==v)worksa=true;
bool worksb = false;
if(depth[b]>=depth[v] and lift(b,depth[b]-depth[v])==v)worksb=true;
if(worksa and worksb){
return lift(a,depth[a]-depth[v]-1)!=lift(b,depth[b]-depth[v]-1);
} else return worksa or worksb;
};
for(int i=1;i<=m;i++)cin>>s[i]>>t[i];
bool works = true;
for(int i=1;i<=m;i++){
for(int j=1;j<i;j++){
bool acontains = collides(i,s[j]);
bool acontaint = collides(i,t[j]);
bool bcontains = collides(j,s[i]);
bool bcontaint = collides(j,t[i]);
if(acontains and acontaint)works=false;
if(acontains and bcontains)works=false;
if(acontaint and bcontaint)works=false;
if(bcontains and bcontaint)works=false;
}
}
if(works){
cout << "Yes\n";
} else cout << "No\n";
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while(t--)solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
11 ms |
600 KB |
Output is correct |
5 |
Correct |
23 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
568 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
25 ms |
556 KB |
Output is correct |
9 |
Correct |
1034 ms |
3264 KB |
Output is correct |
10 |
Correct |
74 ms |
39760 KB |
Output is correct |
11 |
Correct |
10 ms |
604 KB |
Output is correct |
12 |
Correct |
304 ms |
1320 KB |
Output is correct |
13 |
Execution timed out |
5046 ms |
42064 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
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 |
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 |
1 ms |
348 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 |
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 |
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 |
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 |
1 ms |
348 KB |
Output is correct |
14 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
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 |
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 |
1 ms |
348 KB |
Output is correct |
14 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
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 |
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 |
1 ms |
348 KB |
Output is correct |
14 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
11 ms |
600 KB |
Output is correct |
5 |
Correct |
23 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
568 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
25 ms |
556 KB |
Output is correct |
9 |
Correct |
1034 ms |
3264 KB |
Output is correct |
10 |
Correct |
74 ms |
39760 KB |
Output is correct |
11 |
Correct |
10 ms |
604 KB |
Output is correct |
12 |
Correct |
304 ms |
1320 KB |
Output is correct |
13 |
Execution timed out |
5046 ms |
42064 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |