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;
typedef long long ll;
const int N = 5e5 + 10, MOD = 998244353;
vector<int> g[N],x[N];
int timer = 0,tin[N],tout[N],n,m,s[N],t[N],blocked[N],c[N],pp[N];
bool used[N];
void dfs(int v,int pr = 1){
tin[v] = ++timer;
pp[v] = pr;
for(int to:g[v]){
if(to == pr) continue;
dfs(to,v);
}
tout[v] = ++timer;
}
bool is(int v,int u){
return (tin[v] <= tin[u] && tout[v] >= tout[u]);
}
vector<int> path(int v,int u){
vector<int> p(1,v);
while(!is(v,u)){
v = pp[v];
p.push_back(v);
}
while(!is(u,v)){
p.push_back(u);
u = pp[u];
}
return p;
}
bool can(int i){
for(int j:x[i]){
if(j != s[i] && used[j]) return false;
}
if(c[t[i]] > 1) return false;
return true;
}
void test(){
timer =0 ;
cin >> n;
for(int i = 1;i <= n;i++)
g[i].clear();
for(int i = 1;i <= n - 1;i++){
int x,y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
used[x] = used[y] = c[x] = c[y] = 0;
}
dfs(1);
cin >> m;
for(int i = 1;i <= m;i++){
blocked[i] = 0;
}
for(int i = 1;i <= m;++i){
cin >> s[i] >> t[i];
used[s[i]] = 1;
x[i] = path(s[i],t[i]);
for(auto j:x[i]){
c[j]++;
}
}
for(int i = 1;i <= m;i++){
bool ok = false;
for(int j = 1;j <= m;j++){
if(blocked[j] || !can(j)) continue;
blocked[j] = 1;
for(auto k:x[j]){
c[k]--;
}
ok = 1;
used[s[j]] = 0;
used[t[j]] = 1;
break;
}
if(!ok){
cout << "No\n";
return;
}
}
cout << "Yes\n";
}
main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int T = 1;
cin >> T;
for (int i = 1; i <= T; i++)
{
test();
}
}
Compilation message (stderr)
jail.cpp: In function 'void test()':
jail.cpp:50:34: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
50 | used[x] = used[y] = c[x] = c[y] = 0;
| ~~~~~^~~~~~~~~~
jail.cpp: At global scope:
jail.cpp:86:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
86 | main()
| ^~~~
# | 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... |