Submission #949259

#TimeUsernameProblemLanguageResultExecution timeMemory
949259hotboy2703Jail (JOI22_jail)C++14
61 / 100
5062 ms21064 KiB
#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]; ll en[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; en[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; en[a[i].se] = i; } for (ll i = 1;i <= m;i ++){ set <ll> s; for (auto x:find_path(a[i].fi,a[i].se)){ if (st[x]){ if (st[x] != i)g2[i].push_back(st[x]); } if (en[x]){ if (en[x] != i){g2[en[x]].push_back(i);s.insert(en[x]);} } } for (auto x:g2[i])if (s.find(x) != s.end())detect_cycle=1; } // 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...