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();
}
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;
vector<int> path[m];
int o[m];for(int i = 0;i < m;i++) o[i] = i;
for(int i = 0;i < m;i++){
int s;cin >> s >> t;f = false;
dfs(s, g, -1, path[i]);
}
do {
bool occ[n+1] = {false};
for(int i = 0;i < m;i++) occ[path[i][0]] = true;
f = true;
for(int _ = 0;_ < m && f;_++){
int i = o[_];
occ[path[i][0]] = false;
for(int k : path[i]) {
if(occ[k]){
f = false;
break;
}
}
occ[path[i].back()] = true;
}
if(f) {
cout << "Yes\n";
return;
}
} while(next_permutation(o, o+m));
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... |