Submission #888440

# Submission time Handle Problem Language Result Execution time Memory
888440 2023-12-17T12:02:55 Z 8pete8 Spring cleaning (CEOI20_cleaning) C++17
100 / 100
169 ms 27164 KB
#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 time Memory Grader output
1 Correct 3 ms 7004 KB Output is correct
2 Correct 71 ms 9832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 8028 KB Output is correct
2 Correct 38 ms 8028 KB Output is correct
3 Correct 28 ms 16804 KB Output is correct
4 Correct 54 ms 17564 KB Output is correct
5 Correct 66 ms 19480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 8712 KB Output is correct
2 Correct 38 ms 9132 KB Output is correct
3 Correct 47 ms 26856 KB Output is correct
4 Correct 129 ms 26700 KB Output is correct
5 Correct 44 ms 23840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 10392 KB Output is correct
2 Correct 29 ms 10108 KB Output is correct
3 Correct 10 ms 9820 KB Output is correct
4 Correct 10 ms 9820 KB Output is correct
5 Correct 13 ms 10844 KB Output is correct
6 Correct 42 ms 10672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 15588 KB Output is correct
2 Correct 149 ms 15348 KB Output is correct
3 Correct 114 ms 10772 KB Output is correct
4 Correct 133 ms 15336 KB Output is correct
5 Correct 137 ms 15364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 153 ms 19484 KB Output is correct
2 Correct 96 ms 23636 KB Output is correct
3 Correct 112 ms 24404 KB Output is correct
4 Correct 107 ms 25552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7004 KB Output is correct
2 Correct 71 ms 9832 KB Output is correct
3 Correct 39 ms 8028 KB Output is correct
4 Correct 38 ms 8028 KB Output is correct
5 Correct 28 ms 16804 KB Output is correct
6 Correct 54 ms 17564 KB Output is correct
7 Correct 66 ms 19480 KB Output is correct
8 Correct 37 ms 8712 KB Output is correct
9 Correct 38 ms 9132 KB Output is correct
10 Correct 47 ms 26856 KB Output is correct
11 Correct 129 ms 26700 KB Output is correct
12 Correct 44 ms 23840 KB Output is correct
13 Correct 55 ms 10392 KB Output is correct
14 Correct 29 ms 10108 KB Output is correct
15 Correct 10 ms 9820 KB Output is correct
16 Correct 10 ms 9820 KB Output is correct
17 Correct 13 ms 10844 KB Output is correct
18 Correct 42 ms 10672 KB Output is correct
19 Correct 90 ms 15588 KB Output is correct
20 Correct 149 ms 15348 KB Output is correct
21 Correct 114 ms 10772 KB Output is correct
22 Correct 133 ms 15336 KB Output is correct
23 Correct 137 ms 15364 KB Output is correct
24 Correct 153 ms 19484 KB Output is correct
25 Correct 96 ms 23636 KB Output is correct
26 Correct 112 ms 24404 KB Output is correct
27 Correct 107 ms 25552 KB Output is correct
28 Correct 105 ms 16012 KB Output is correct
29 Correct 101 ms 24744 KB Output is correct
30 Correct 53 ms 20672 KB Output is correct
31 Correct 99 ms 27164 KB Output is correct
32 Correct 169 ms 16480 KB Output is correct
33 Correct 107 ms 21588 KB Output is correct
34 Correct 124 ms 23636 KB Output is correct
35 Correct 152 ms 24564 KB Output is correct