Submission #963494

#TimeUsernameProblemLanguageResultExecution timeMemory
963494Darren0724Spring cleaning (CEOI20_cleaning)C++17
9 / 100
1090 ms19432 KiB
#include <bits/stdc++.h> using namespace std; const int N=200005; vector<int> adj[N],big(N,-1),sz(N,0),pa(N),deg(N,0); vector<vector<int>> path(1); void dfs(int k){ if(deg[k]==1)sz[k]=1; for(int j:adj[k]){ if(j==pa[k])continue; pa[j]=k; dfs(j); sz[k]+=sz[j]; big[k]=(big[k]==-1||sz[big[k]]<sz[j]?j:big[k]); } } void dfs2(int k){ path.back().push_back(k); if(big[k]!=-1){ dfs2(big[k]); } for(int j:adj[k]){ if(j!=big[k]&&j!=pa[k]){ path.push_back(vector<int>()); dfs2(j); } } } void add(int p,int x){ if(x==1&&deg[p]==1){ sz[p]--; } if(x==-1&&deg[p]==1){ sz[p]++; } for(int i=p;i>0;i=pa[i]){ sz[i]+=x; } } int32_t main(){ int n,q;cin>>n>>q; for(int i=1;i<n;i++){ int a,b;cin>>a>>b; adj[a].push_back(b); adj[b].push_back(a); } int rt=1; for(int i=1;i<=n;i++){ deg[i]=adj[i].size(); if(deg[i]>=2)rt=i; } dfs(rt); dfs2(rt); for(int i=0;i<q;i++){ int t;cin>>t; int ans=n-1+t; vector<int> a(t); for(int j=0;j<t;j++){ cin>>a[j]; add(a[j],1); deg[a[j]]++; } int leaves=t; for(int j=1;j<=n;j++){ if(j==rt)continue; leaves+=(deg[j]==1); ans+=(sz[j]%2==0); } for(int j=0;j<t;j++){ add(a[j],-1); deg[a[j]]--; } if(leaves&1){ cout<<-1<<endl; } else{ cout<<ans<<endl; } } return 0; }
#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...