답안 #1055387

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1055387 2024-08-12T18:36:15 Z TAhmed33 Spring cleaning (CEOI20_cleaning) C++
100 / 100
282 ms 24404 KB
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
typedef long long ll;
const int MAXN = 2e5 + 25;
#define mid ((l + r) >> 1)
#define tl (node << 1)
#define tr (node << 1 | 1)
struct SegmentTree {
	int tree[MAXN << 2], lazy[MAXN << 2];
	void prop (int l, int r, int node) {
		if (l != r) {
			lazy[tl] ^= lazy[node];
			lazy[tr] ^= lazy[node];
		}
		if (lazy[node]) {
			tree[node] = r - l + 1 - tree[node];
		}
		lazy[node] = 0;
	}
	void update (int l, int r, int a, int b, int node) {
		prop(l, r, node);
		if (l > b || r < a) return;
		if (l >= a && r <= b) {
			lazy[node] ^= 1;
			prop(l, r, node);
			return;
		}
		update(l, mid, a, b, tl); update(mid + 1, r, a, b, tr);
		tree[node] = tree[tl] + tree[tr];
	}
	int get (int l, int r, int a, int b, int node) {
		prop(l, r, node);
		if (l > b || r < a) return 0;
		if (l >= a && r <= b) return tree[node];
		return get(l, mid, a, b, tl) + get(mid + 1, r, a, b, tr);
	}
} cur;
int n, q;
vector <int> adj[MAXN];
int sze[MAXN], nxt[MAXN], dep[MAXN], in[MAXN], out[MAXN], tt;
int p[MAXN];
void fix (int pos, int par) {
	p[pos] = par;
	sze[pos] = 1;
	if (par != 0) {
		adj[pos].erase(find(adj[pos].begin(), adj[pos].end(), par));
	}
	for (auto &j : adj[pos]) {
		fix(j, pos);
		sze[pos] += sze[j];
		if (sze[j] > sze[adj[pos][0]]) {
			swap(j, adj[pos][0]);
		}
	}
}
int leaf[MAXN];
void dfs (int pos) {
	in[pos] = ++tt;
	if (adj[pos].empty()) {
		leaf[pos] = 1;
	}
	for (auto j : adj[pos]) {
		dep[j] = 1 + dep[pos];
		nxt[j] = (j == adj[pos][0] ? nxt[pos] : j);
		dfs(j);
		leaf[pos] += leaf[j];
	}
	out[pos] = tt;
}
void upd (int x) {
	while (x) {
		cur.update(1, n, in[nxt[x]], in[x], 1);
		x = p[nxt[x]];
	}
}
void solve () {
	cin >> n >> q;
	for (int i = 1; i < n; i++) {
		int a, b; cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	int root = -1;
	for (int i = 1; i <= n; i++) {
		if (adj[i].size() >= 2) {
			root = i;
			break;
		}
	}
	fix(root, 0);
	nxt[root] = root;
	dfs(root);
	for (int i = 1; i <= n; i++) {
		if (leaf[i] & 1) {
			cur.update(1, n, in[i], in[i], 1);
		}
	}
	while (q--) {
		int d; cin >> d;
		vector <int> c(d);
		for (auto &i : c) {
			cin >> i;
		}
		for (auto i : c) {
			if (leaf[i] == 1 && adj[i].empty()) {
				leaf[i]--;
				upd(i);
			}
			upd(i);
/*		for (int i = 1; i <= n; i++) {
			cout << cur.get(1, n, in[i], in[i], 1) << " ";
		}
		cout << '\n';*/
		}
/*		for (int i = 1; i <= n; i++) {
			cout << cur.get(1, n, in[i], in[i], 1) << " ";
		}
		cout << '\n';*/
		int z = cur.get(1, n, in[root], in[root], 1);
		if (z == 1) {
			cout << -1 << '\n';
		} else {
			int cnt = d;
			cnt += cur.get(1, n, 1, n, 1);
			cnt += 2 * (n - 1 - cur.get(1, n, 1, n, 1));
			cout << cnt << '\n';
		}
		for (auto i : c) {
			upd(i);
			if (leaf[i] == 0 && adj[i].empty()) {
				leaf[i]++;
				upd(i);
			}
		}
	}
}		
signed main () {
	ios::sync_with_stdio(0); cin.tie(0);
	int tc = 1; //cin >> tc;
	while (tc--) solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10584 KB Output is correct
2 Correct 122 ms 11736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 11100 KB Output is correct
2 Correct 30 ms 11100 KB Output is correct
3 Correct 31 ms 16212 KB Output is correct
4 Correct 91 ms 15308 KB Output is correct
5 Correct 114 ms 16332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 11608 KB Output is correct
2 Correct 26 ms 11612 KB Output is correct
3 Correct 35 ms 23344 KB Output is correct
4 Correct 70 ms 22868 KB Output is correct
5 Correct 32 ms 22364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 12380 KB Output is correct
2 Correct 45 ms 11732 KB Output is correct
3 Correct 7 ms 11612 KB Output is correct
4 Correct 7 ms 12124 KB Output is correct
5 Correct 9 ms 12380 KB Output is correct
6 Correct 44 ms 12240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 192 ms 13904 KB Output is correct
2 Correct 282 ms 13660 KB Output is correct
3 Correct 234 ms 12120 KB Output is correct
4 Correct 265 ms 13708 KB Output is correct
5 Correct 271 ms 13716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 216 ms 16212 KB Output is correct
2 Correct 113 ms 20800 KB Output is correct
3 Correct 122 ms 20988 KB Output is correct
4 Correct 121 ms 20304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10584 KB Output is correct
2 Correct 122 ms 11736 KB Output is correct
3 Correct 30 ms 11100 KB Output is correct
4 Correct 30 ms 11100 KB Output is correct
5 Correct 31 ms 16212 KB Output is correct
6 Correct 91 ms 15308 KB Output is correct
7 Correct 114 ms 16332 KB Output is correct
8 Correct 26 ms 11608 KB Output is correct
9 Correct 26 ms 11612 KB Output is correct
10 Correct 35 ms 23344 KB Output is correct
11 Correct 70 ms 22868 KB Output is correct
12 Correct 32 ms 22364 KB Output is correct
13 Correct 57 ms 12380 KB Output is correct
14 Correct 45 ms 11732 KB Output is correct
15 Correct 7 ms 11612 KB Output is correct
16 Correct 7 ms 12124 KB Output is correct
17 Correct 9 ms 12380 KB Output is correct
18 Correct 44 ms 12240 KB Output is correct
19 Correct 192 ms 13904 KB Output is correct
20 Correct 282 ms 13660 KB Output is correct
21 Correct 234 ms 12120 KB Output is correct
22 Correct 265 ms 13708 KB Output is correct
23 Correct 271 ms 13716 KB Output is correct
24 Correct 216 ms 16212 KB Output is correct
25 Correct 113 ms 20800 KB Output is correct
26 Correct 122 ms 20988 KB Output is correct
27 Correct 121 ms 20304 KB Output is correct
28 Correct 177 ms 14676 KB Output is correct
29 Correct 102 ms 20820 KB Output is correct
30 Correct 104 ms 17612 KB Output is correct
31 Correct 71 ms 24404 KB Output is correct
32 Correct 274 ms 14928 KB Output is correct
33 Correct 112 ms 19024 KB Output is correct
34 Correct 118 ms 20564 KB Output is correct
35 Correct 123 ms 20460 KB Output is correct