Submission #888440

#TimeUsernameProblemLanguageResultExecution timeMemory
8884408pete8Spring cleaning (CEOI20_cleaning)C++17
100 / 100
169 ms27164 KiB
#include<iostream> #include<stack> #include<map> #include<vector> #include<string> #include<unordered_map> #include <queue> #include<cstring> #include <cassert> #include<limits.h> #include<cmath> #include<set> #include<algorithm> #include<bitset> #include <iomanip> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; #define ll long long #define f first #define endl "\n" #define s second #define pii pair<int,int> #define ppii pair<int,pii> #define vi vector<int> #define pb push_back //#define p push #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define F(n) for(int i=0;i<n;i++) #define lb lower_bound #define ub upper_bound #define fastio ios::sync_with_stdio(false);cin.tie(NULL); using namespace std; #pragma GCC optimize ("03,unroll-loops") #define int long long const int mxn=1e5+5,inf=1e9,lg=30,mod=1e9+7; int n,q; vector<int>adj[mxn+10]; int val[mxn+10],up[mxn+10],cnt=0; bool leaf[mxn+10],yes[mxn+10]; int tin[mxn+10],tout[mxn+10],hvychild[mxn+10],hvypa[mxn+10],sz[mxn+10],rev[mxn+10]; int t=0; int pos[mxn+10]; void dfs(int cur,int p){ sz[cur]=1; for(auto i:adj[cur]){ if(i==p)continue; dfs(i,cur); up[i]=cur; val[cur]+=val[i]; sz[cur]+=sz[i]; } if(val[cur]%2)val[cur]=1; else val[cur]=2; if(cur==1)val[cur]=0; } void hld(int cur,int p){ tin[cur]=t++; rev[tin[cur]]=cur; if(p!=-1&&hvychild[p]==cur)hvypa[cur]=hvypa[p]; else hvypa[cur]=cur; for(auto i:adj[cur])if(i!=p&&sz[hvychild[cur]]<sz[i])hvychild[cur]=i; if(hvychild[cur])hld(hvychild[cur],cur); for(auto i:adj[cur])if(i!=p&&i!=hvychild[cur])hld(i,cur); } struct seg{ int v1[4*mxn+10],v2[4*mxn+10]; int lazy[4*mxn+10]; void push(int l,int r,int node){ if(!lazy[node])return; if(lazy[node]%2)swap(v1[node],v2[node]); if(l!=r)lazy[node<<1]=lazy[(node<<1)^1]+=lazy[node]; lazy[node]=0; } void update(int l,int r,int node,int ql,int qr){ push(l,r,node); if(l>qr||r<ql)return; if(ql<=l&&r<=qr){ lazy[node]++; push(l,r,node); return; } int mid=l+(r-l)/2; update(l,mid,node<<1,ql,qr); update(mid+1,r,(node<<1)^1,ql,qr); v1[node]=v1[node<<1]+v1[(node<<1)^1]; v2[node]=v2[node<<1]+v2[(node<<1)^1]; } void build(int l,int r,int node){ if(l==r){ if(val[rev[l]]==1)v1[node]++; else if(val[rev[l]]==2)v2[node]++; return; } int mid=l+(r-l)/2; build(l,mid,node<<1); build(mid+1,r,(node<<1)^1); v1[node]=v1[node<<1]+v1[(node<<1)^1]; v2[node]=v2[node<<1]+v2[(node<<1)^1]; } }st; void solve(int cur){ while(hvypa[cur]!=1){ st.update(0,n-1,1,tin[hvypa[cur]],tin[cur]); cur=up[hvypa[cur]]; } st.update(0,n-1,1,tin[hvypa[cur]],tin[cur]); } int32_t main(){ fastio cin>>n>>q; for(int i=0;i<n-1;i++){ int a,b;cin>>a>>b; adj[a].pb(b); adj[b].pb(a); } for(int i=1;i<=n;i++){ if(adj[i].size()==1){ leaf[i]=true; if(i!=1)val[i]++; cnt++; } } dfs(1,-1); hld(1,-1); st.build(0,n-1,1); while(q--){ int m;cin>>m; vector<int>v(m); for(int i=0;i<m;i++){ cin>>v[i]; if(!leaf[v[i]]||yes[v[i]])solve(v[i]),cnt++; else yes[v[i]]=true; } for(auto i:v)yes[i]=false; cout<<((cnt%2)?-1:st.v1[1]+(2*st.v2[1])+m)<<'\n'; for(auto i:v){ if(!leaf[i]||yes[i])solve(i),cnt--; else yes[i]=true; } for(auto i:v)yes[i]=false; } 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...