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 ll = long long;
using namespace std;
#define pll pair <ll,ll>
#define fi first
#define se second
#define sz(a) (ll((a).size()))
#define BIT(mask,i) (((mask) >> (i))&1)
#define MASK(i) (1LL << (i))
#define MP make_pair
ll t,n,m;
const ll MAXN = 1.2e5;
vector <ll> g1[MAXN+100];
vector <ll> g2[MAXN+100];
pll a[MAXN+100];
ll st[MAXN+100];
ll trace[MAXN+100];
bool in[MAXN+100];
bool in_st[MAXN+100];
bool detect_cycle = 0;
vector <ll> find_path(ll x,ll y){
for (ll i = 1;i <= n;i ++)trace[i] = 0;
trace[x] = x;
queue <ll> q;
q.push(x);
while (!q.empty()){
ll u = q.front();
q.pop();
for (auto v:g1[u]){
if (!trace[v]){
trace[v] = u;
q.push(v);
}
}
}
ll cur=y;
// cout<<"WOW "<<endl;
vector <ll> res;
do{
res.push_back(cur);
cur = trace[cur];
// cout<<cur<<endl;
}while (cur != x);
// cout<<"WHAT "<<endl;
reverse(res.begin(),res.end());
return res;
}
void dfs(ll u){
in[u] = 1;
in_st[u] = 1;
for (auto v:g2[u]){
if (in_st[v])detect_cycle = 1;
if (in[v])continue;
dfs(v);
}
in_st[u] = 0;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
cin>>t;
while (t--){
cin>>n;
detect_cycle = 0;
for (ll i = 1;i <= n;i ++){
g1[i].clear();
g2[i].clear();
st[i] = 0;
in[i] = in_st[i] = 0;
}
for (ll i = 1;i < n;i ++){
ll u,v;
cin>>u>>v;
g1[u].push_back(v);
g1[v].push_back(u);
}
cin>>m;
for (ll i = 1;i <= m;i ++){
cin>>a[i].fi>>a[i].se;
st[a[i].fi] = i;
}
for (ll i = 1;i <= m;i ++){
for (auto x:find_path(a[i].fi,a[i].se)){
if (st[x]){
if (st[x] != i)g2[i].push_back(st[x]);
}
}
}
// cout<<"OK"<<endl;
for (ll i = 1;i <= m;i ++){
if (!in[i])dfs(i);
}
if (detect_cycle)cout<<"No\n";
else cout<<"Yes\n";
}
}
# | 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... |