#include <bits/stdc++.h>
using namespace std;
main(){
int tt = 1; cin >> tt;
while(tt--){
int n;
cin >> n;
vector<int> adj[n + 1];
for(int i = 1; i <= n - 1; i++){
int x, y;
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
vector<int> par(n + 1), d(n + 1);
function<void(int, int)> dfs = [&](int v, int p){
par[v] = p; d[v] = d[p] + 1;
for(auto x : adj[v]){
if(x == p) continue;
dfs(x, v);
}
};
dfs(1, 0);
int q;
cin >> q;
vector<int> a(q + 1), b(q + 1), g[n + 1];
for(int i = 1; i <= q; i++){
cin >> a[i] >> b[i];
g[b[i]].push_back(i);
}
vector<int> G[q + 1], in(q + 1);
for(int i = 1; i <= q; i++){
int x = a[i], y = par[b[i]];
while(x != y){
if(d[x] < d[y]) swap(x, y);
for(auto u : g[x]){
if(u == i) continue;
G[i].push_back(u);
in[u]++;
}
x = par[x];
}
}
queue<int> Q;
for(int i = 1; i <= q; i++){
if(in[i] == 0) Q.push(i);
}
vector<int> p;
while(!Q.empty()){
int v = Q.front();
Q.pop();
p.push_back(v);
for(auto x : G[v]){
in[x]--;
if(in[x] == 0) Q.push(x);
}
}
if(p.size() == q) cout << "Yes" << '\n';
else cout << "No" << '\n';
}
}
Compilation message
jail.cpp:4:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
4 | main(){
| ^~~~
jail.cpp: In function 'int main()':
jail.cpp:59:15: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
59 | if(p.size() == q) cout << "Yes" << '\n';
| ~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
440 KB |
Output is correct |
3 |
Incorrect |
1 ms |
504 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
440 KB |
Output is correct |
3 |
Incorrect |
1 ms |
504 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |