#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int maxn = 120010;
int n, m, s[maxn], f[maxn], used[maxn];
vector<int> v[maxn];
void read(){
cin >> n;
for(int i = 1;i <= n;i++)
v[i].clear();
for(int i = 1;i <= n - 1;i++){
int x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
cin >> m;
for(int i = 1;i <= m;i++){
cin >> s[i] >> f[i];
}
}
void dfs(int ver){
if(used[ver] > 1)
return;
used[ver] = -1;
for(int nb : v[ver]){
if(used[nb])
continue;
dfs(nb);
}
}
void solve(){
read();
for(int i = 1;i <= m;i++){
for(int j = 1;j <= m;j++){
if(i == j)
continue;
for(int k = 1;k <= n;k++)
used[k] = 0;
for(int k = 1;k <= m;k++)
used[s[k]]++;
dfs(s[j]);
int cnt = 0;
if(used[f[j]] != -1)
cnt++;
for(int k = 1;k <= n;k++)
used[k] = 0;
for(int k = 1;k <= m;k++)
used[s[k]]++;
dfs(s[i]);
if(used[f[i]] != -1)
cnt++;
else{
for(int k = 1;k <= n;k++)
used[k] = 0;
for(int k = 1;k <= m;k++)
used[s[k]]++;
used[s[i]]--;
used[f[i]]++;
dfs(s[j]);
if(used[f[j]] != -1)
cnt++;
}
if(cnt == 2){
cout << "No\n";
return;
}
}
}
cout << "Yes\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--){
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3164 KB |
Output is correct |
2 |
Incorrect |
1 ms |
3164 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3160 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Incorrect |
2 ms |
3164 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3160 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Incorrect |
2 ms |
3164 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3160 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Incorrect |
2 ms |
3164 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3160 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Incorrect |
2 ms |
3164 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3164 KB |
Output is correct |
2 |
Correct |
2 ms |
3396 KB |
Output is correct |
3 |
Incorrect |
1 ms |
3160 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3164 KB |
Output is correct |
2 |
Incorrect |
1 ms |
3164 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |