Submission #971890

#TimeUsernameProblemLanguageResultExecution timeMemory
971890happy_nodeJail (JOI22_jail)C++17
0 / 100
2144 ms1048576 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], dep[MX]; void dfs(int v, int p) { par[v]=p; for(auto u:adj[v]) { if(u==p) continue; dep[u]=dep[v]+1; 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, dep[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; dfs(1,0); for(int i=0;i<M;i++) { cin>>S[i]>>T[i]; vector<int> L,R; int u=S[i],v=T[i]; while(dep[u]>dep[v]) { L.push_back(u); u=par[u]; } while(dep[v]>dep[u]) { R.push_back(v); v=par[v]; } while(u!=v) { L.push_back(u); R.push_back(v); u=par[u]; v=par[v]; } for(auto x:L) path[i].push_back(x); path[i].push_back(S[i]); reverse(R.begin(),R.end()); for(auto x:R) path[i].push_back(x); } 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...