Submission #784444

#TimeUsernameProblemLanguageResultExecution timeMemory
784444t6twotwoSpring cleaning (CEOI20_cleaning)C++17
100 / 100
191 ms26060 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; struct node { int z, o; node() { z = o = 0; } node(int v) { if (v == 0) { z = 1; o = 0; } else { o = 1; z = 0; } } node(int a, int b) { z = a; o = b; } }; node operator+(const node &a, const node &b) { return {a.z + b.z, a.o + b.o}; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, Q; cin >> N >> Q; vector<int> deg(N); vector<vector<int>> adj(N); for (int i = 0; i < N - 1; i++) { int x, y; cin >> x >> y; deg[--x]++; deg[--y]++; adj[x].push_back(y); adj[y].push_back(x); } vector<int> v(N), par(N, -1), dep(N), sz(N); int cnt = 0, leaves = 0; auto init = [&](auto init, int x) -> void { if (deg[x] == 1) { v[x] = 1; leaves++; } if (par[x] != -1) { adj[x].erase(find(adj[x].begin(), adj[x].end(), par[x])); } sz[x] = 1; for (int &y : adj[x]) { par[y] = x; dep[y] = dep[x] + 1; init(init, y); sz[x] += sz[y]; v[x] ^= v[y]; if (sz[y] > sz[adj[x][0]]) { swap(y, adj[x][0]); } } cnt += v[x]; }; init(init, 0); int K = 2 << __lg(N - 1); vector<int> top(N), in(N), lazy(2 * K); vector<node> st(2 * K); int timer = 0; auto dfs = [&](auto dfs, int x) -> void { in[x] = timer++; st[in[x] + K] = v[x]; for (int y : adj[x]) { top[y] = y == adj[x][0] ? top[x] : y; dfs(dfs, y); } }; dfs(dfs, 0); auto apply = [&](int p, int z) { if (z == 0) { return; } swap(st[p].z, st[p].o); lazy[p] ^= 1; }; auto pull = [&](int p) { st[p] = st[p * 2] + st[p * 2 + 1]; }; auto push = [&](int p) { apply(p * 2, lazy[p]); apply(p * 2 + 1, lazy[p]); lazy[p] = 0; }; auto update = [&](auto f, int p, int l, int r, int L, int R) -> void { if (R <= l || r <= L) { return; } if (L <= l && r <= R) { apply(p, 1); return; } int m = (l + r) / 2; push(p); f(f, p * 2, l, m, L, R); f(f, p * 2 + 1, m, r, L, R); pull(p); }; auto query = [&](auto f, int p, int l, int r, int L, int R) -> node { if (R <= l || r <= L) { return {}; } if (L <= l && r <= R) { return st[p]; } int m = (l + r) / 2; push(p); return f(f, p * 2, l, m, L, R) + f(f, p * 2 + 1, m, r, L, R); }; for (int i = K - 1; i; i--) { pull(i); } while (Q--) { int D; cin >> D; vector<int> a(D); for (int &p : a) { cin >> p; p--; if (deg[p]++ != 1) { leaves++; int x = p; while (x != -1) { update(update, 1, 0, K, in[top[x]], in[x] + 1); x = par[top[x]]; } } } if (leaves % 2 == 1) { cout << -1 << "\n"; } else { cout << 2 * (N - 1) - st[1].o + D << "\n"; } for (int p : a) { if (--deg[p] != 1) { leaves--; int x = p; while (x != -1) { update(update, 1, 0, K, in[top[x]], in[x] + 1); x = par[top[x]]; } } } } return 6/22; }

Compilation message (stderr)

cleaning.cpp: In function 'int main()':
cleaning.cpp:107:10: warning: variable 'query' set but not used [-Wunused-but-set-variable]
  107 |     auto query = [&](auto f, int p, int l, int r, int L, int R) -> node {
      |          ^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...