#include <bits/stdc++.h>
using namespace std;
int pr[120001],dep[120001],vis[120001],deg[1001];
vector<int> adj[120001],tree[1001];
void dfs(int i,int ppp){
pr[i] = ppp;
dep[i] = dep[ppp]+1;
for(auto j:adj[i]){
if(j==ppp)continue;
dfs(j,i);
}
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//freopen("input.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t;cin>>t;
z:while(t--){
int n;cin>>n;
for(int i = 1;i<=n;i++){
adj[i].clear();
pr[i] = 0 , dep[i] = 0;
vis[i] = 0;
}
for(int i = 1;i<n;i++){
int a,b;cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1,0);
int q;cin>>q;
vector<pair<int,int>> qu;
for(int i = 1;i<=q;i++){
int a,b;
cin>>a>>b;
qu.push_back({a,b});
}
for(int i = 1;i<=q;i++){
deg[i] = 0 ;tree[i].clear();
}
for(int i = 0;i<qu.size();i++){
int x = qu[i].first , y = qu[i].second;
while(x!=y){
if(dep[x]<dep[y])swap(x,y);
vis[x] = i+1;
x = pr[x];
}
vis[x] = i+1;
for(int j = i+1;j<qu.size();j++){
if(vis[qu[j].first]==i+1&&vis[qu[j].second]==i+1){
cout<<"No\n";
goto z;
}
if(vis[qu[j].first]==i+1){
tree[i+1].push_back(j+1);
deg[j+1]++;
}else if(vis[qu[j].second]==i+1){
tree[j+1].push_back(i+1);
deg[i+1]++;
}
}
}
int cnt = 0;queue<int> qe;
for(int i = 1;i<=q;i++){
if(deg[i]==0){
qe.push(i);
}
}
bool ss = 1;
while(!qe.empty()){
int x = qe.front();qe.pop();
cnt++;
for(auto j:tree[x]){
deg[j]--;
if(deg[j]<0){
ss = 0;break;
}
if(deg[j]==0){
qe.push(j);
}
}
if(ss==0)break;
}
if(cnt==q&&ss)cout<<"Yes\n";
else cout<<"No\n";
}
return 0;
}
Compilation message
jail.cpp: In function 'int main()':
jail.cpp:42:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for(int i = 0;i<qu.size();i++){
| ~^~~~~~~~~~
jail.cpp:50:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for(int j = i+1;j<qu.size();j++){
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
3164 KB |
Output is correct |
2 |
Correct |
1 ms |
3160 KB |
Output is correct |
3 |
Incorrect |
1 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 |
1 ms |
3160 KB |
Output is correct |
3 |
Incorrect |
1 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 |
1 ms |
3160 KB |
Output is correct |
3 |
Incorrect |
1 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 |
1 ms |
3160 KB |
Output is correct |
3 |
Incorrect |
1 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 |
1 ms |
3160 KB |
Output is correct |
3 |
Incorrect |
1 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 |
Incorrect |
1 ms |
3164 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |