#include <bits/stdc++.h>
using namespace std;
int t,n,m,A[120005],B[120005],in[120005],out[120005],timp,par[120005],niv[1200005],dp[21][120005];
vector<int> muchii[120005];
bool isancestor(int a,int b)
{
return in[a]<=in[b]&&out[a]>=out[b];
}
int LCA(int a,int b)
{
if(niv[a]>niv[b])
swap(a,b);
if(isancestor(a,b))
return a;
for(int i=18;i>=0;i--)
if(dp[i][a]!=0&&!isancestor(dp[i][a],b))
a=dp[i][a];
return par[a];
}
int dist(int a,int b)
{
int lca=LCA(a,b);
return niv[a]+niv[b]-2*niv[lca];
}
void dfs(int nod)
{
timp++;
in[nod]=timp;
dp[0][nod]=par[nod];
for(int i=1;i<=18;i++)
dp[i][nod]=dp[i-1][dp[i-1][nod]];
for(int i:muchii[nod])
if(i!=par[nod])
{
par[i]=nod;
niv[i]=niv[nod]+1;
dfs(i);
}
out[nod]=timp;
}
void solve()
{
cin>>n;
timp=0;
for(int i=1;i<=n;i++)
muchii[i].clear();
for(int i=1;i<n;i++)
{
int a,b;
cin>>a>>b;
muchii[a].push_back(b);
muchii[b].push_back(a);
}
dfs(1);
cin>>m;
for(int i=1;i<=m;i++)
cin>>A[i]>>B[i];
for(int i=1;i<=m;i++)
for(int j=1;j<=m;j++)
if(i!=j)
{
if(dist(A[i],B[j])+dist(B[j],B[i])+dist(B[i],A[j])==dist(A[i],A[j]))
{
cout<<"No\n";
return;
}
if(dist(B[i],A[j])+dist(A[j],A[i])+dist(A[i],B[j])==dist(B[i],B[j]))
{
cout<<"No\n";
return;
}
if(dist(A[i],A[j])+dist(A[j],B[j])+dist(B[j],B[i])==dist(A[i],B[i]))
{
cout<<"No\n";
return;
}
if(dist(A[i],B[j])+dist(B[j],A[j])+dist(A[j],B[i])==dist(A[i],B[i]))
{
cout<<"No\n";
return;
}
}
cout<<"Yes\n";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>t;
while(t--)
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14936 KB |
Output is correct |
2 |
Correct |
2 ms |
14936 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
9 ms |
14940 KB |
Output is correct |
5 |
Correct |
17 ms |
15648 KB |
Output is correct |
6 |
Correct |
3 ms |
14936 KB |
Output is correct |
7 |
Correct |
3 ms |
15076 KB |
Output is correct |
8 |
Correct |
6 ms |
14940 KB |
Output is correct |
9 |
Correct |
78 ms |
16588 KB |
Output is correct |
10 |
Correct |
40 ms |
28272 KB |
Output is correct |
11 |
Correct |
6 ms |
14940 KB |
Output is correct |
12 |
Correct |
32 ms |
15776 KB |
Output is correct |
13 |
Execution timed out |
5092 ms |
29264 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
15024 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14940 KB |
Output is correct |
6 |
Correct |
4 ms |
14936 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
3 ms |
14952 KB |
Output is correct |
9 |
Correct |
3 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14996 KB |
Output is correct |
13 |
Correct |
2 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
15024 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14940 KB |
Output is correct |
6 |
Correct |
4 ms |
14936 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
3 ms |
14952 KB |
Output is correct |
9 |
Correct |
3 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14996 KB |
Output is correct |
13 |
Correct |
2 ms |
14940 KB |
Output is correct |
14 |
Incorrect |
2 ms |
14940 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
15024 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14940 KB |
Output is correct |
6 |
Correct |
4 ms |
14936 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
3 ms |
14952 KB |
Output is correct |
9 |
Correct |
3 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14996 KB |
Output is correct |
13 |
Correct |
2 ms |
14940 KB |
Output is correct |
14 |
Incorrect |
2 ms |
14940 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
15024 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14940 KB |
Output is correct |
6 |
Correct |
4 ms |
14936 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
3 ms |
14952 KB |
Output is correct |
9 |
Correct |
3 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14996 KB |
Output is correct |
13 |
Correct |
2 ms |
14940 KB |
Output is correct |
14 |
Incorrect |
2 ms |
14940 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Incorrect |
2 ms |
14940 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14936 KB |
Output is correct |
2 |
Correct |
2 ms |
14936 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
9 ms |
14940 KB |
Output is correct |
5 |
Correct |
17 ms |
15648 KB |
Output is correct |
6 |
Correct |
3 ms |
14936 KB |
Output is correct |
7 |
Correct |
3 ms |
15076 KB |
Output is correct |
8 |
Correct |
6 ms |
14940 KB |
Output is correct |
9 |
Correct |
78 ms |
16588 KB |
Output is correct |
10 |
Correct |
40 ms |
28272 KB |
Output is correct |
11 |
Correct |
6 ms |
14940 KB |
Output is correct |
12 |
Correct |
32 ms |
15776 KB |
Output is correct |
13 |
Execution timed out |
5092 ms |
29264 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |