Submission #977014

#TimeUsernameProblemLanguageResultExecution timeMemory
977014DAleksaSpring cleaning (CEOI20_cleaning)C++17
0 / 100
59 ms20288 KiB
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10, LOG = 17; int n, q; vector<int> g[N], fake[N]; int timer, tin[N], tout[N]; int up[N][LOG]; int cnt[N], even[N], odd[N]; int dep[N]; int root; bool mark[N]; bool is_ancestor(int u, int v) { return tin[u] <= tin[v] && tout[u] >= tout[v]; } int LCA(int u, int v) { if(is_ancestor(u, v)) return u; if(is_ancestor(v, u)) return v; for(int j = LOG - 1; j >= 0; j--) { if(up[u][j] == 0) continue; if(is_ancestor(up[u][j], v)) continue; u = up[u][j]; } return up[u][0]; } int ans; void dfs(int u, int par) { if(g[u].size() == 1) cnt[u]++; tin[u] = ++timer; for(int v : g[u]) { if(v == par) continue; dep[v] = dep[u] + 1; dfs(v, u); up[v][0] = u; cnt[u] += cnt[v]; } tout[u] = ++timer; } void dfs2(int u, int par) { if(cnt[u] % 2 == 0) { even[u]++; ans++; } else odd[u]++; for(int v : g[u]) { if(v == par) continue; even[v] = even[u]; odd[v] = odd[u]; dfs2(v, u); } } int sz[N]; int add; void fdfs(int u, int par) { sz[u] = 1; for(int v : fake[u]) { fdfs(v, u); sz[u] ^= sz[v]; if(sz[v] == 1) { add -= even[v] - even[u]; add += odd[v] - odd[u]; } } } void clr(int u, int par) { for(int v : fake[u]) clr(v, u); fake[u].clear(); mark[u] = false; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> q; for(int i = 1; i < n; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } for(int i = 1; i <= n; i++) { if(g[i].size() > 1) { root = i; break; } } int leaves = 0; for(int i = 1; i <= n; i++) leaves += (g[i].size() == 1); dfs(root, 0); dfs2(root, 0); for(int j = 1; j < LOG; j++) for(int i = 1; i <= n; i++) up[i][j] = up[up[i][j - 1]][j - 1]; // cout << ans << "\n"; while(q--) { int m; cin >> m; vector<int> all; int more = 0; for(int i = 0; i < m; i++) { int x; cin >> x; if(g[x].size() == 1) continue; more++; all.push_back(x); } if((leaves + more) % 2 == 1) { cout << "-1\n"; continue; } vector<int> nw; while(all.size() > 0) { sort(all.begin(), all.end(), [&](int u, int v) { return tout[u] < tout[v]; }); nw.clear(); for(int i = 0; i < all.size(); i++) { int u = all[i]; int par = root; if(i > 0) par = LCA(all[i - 1], all[i]); if(i < all.size() - 1) { int lca = LCA(all[i], all[i + 1]); if(dep[lca] > dep[par]) par = lca; } if(par == u) continue; fake[par].push_back(u); mark[u] = true; nw.push_back(par); } all.clear(); for(int i = 0; i < nw.size(); i++) { if(mark[nw[i]]) continue; all.push_back(nw[i]); } } add = 0; // for(int i = 1; i <= n; i++) { // cout << i << ": "; // for(int j : fake[i]) cout << j << " "; // cout << "\n"; // } fdfs(root, 0); cout << n - 2 + m + ans + add << "\n"; clr(root, 0); } return 0; }

Compilation message (stderr)

cleaning.cpp: In function 'int main()':
cleaning.cpp:119:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |             for(int i = 0; i < all.size(); i++) {
      |                            ~~^~~~~~~~~~~~
cleaning.cpp:123:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |                 if(i < all.size() - 1) {
      |                    ~~^~~~~~~~~~~~~~~~
cleaning.cpp:133:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |             for(int i = 0; i < nw.size(); i++) {
      |                            ~~^~~~~~~~~~~
#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...