Submission #971884

#TimeUsernameProblemLanguageResultExecution timeMemory
971884happy_nodeJail (JOI22_jail)C++17
61 / 100
5093 ms282468 KiB
#include <bits/stdc++.h> using namespace std; const int MX=4e5+5; int Q,N,M; int S[MX], T[MX]; vector<int> path[MX]; // store the path between Si and Ti vector<int> adj[MX]; int par[MX]; void dfs(int v, int p) { par[v]=p; for(auto u:adj[v]) { if(u==p) continue; dfs(u,v); } } vector<int> ADJ[MX]; int deg[MX]; bool f(int i, int x) { for(auto y:path[i]) if(y==x) return true; return false; } int cntT[MX], cntS[MX]; int main() { cin.tie(0); ios_base::sync_with_stdio(0); cin>>Q; while(Q--) { cin>>N; for(int i=1;i<=N;i++) adj[i].clear(), cntT[i]=0, cntS[i]=0; for(int i=0;i<N-1;i++) { int u,v; cin>>u>>v; adj[u].push_back(v); adj[v].push_back(u); } cin>>M; for(int i=0;i<2*N+M;i++) path[i].clear(), ADJ[i].clear(), deg[i]=0; for(int i=0;i<M;i++) { cin>>S[i]>>T[i]; dfs(S[i],S[i]); int p=T[i]; while(p!=S[i]) { path[i].push_back(p); p=par[p]; } path[i].push_back(S[i]); } for(int i=0;i<M;i++) { cntS[S[i]]+=1; cntT[T[i]]+=1; } set<pair<int,int>> st; for(int i=0;i<M;i++) { cntS[S[i]]-=1; cntT[T[i]]-=1; for(auto x:path[i]) { if(cntS[x]) { ADJ[M-1+x].push_back(i); deg[i]+=1; } if(cntT[x]) { ADJ[i].push_back(M-1+x+N); deg[M-1+x+N]+=1; } } ADJ[i].push_back(M-1+S[i]); deg[M-1+S[i]]+=1; ADJ[M-1+T[i]+N].push_back(i); deg[i]+=1; cntS[S[i]]+=1; cntT[T[i]]+=1; } queue<int> q; for(int i=0;i<2*N+M;i++) { if(!deg[i]) { q.push(i); } } int ans=0; while(!q.empty()) { auto v=q.front(); q.pop(); ans+=1; for(auto u:ADJ[v]) { deg[u]--; if(!deg[u]) q.push(u); } } cout<<(ans==2*N+M?"Yes":"No")<<'\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...