#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
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7256 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
8 ms |
7516 KB |
Output is correct |
5 |
Correct |
15 ms |
7956 KB |
Output is correct |
6 |
Correct |
2 ms |
7260 KB |
Output is correct |
7 |
Correct |
4 ms |
7260 KB |
Output is correct |
8 |
Correct |
159 ms |
7260 KB |
Output is correct |
9 |
Execution timed out |
5038 ms |
8044 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
2 ms |
7296 KB |
Output is correct |
5 |
Correct |
2 ms |
7260 KB |
Output is correct |
6 |
Correct |
2 ms |
7260 KB |
Output is correct |
7 |
Correct |
3 ms |
7260 KB |
Output is correct |
8 |
Correct |
2 ms |
7260 KB |
Output is correct |
9 |
Correct |
2 ms |
7260 KB |
Output is correct |
10 |
Correct |
2 ms |
7260 KB |
Output is correct |
11 |
Correct |
2 ms |
7260 KB |
Output is correct |
12 |
Correct |
2 ms |
7260 KB |
Output is correct |
13 |
Correct |
2 ms |
7260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
2 ms |
7296 KB |
Output is correct |
5 |
Correct |
2 ms |
7260 KB |
Output is correct |
6 |
Correct |
2 ms |
7260 KB |
Output is correct |
7 |
Correct |
3 ms |
7260 KB |
Output is correct |
8 |
Correct |
2 ms |
7260 KB |
Output is correct |
9 |
Correct |
2 ms |
7260 KB |
Output is correct |
10 |
Correct |
2 ms |
7260 KB |
Output is correct |
11 |
Correct |
2 ms |
7260 KB |
Output is correct |
12 |
Correct |
2 ms |
7260 KB |
Output is correct |
13 |
Correct |
2 ms |
7260 KB |
Output is correct |
14 |
Correct |
2 ms |
7260 KB |
Output is correct |
15 |
Correct |
1 ms |
7260 KB |
Output is correct |
16 |
Correct |
3 ms |
7260 KB |
Output is correct |
17 |
Correct |
3 ms |
7296 KB |
Output is correct |
18 |
Correct |
4 ms |
7340 KB |
Output is correct |
19 |
Correct |
2 ms |
7260 KB |
Output is correct |
20 |
Correct |
3 ms |
7260 KB |
Output is correct |
21 |
Correct |
3 ms |
7260 KB |
Output is correct |
22 |
Correct |
3 ms |
7260 KB |
Output is correct |
23 |
Correct |
2 ms |
7512 KB |
Output is correct |
24 |
Correct |
2 ms |
7260 KB |
Output is correct |
25 |
Correct |
3 ms |
7256 KB |
Output is correct |
26 |
Correct |
2 ms |
7260 KB |
Output is correct |
27 |
Correct |
3 ms |
7372 KB |
Output is correct |
28 |
Correct |
2 ms |
7260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
2 ms |
7296 KB |
Output is correct |
5 |
Correct |
2 ms |
7260 KB |
Output is correct |
6 |
Correct |
2 ms |
7260 KB |
Output is correct |
7 |
Correct |
3 ms |
7260 KB |
Output is correct |
8 |
Correct |
2 ms |
7260 KB |
Output is correct |
9 |
Correct |
2 ms |
7260 KB |
Output is correct |
10 |
Correct |
2 ms |
7260 KB |
Output is correct |
11 |
Correct |
2 ms |
7260 KB |
Output is correct |
12 |
Correct |
2 ms |
7260 KB |
Output is correct |
13 |
Correct |
2 ms |
7260 KB |
Output is correct |
14 |
Correct |
2 ms |
7260 KB |
Output is correct |
15 |
Correct |
1 ms |
7260 KB |
Output is correct |
16 |
Correct |
3 ms |
7260 KB |
Output is correct |
17 |
Correct |
3 ms |
7296 KB |
Output is correct |
18 |
Correct |
4 ms |
7340 KB |
Output is correct |
19 |
Correct |
2 ms |
7260 KB |
Output is correct |
20 |
Correct |
3 ms |
7260 KB |
Output is correct |
21 |
Correct |
3 ms |
7260 KB |
Output is correct |
22 |
Correct |
3 ms |
7260 KB |
Output is correct |
23 |
Correct |
2 ms |
7512 KB |
Output is correct |
24 |
Correct |
2 ms |
7260 KB |
Output is correct |
25 |
Correct |
3 ms |
7256 KB |
Output is correct |
26 |
Correct |
2 ms |
7260 KB |
Output is correct |
27 |
Correct |
3 ms |
7372 KB |
Output is correct |
28 |
Correct |
2 ms |
7260 KB |
Output is correct |
29 |
Correct |
157 ms |
7372 KB |
Output is correct |
30 |
Correct |
33 ms |
7352 KB |
Output is correct |
31 |
Correct |
5 ms |
7260 KB |
Output is correct |
32 |
Correct |
45 ms |
7360 KB |
Output is correct |
33 |
Correct |
10 ms |
7352 KB |
Output is correct |
34 |
Correct |
69 ms |
7380 KB |
Output is correct |
35 |
Correct |
71 ms |
7512 KB |
Output is correct |
36 |
Correct |
148 ms |
7376 KB |
Output is correct |
37 |
Correct |
99 ms |
7356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
2 ms |
7296 KB |
Output is correct |
5 |
Correct |
2 ms |
7260 KB |
Output is correct |
6 |
Correct |
2 ms |
7260 KB |
Output is correct |
7 |
Correct |
3 ms |
7260 KB |
Output is correct |
8 |
Correct |
2 ms |
7260 KB |
Output is correct |
9 |
Correct |
2 ms |
7260 KB |
Output is correct |
10 |
Correct |
2 ms |
7260 KB |
Output is correct |
11 |
Correct |
2 ms |
7260 KB |
Output is correct |
12 |
Correct |
2 ms |
7260 KB |
Output is correct |
13 |
Correct |
2 ms |
7260 KB |
Output is correct |
14 |
Correct |
2 ms |
7260 KB |
Output is correct |
15 |
Correct |
1 ms |
7260 KB |
Output is correct |
16 |
Correct |
3 ms |
7260 KB |
Output is correct |
17 |
Correct |
3 ms |
7296 KB |
Output is correct |
18 |
Correct |
4 ms |
7340 KB |
Output is correct |
19 |
Correct |
2 ms |
7260 KB |
Output is correct |
20 |
Correct |
3 ms |
7260 KB |
Output is correct |
21 |
Correct |
3 ms |
7260 KB |
Output is correct |
22 |
Correct |
3 ms |
7260 KB |
Output is correct |
23 |
Correct |
2 ms |
7512 KB |
Output is correct |
24 |
Correct |
2 ms |
7260 KB |
Output is correct |
25 |
Correct |
3 ms |
7256 KB |
Output is correct |
26 |
Correct |
2 ms |
7260 KB |
Output is correct |
27 |
Correct |
3 ms |
7372 KB |
Output is correct |
28 |
Correct |
2 ms |
7260 KB |
Output is correct |
29 |
Correct |
157 ms |
7372 KB |
Output is correct |
30 |
Correct |
33 ms |
7352 KB |
Output is correct |
31 |
Correct |
5 ms |
7260 KB |
Output is correct |
32 |
Correct |
45 ms |
7360 KB |
Output is correct |
33 |
Correct |
10 ms |
7352 KB |
Output is correct |
34 |
Correct |
69 ms |
7380 KB |
Output is correct |
35 |
Correct |
71 ms |
7512 KB |
Output is correct |
36 |
Correct |
148 ms |
7376 KB |
Output is correct |
37 |
Correct |
99 ms |
7356 KB |
Output is correct |
38 |
Execution timed out |
5047 ms |
8036 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
1 ms |
7256 KB |
Output is correct |
3 |
Correct |
1 ms |
7256 KB |
Output is correct |
4 |
Correct |
2 ms |
7260 KB |
Output is correct |
5 |
Correct |
6 ms |
7260 KB |
Output is correct |
6 |
Correct |
3 ms |
7260 KB |
Output is correct |
7 |
Correct |
3 ms |
7260 KB |
Output is correct |
8 |
Correct |
2 ms |
7092 KB |
Output is correct |
9 |
Correct |
2 ms |
7260 KB |
Output is correct |
10 |
Correct |
2 ms |
7260 KB |
Output is correct |
11 |
Correct |
2 ms |
7256 KB |
Output is correct |
12 |
Correct |
144 ms |
7340 KB |
Output is correct |
13 |
Correct |
99 ms |
7768 KB |
Output is correct |
14 |
Correct |
383 ms |
8188 KB |
Output is correct |
15 |
Correct |
348 ms |
7816 KB |
Output is correct |
16 |
Execution timed out |
5032 ms |
12880 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7256 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
8 ms |
7516 KB |
Output is correct |
5 |
Correct |
15 ms |
7956 KB |
Output is correct |
6 |
Correct |
2 ms |
7260 KB |
Output is correct |
7 |
Correct |
4 ms |
7260 KB |
Output is correct |
8 |
Correct |
159 ms |
7260 KB |
Output is correct |
9 |
Execution timed out |
5038 ms |
8044 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |