답안 #578143

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
578143 2022-06-16T05:45:40 Z amunduzbaev Spring cleaning (CEOI20_cleaning) C++17
100 / 100
154 ms 24372 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){
				int c = d[x] - d[u], o = pref[x] - pref[u];
				tot = tot + o - (c - o);
			}
			
			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;
		vector<int> t;
		for(int x=0;x<d;x++){
			int p; cin>>p;
			t.push_back(p);
			add[p]++;
			if(is[p]){
				is[p]--;
				cnt--;
				add[p]--;
			}
		}
		
		if((cnt + d) & 1){
			cout<<-1<<"\n";
		} else {
			cout<<n - 1 + d + 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 2 ms 2644 KB Output is correct
2 Correct 56 ms 5572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 3792 KB Output is correct
2 Correct 9 ms 3736 KB Output is correct
3 Correct 52 ms 16860 KB Output is correct
4 Correct 66 ms 13684 KB Output is correct
5 Correct 98 ms 17908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 4544 KB Output is correct
2 Correct 9 ms 4312 KB Output is correct
3 Correct 70 ms 23576 KB Output is correct
4 Correct 78 ms 22852 KB Output is correct
5 Correct 49 ms 21600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 6100 KB Output is correct
2 Correct 27 ms 5460 KB Output is correct
3 Correct 12 ms 5460 KB Output is correct
4 Correct 13 ms 5972 KB Output is correct
5 Correct 14 ms 6096 KB Output is correct
6 Correct 36 ms 5988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 11880 KB Output is correct
2 Correct 98 ms 11648 KB Output is correct
3 Correct 67 ms 7264 KB Output is correct
4 Correct 101 ms 11720 KB Output is correct
5 Correct 99 ms 11808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 94 ms 16960 KB Output is correct
2 Correct 120 ms 19660 KB Output is correct
3 Correct 133 ms 19616 KB Output is correct
4 Correct 114 ms 19024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 56 ms 5572 KB Output is correct
3 Correct 14 ms 3792 KB Output is correct
4 Correct 9 ms 3736 KB Output is correct
5 Correct 52 ms 16860 KB Output is correct
6 Correct 66 ms 13684 KB Output is correct
7 Correct 98 ms 17908 KB Output is correct
8 Correct 15 ms 4544 KB Output is correct
9 Correct 9 ms 4312 KB Output is correct
10 Correct 70 ms 23576 KB Output is correct
11 Correct 78 ms 22852 KB Output is correct
12 Correct 49 ms 21600 KB Output is correct
13 Correct 35 ms 6100 KB Output is correct
14 Correct 27 ms 5460 KB Output is correct
15 Correct 12 ms 5460 KB Output is correct
16 Correct 13 ms 5972 KB Output is correct
17 Correct 14 ms 6096 KB Output is correct
18 Correct 36 ms 5988 KB Output is correct
19 Correct 57 ms 11880 KB Output is correct
20 Correct 98 ms 11648 KB Output is correct
21 Correct 67 ms 7264 KB Output is correct
22 Correct 101 ms 11720 KB Output is correct
23 Correct 99 ms 11808 KB Output is correct
24 Correct 94 ms 16960 KB Output is correct
25 Correct 120 ms 19660 KB Output is correct
26 Correct 133 ms 19616 KB Output is correct
27 Correct 114 ms 19024 KB Output is correct
28 Correct 97 ms 12236 KB Output is correct
29 Correct 118 ms 21396 KB Output is correct
30 Correct 56 ms 18688 KB Output is correct
31 Correct 82 ms 24372 KB Output is correct
32 Correct 102 ms 12984 KB Output is correct
33 Correct 124 ms 17740 KB Output is correct
34 Correct 147 ms 21328 KB Output is correct
35 Correct 154 ms 21380 KB Output is correct