Submission #888429

# Submission time Handle Problem Language Result Execution time Memory
888429 2023-12-17T11:43:53 Z 8pete8 Spring cleaning (CEOI20_cleaning) C++17
9 / 100
120 ms 18128 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]==0||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];
  bool lazy[4*mxn+10];
  void push(int l,int r,int node){
    if(!lazy[node])return;
    swap(v1[node],v2[node]);
    if(l!=r)lazy[node<<1]=lazy[(node<<1)^1]=true;
    lazy[node]=false;
  }
  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]=true;
      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 2 ms 8536 KB Output is correct
2 Incorrect 67 ms 10588 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 34 ms 9048 KB Output is correct
2 Correct 34 ms 9052 KB Output is correct
3 Correct 25 ms 16528 KB Output is correct
4 Correct 47 ms 16592 KB Output is correct
5 Correct 48 ms 18128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 31 ms 9820 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 49 ms 11356 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 83 ms 15188 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 120 ms 17772 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Incorrect 67 ms 10588 KB Output isn't correct
3 Halted 0 ms 0 KB -