#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
const int maxn = 12*1e4+5;
int q,n,m;
int inf = 1e9;
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<=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);
if(prechki==2) ch1 = 2;
if(prechki==1) ch1 = 1;
used[s[j]] = 0;
used[t[j]] = 0;
if(prechki==3)
{
cout << "No" << endl;
return;
}
used[s[i]] = 2;
used[t[i]] = 1;
kr = t[j];
dfskr(s[j],s[j],0);
used[s[j]] = 0;
used[t[j]] = 0;
if(prechki==2) ch2 = 2;
if(prechki==1) ch2 = 1;
if(prechki==3||ch1==ch2)
{
cout << "No" << endl;
return;
}
if(ch1==1||ch2==2) v[i].push_back(j);
if(ch1==2||ch2==1) v[j].push_back(i);
}
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7256 KB |
Output is correct |
2 |
Incorrect |
2 ms |
7256 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
1 ms |
7260 KB |
Output is correct |
3 |
Incorrect |
2 ms |
7336 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
1 ms |
7260 KB |
Output is correct |
3 |
Incorrect |
2 ms |
7336 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
1 ms |
7260 KB |
Output is correct |
3 |
Incorrect |
2 ms |
7336 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
1 ms |
7260 KB |
Output is correct |
3 |
Incorrect |
2 ms |
7336 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
1 ms |
7260 KB |
Output is correct |
3 |
Incorrect |
2 ms |
7256 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7256 KB |
Output is correct |
2 |
Incorrect |
2 ms |
7256 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |