This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
const int maxn = 12*1e4+5;
int q,n,m;
vector<int>g[maxn],v[maxn];
int s[maxn],t[maxn];
int used[maxn];
bool ans;
int kr,prechki;
void precom()
{
for(int i=1;i<=n;i++)
{
g[i].clear();
used[i] = 0;
v[i].clear();
}
}
void read()
{
cin >> n;
precom();
int a,b;
for(int i=1;i<n;i++)
{
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
cin >> m;
for(int i=1;i<=m;i++)
{
cin >> s[i] >> t[i];
}
}
void dfs(int beg)
{
used[beg] = 1;
for(int nb:v[beg])
{
if(used[nb]==1) ans = false;
if(!used[nb]) dfs(nb);
}
used[beg] = 2;
}
void dfskr(int beg, int par, int sum)
{
sum += used[beg];
if(beg==kr) prechki = sum;
for(int nb:g[beg])
{
if(nb!=par) dfskr(nb,beg,sum);
}
}
void solve()
{
/*for(int i=1;i<=n;i++)
{
cout << i << "->";
for(int nb:g[i]) cout << " " << nb;
cout << endl;
}*/
for(int i=1;i<=m;i++)
{
for(int j=i+1;j<=m;j++)
{
int ch1,ch2;
ch1 = ch2 = 0;
used[s[j]] = 2;
used[t[j]] = 1;
kr = t[i];
dfskr(s[i],s[i],0);
ch1 = prechki;
used[s[j]] = 0;
used[t[j]] = 0;
if(ch1==3)
{
cout << "No" << endl;
return;
}
used[s[i]] = 2;
used[t[i]] = 1;
kr = t[j];
dfskr(s[j],s[j],0);
ch2 = prechki;
used[s[i]] = 0;
used[t[i]] = 0;
if(ch2==3)
{
cout << "No" << endl;
return;
}
if(ch1==1||ch2==2) v[i].push_back(j);
if(ch1==2||ch2==1) v[j].push_back(i);
}
}
/*for(int i=1;i<=m;i++)
{
cout << i << "->";
for(int nb:v[i]) cout << " " << nb;
cout << endl;
}*/
ans = true;
for(int i=1;i<=m;i++) used[i] = 0;
for(int i=1;i<=m;i++)
{
if(!used[i]) dfs(i);
}
if(ans) cout << "Yes" << endl;
else cout << "No" << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> q;
for(int i=1;i<=q;i++)
{
read();
solve();
}
return 0;
}
/*
1
8
1 2
2 3
3 4
4 5
5 6
6 7
7 8
4
1 5
2 6
3 7
4 8
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |