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>
#define ll long long
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
using namespace std;
const ll maxn=120005;
pll sp[20][maxn*2];
vector <pll> euler;
vector <ll> A[maxn];
ll check[maxn], cnt[maxn];
ll dis[maxn], lca[maxn], u[maxn], v[maxn];
void dfs(ll u, ll pa)
{
lca[u]=euler.size()+1;
euler.pb({lca[u], u});
for (ll v:A[u])
{
if (v==pa)
continue;
dis[v]=dis[u]+1, dfs(v, u);
euler.pb({lca[u], u});
}
}
void initsparse()
{
ll n=euler.size();
for (ll i=0; i<n; i++)
sp[0][i+1]=euler[i];
for (ll i=1; i<20; i++)
for (ll j=1; j+(1<<i)<=n+1; j++)
sp[i][j]=min(sp[i-1][j], sp[i-1][j+(1<<i-1)]);
}
ll dist(ll u, ll v)
{
ll l=lca[u], r=lca[v];
if (l>r) swap(l, r);
ll j=__lg(r-l+1), p=min(sp[j][l], sp[j][r-(1<<j)+1]).se;
return dis[u]+dis[v]-dis[p]*2;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll t; cin >> t;
while (t--)
{
ll n; cin >> n;
for (ll i=1; i<=n; i++)
A[i].clear(), cnt[i]=0, check[i]=0;
euler.clear();
for (ll i=1; i<n; i++)
{
ll u, v; cin >> u >> v;
A[u].pb(v); A[v].pb(u);
}
dfs(1, 0); initsparse();
ll m; cin >> m;
for (ll i=1; i<=m; i++)
cin >> u[i] >> v[i];
for (ll i=1; i<=m; i++)
for (ll j=1; j<=m; j++)
if (i!=j && dist(u[j], u[i])+dist(u[i], v[j])==dist(u[j], v[j]))
cnt[j]++;
ll ans=0;
while (ans<m)
{
ll c=0;
for (ll i=1; i<=m; i++)
if (!check[i] && cnt[i]==0)
{c=i; break;}
if (!c) break;
check[c]=1; ans++;
for (ll i=1; i<=m; i++)
{
if (c!=i && dist(u[i], u[c])+dist(u[c], v[i])==dist(u[i], v[i]))
cnt[i]--;
if (c!=i && dist(u[i], v[c])+dist(v[c], v[i])==dist(u[i], v[i]))
cnt[i]++;
}
}
if (ans==m) cout << "Yes\n";
else cout << "No\n";
}
}
Compilation message (stderr)
jail.cpp: In function 'void initsparse()':
jail.cpp:38:53: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
38 | sp[i][j]=min(sp[i-1][j], sp[i-1][j+(1<<i-1)]);
| ~^~
# | 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... |