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;
const int maxn = 1e5 + 2e4 + 10;
typedef pair<int,int> ii;
#define ff first
#define ss second
int n,m;
vector <int> adj[maxn];
int par[18][maxn];
int in[maxn],out[maxn],cnt;
bool dau[maxn];
vector <int> d;
ii num[maxn], a[maxn];
void dfs(int x, int p)
{
par[0][x]=p;
in[x]=++cnt;
for (int i=1; i<=17; i++) par[i][x]=par[i-1][par[i-1][x]];
for (int i:adj[x])
if (i!=p)
dfs(i,x);
out[x]=cnt;
}
bool check(int u, int v)
{
return in[u]<=in[v]&&out[v]<=out[u];
}
int lca(int u, int v)
{
if (check(u,v)) return u;
if (check(v,u)) return v;
for (int i=17; i>=0; i--)
if (!check(par[i][u],v)) u=par[i][u];
return par[0][u];
}
void upd(int u, int v, int type)
{
// int it=1;
while (v!=u)
{
// it++; if (it==10) break;
// cerr<<u<<' '<<v<<endl;
if (type==1) num[v].ff++;
else num[v].ss++;
v=par[0][v];
}
}
bool check_mid(int u, int v, int mid)
{
return check(u,mid)&&check(mid,v);
}
bool check_path(int u, int v, int x, int y)
{
/**
u x y v
**/
int p=lca(u,v);
if ((check_mid(p,u,x)||check_mid(p,v,x))&&(check_mid(p,u,y)||check_mid(p,v,y))) return 1;
return 0;
}
void xuly()
{
cin>>n;
for (int i=1; i<=n; i++) adj[i].clear(), in[i]=out[i]=0, num[i]={0,0}; cnt=0;
for (int i=1; i<n; i++)
{
int u,v; cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1,1);
cin>>m; d.clear();
for (int i=1; i<=m; i++) cin>>a[i].ff>>a[i].ss, d.push_back(i);
do
{
for (int i=1; i<=m; i++) dau[a[i].ff] = 1;
bool ccheck = 1;
for (int i:d)
{
int u = a[i].ff; int v=a[i].ss;
dau[u] = 0;
int p=lca(u,v);
while (u!=p)
{
if (dau[u]) ccheck = 0;
u=par[0][u];
}
if (dau[p]) ccheck=0;
while (v!=p)
{
if (dau[v]) ccheck=0;
v=par[0][v];
}
dau[v] = 1;
if (!ccheck) break;
}
if (!ccheck) continue;
cout<<"Yes\n";
return;
}
while (next_permutation(d.begin(),d.end()));
cout<<"No\n";
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int tt; cin>>tt;
while (tt--) xuly();
}
Compilation message (stderr)
jail.cpp: In function 'void xuly()':
jail.cpp:64:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
64 | for (int i=1; i<=n; i++) adj[i].clear(), in[i]=out[i]=0, num[i]={0,0}; cnt=0;
| ^~~
jail.cpp:64:76: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
64 | for (int i=1; i<=n; i++) adj[i].clear(), in[i]=out[i]=0, num[i]={0,0}; cnt=0;
| ^~~
# | 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... |