This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
bool f; int T;
void dfs(int n, vector<int> g[], int p, vector<int>& path){
path.push_back(n);
if(n == T){
f = true;
return;
}
for(int c : g[n]){
if(f) break;
if(c == p) continue;
dfs(c, g, n, path);
}
if(!f) path.pop_back();
}
struct node {
vector<node*> l;
bool v = false, c = false;
};
bool ans;
void dfs(node* n){
n->v = n->c = true;
for(node* c : n->l){
if(!ans) break;
if(c->c){
ans = false;
break;
}
if(!c->v) dfs(c);
}
n->c = false;
}
void solve(){
int n,m;cin >> n;
vector<int> g[n+1];
for(int i = 1;i < n;i++){
int u, v;cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
cin >> m;
int s[m], t[m];
int sn[n+1], tn[n+1];
fill(sn+1, sn+n+1, -1);fill(tn+1, tn+n+1, -1);
for(int i = 0;i < m;i++){
cin >> s[i] >> t[i];
sn[s[i]] = i;
tn[t[i]] = i;
}
node mg[m];
for(int i = 0;i < m;i++){
vector<int> path;
f = false;T = t[i];
dfs(s[i], g, -1, path);
for(int _ : path){
if(sn[_] != -1 && sn[_] != i) mg[i].l.push_back(&mg[sn[_]]);
if(tn[_] != -1 && tn[_] != i) mg[tn[_]].l.push_back(&mg[i]);
}
}
ans = true;
for(int i = 0;i < m && ans;i++){
if(!mg[i].v) dfs(&mg[i]);
}
if(ans)cout << "Yes\n";
else cout << "No\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;cin >> t;
while(t--) solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |