답안 #578132

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
578132 2022-06-16T05:36:03 Z amunduzbaev Spring cleaning (CEOI20_cleaning) C++17
26 / 100
139 ms 23616 KB
#include "bits/stdc++.h"
using namespace std;
 
#define ar array
typedef int64_t ll;

const int N = 1e5 + 5;
vector<int> edges[N];
int sub[N], add[N], is[N];
int d[N], par[N][20], pref[N];
int tin[N], tout[N], t, res;

void dfs(int u, int p = -1){
	tin[u] = ++t;
	for(int j=1;j<20;j++){
		par[u][j] = par[par[u][j-1]][j-1];
	}
	for(auto x : edges[u]){
		if(x == p) continue;
		d[x] = d[u] + 1;
		par[x][0] = u;
		dfs(x, u);
		sub[u] += sub[x];
	}
	
	if(!sub[u]) sub[u] = 1, is[u] = 1;
	tout[u] = t;
}

void dfs2(int u, int p = -1){
	sub[u] &= 1;
	res += (!sub[u]);
	pref[u] += sub[u];
	for(auto x : edges[u]){
		if(x == p) continue;
		pref[x] = pref[u];
		dfs2(x, u);
	}
}

bool isp(int a, int b) { return (tin[a] <= tin[b] && tin[b] <= tout[a]); }
int lca(int a, int b){
	if(isp(a, b)) return a;
	if(isp(b, a)) return b;
	for(int j=19;~j;j--){
		if(!isp(par[a][j], b)) a = par[a][j];
	}
	
	return par[a][0];
}

int solve(vector<int> t){
	sort(t.begin(), t.end());
	t.erase(unique(t.begin(), t.end()), t.end());
	sort(t.begin(), t.end(), [&](int a, int b){
		return tin[a] < tin[b];
	});
	int m = t.size();
	for(int i=1;i<m;i++){
		t.push_back(lca(t[i-1], t[i]));
	}
	
	sort(t.begin(), t.end());
	t.erase(unique(t.begin(), t.end()), t.end());
	sort(t.begin(), t.end(), [&](int a, int b){
		return tin[a] < tin[b];
	});
	
	int tot = 0;
	function<int(int)> dfs = [&](int i){
		int mx = i + 1, u = t[i];
		while(mx < (int)t.size() && tin[t[mx]] < tout[t[i]]){
			int x = t[mx];
			mx = dfs(mx);
			
			if(add[x] & 1){
				tot -= (d[x] - d[u] - pref[x] + pref[u]);
				tot += (pref[x] - pref[u]);
			}
			
			add[u] += add[x];
		}
		
		if(!i && (add[u]&1)){
			tot -= (d[u] - pref[u]);
			tot += pref[u];
		}
		
		return mx;
	};
	dfs(0);
	for(auto x : t) add[x] = 0;
	return tot;
}

signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	int n, q; cin>>n>>q;
	for(int i=1;i<n;i++){
		int a, b; cin>>a>>b;
		edges[a].push_back(b);
		edges[b].push_back(a);
	}
	int r = -1;
	for(int i=1;i<=n;i++){
		if((int)edges[i].size() > 1) { r = i; break; }
	}
	
	assert(~r);
	
	par[r][0] = r, d[r] = 1;
	dfs(r);
	dfs2(r);
	//~ cout<<res<<"\n";
	int cnt = accumulate(is + 1, is + n + 1, 0);
	
	for(int i=0;i<q;i++){
		int d; cin>>d;
		//~ cout<<"here"<<endl;
		vector<int> t;
		int tmp = 0;
		for(int x=0;x<d;x++){
			int p; cin>>p;
			//~ nw[p]++;
			t.push_back(p);
			add[p]++, tmp++;
			if(is[p]){
				is[p]--;
				cnt--;
				add[p]--;
			}
		}
		
		if((cnt + d) & 1){
			cout<<-1<<"\n";
		} else {
			
			//~ cout<<n - 1<<" "<<tmp<<" "<<res<<" "<<solve(t)<<"\n";
			cout<<n - 1 + tmp + res + solve(t) - 1<<"\n";
		}
		
		for(auto p : t){
			add[p] = 0;
			if((int)edges[p].size() == 1 && !is[p]){
				is[p] = 1;
				cnt++;
			}
		}
	}
}

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Incorrect 55 ms 5460 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 14 ms 3792 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 4564 KB Output is correct
2 Correct 10 ms 4312 KB Output is correct
3 Correct 61 ms 23616 KB Output is correct
4 Correct 86 ms 22888 KB Output is correct
5 Correct 61 ms 21588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 41 ms 6100 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 11956 KB Output is correct
2 Incorrect 120 ms 11664 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 119 ms 16932 KB Output is correct
2 Correct 139 ms 19608 KB Output is correct
3 Correct 133 ms 19716 KB Output is correct
4 Correct 120 ms 19148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Incorrect 55 ms 5460 KB Output isn't correct
3 Halted 0 ms 0 KB -