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;
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
void solve(){
int n; cin >> n;
vector <vector<int>> G(n);
for ( int i = 0; i + 1 < n; i++ ){
int u, v; cin >> u >> v;
--u, --v;
G[u].pb(v), G[v].pb(u);
}
int m; cin >> m;
vector <int> S(m), T(m);
for ( int i = 0; i < m; i++ ){
cin >> S[i] >> T[i];
--S[i], --T[i];
}
vector <int> tmp;
auto dfs = [&](auto dfs, int u, int p, int des) -> bool{
if ( u == des ){
return true;
}
for ( auto &v: G[u] ){
if ( v != p ){
if ( dfs(dfs, v, u, des) ){
tmp.pb(u);
return true;
}
}
}
return false;
};
vector <vector<int>> lst(m);
for ( int i = 0; i < m; i++ ){
tmp.clear();
dfs(dfs, S[i], -1, T[i]);
reverse(all(tmp));
tmp.pb(T[i]);
lst[i] = tmp;
}
vector <int> p(m);
iota(all(p), 0);
do{
vector <int> us(n);
for ( int i = 0; i < m; i++ ){
us[S[i]] = true;
}
bool good = true;
for ( int i = 0; i < m; i++ ){
auto &tmp = lst[p[i]];
int pre = S[p[i]];
for ( int j = 1; j < tmp.size(); j++ ){
if ( us[tmp[j]] ){
good = false;
} us[pre] = false;
us[pre = tmp[j]] = true;
}
}
if ( good ){
cout << "Yes\n";
return;
}
} while ( next_permutation(all(p)) );
cout << "No\n";
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t; cin >> t;
while ( t-- ){
solve();
}
cout << '\n';
}
Compilation message (stderr)
jail.cpp: In function 'void solve()':
jail.cpp:79:32: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for ( int j = 1; j < tmp.size(); j++ ){
| ~~^~~~~~~~~~~~
# | 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... |