Submission #952864

#TimeUsernameProblemLanguageResultExecution timeMemory
952864GrindMachineSpring cleaning (CEOI20_cleaning)C++17
34 / 100
1051 ms44004 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef long long int ll; typedef long double ld; typedef pair<int,int> pii; typedef pair<ll,ll> pll; #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL) #define pb push_back #define endl '\n' #define sz(a) (int)a.size() #define setbits(x) __builtin_popcountll(x) #define ff first #define ss second #define conts continue #define ceil2(x,y) ((x+y-1)/(y)) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define yes cout << "Yes" << endl #define no cout << "No" << endl #define rep(i,n) for(int i = 0; i < n; ++i) #define rep1(i,n) for(int i = 1; i <= n; ++i) #define rev(i,s,e) for(int i = s; i >= e; --i) #define trav(i,a) for(auto &i : a) template<typename T> void amin(T &a, T b) { a = min(a,b); } template<typename T> void amax(T &a, T b) { a = max(a,b); } #ifdef LOCAL #include "debug.h" #else #define debug(x) 42 #endif /* */ const int MOD = 1e9 + 7; const int N = 1e5 + 5; const int inf1 = int(1e9) + 5; const ll inf2 = ll(1e18) + 5; vector<ll> adj[N]; struct lca_algo { // LCA template (for graphs with 1-based indexing) int LOG = 1; vector<int> depth; vector<vector<int>> up; vector<int> tin, tout; int timer = 1; lca_algo() { } lca_algo(int n, int r) { lca_init(n,r); } void lca_init(int n, int r) { while ((1 << LOG) < n) LOG++; up = vector<vector<int>>(n + 1, vector<int>(LOG, r)); depth = vector<int>(n + 1); tin = vector<int>(n + 1); tout = vector<int>(n + 1); lca_dfs(r, -1); } void lca_dfs(int node, int par) { tin[node] = timer++; trav(child, adj[node]) { if (child == par) conts; up[child][0] = node; rep1(j, LOG - 1) { up[child][j] = up[up[child][j - 1]][j - 1]; } depth[child] = depth[node] + 1; lca_dfs(child, node); } tout[node] = timer-1; } int lift(int u, int k) { rep(j, LOG) { if (k & (1 << j)) { u = up[u][j]; } } return u; } int query(int u, int v) { if (depth[u] < depth[v]) swap(u, v); int k = depth[u] - depth[v]; u = lift(u, k); if (u == v) return u; rev(j, LOG - 1, 0) { if (up[u][j] != up[v][j]) { u = up[u][j]; v = up[v][j]; } } u = up[u][0]; return u; } int get_dis(int u, int v) { int lca = query(u, v); return depth[u] + depth[v] - 2 * depth[lca]; } bool is_ances(int u, int v){ return tin[u] <= tin[v] and tout[u] >= tout[v]; } }; vector<ll> depth(N); vector<ll> leaves(N); vector<ll> dp(N); vector<ll> single(N); vector<bool> leaf(N); vector<ll> leaf_sum(N); void dfs1(ll u, ll p){ leaves[u] = 0; ll children = 0; trav(v,adj[u]){ if(v == p) conts; depth[v] = depth[u]+1; dfs1(v,u); children++; } if(!children){ leaves[u] = leaf[u] = leaf_sum[u] = single[u] = 1; return; } trav(v,adj[u]){ if(v == p) conts; leaves[u] += leaves[v]; } leaf_sum[u] = leaves[u]; if(leaves[u]){ if(leaves[u]&1){ dp[u] = depth[u]*(leaves[u]>>1); single[u] = (leaves[u] == 1); leaves[u] = 1; } else{ dp[u] = depth[u]*((leaves[u]-2)>>1); leaves[u] = 2; } } } ll parity_sum[N][3][3]; void dfs2(ll u, ll p){ if(p != -1){ parity_sum[u][leaves[u]][leaves[p]] = depth[p]; rep1(j,2){ rep1(k,2){ parity_sum[u][j][k] += parity_sum[p][j][k]; } } } trav(v,adj[u]){ if(v == p) conts; dfs2(v,u); } } vector<ll> adj2[N]; void solve(int test_case) { ll n,q; cin >> n >> q; rep1(i,n-1){ ll u,v; cin >> u >> v; adj[u].pb(v), adj[v].pb(u); } ll root = -1; rep1(i,n){ if(sz(adj[i]) >= 2){ root = i; } } dfs1(root,-1); dfs2(root,-1); lca_algo LCA(n,root); ll depth_sum = 0; rep1(i,n) depth_sum += depth[i]*leaf[i]; ll leaf_cnt = 0; rep1(i,n) leaf_cnt += leaf[i]; ll sub = 0; rep1(i,n) sub += dp[i]; debug(root); while(q--){ ll k; cin >> k; vector<pll> b(k); ll curr_depth_sum = depth_sum; ll curr_leaf_cnt = leaf_cnt; ll curr_sub = sub; vector<ll> curr_leaves(n+5); rep1(i,n){ curr_leaves[i] = leaf_sum[i]; } auto is_leaf = leaf; rep(i,k){ ll u; cin >> u; b[i] = {LCA.tin[u],u}; curr_leaf_cnt++; curr_depth_sum += depth[u]+1; if(is_leaf[u]){ is_leaf[u] = 0; curr_leaves[u] = 0; curr_leaf_cnt--; curr_depth_sum -= depth[u]; } curr_leaves[u]++; } sort(all(b)); rep(i,k-1){ ll u = b[i].ss, v = b[i+1].ss; ll lca = LCA.query(u,v); b.pb({LCA.tin[lca],lca}); } b.pb({LCA.tin[root],root}); sort(all(b)); b.resize(unique(all(b))-b.begin()); ll siz = sz(b); vector<ll> par(siz); stack<ll> stk; rep(i,siz){ ll u = b[i].ss; while(!stk.empty() and !LCA.is_ances(stk.top(),u)){ stk.pop(); } if(i){ par[i] = stk.top(); adj2[par[i]].pb(u); } stk.push(u); } rev(i,siz-1,0){ ll u = b[i].ss, p = par[i]; trav(v,adj2[u]){ if(curr_leaves[v] != leaves[v]){ ll just_below = LCA.lift(v,depth[v]-depth[u]-1); curr_leaves[u] -= leaves[just_below]; curr_leaves[u] += 3^leaves[just_below]; } } curr_sub -= dp[u]; if(curr_leaves[u]&1){ curr_sub += depth[u]*(curr_leaves[u]>>1); curr_leaves[u] = 1; } else{ curr_sub += depth[u]*((curr_leaves[u]-2)>>1); curr_leaves[u] = 2; } if(i and leaves[u] != curr_leaves[u]){ ll p = par[i]; ll just_below = LCA.lift(u,depth[u]-depth[p]-1); ll sum1 = parity_sum[u][1][2]-parity_sum[just_below][1][2]; ll sum2 = parity_sum[u][2][1]-parity_sum[just_below][2][1]; curr_sub += sum1-sum2; } // ll even_sum = parity_sum[u][0]-parity_sum[p][0]; // ll odd_sum = parity_sum[u][1]-parity_sum[p][1]; // ll curr_single_sum = single_sum[u]-single_sum[p]; // curr_sub += even_sum*ceil2(val[u],2); // curr_sub += odd_sum*(val[u]/2); // if(val[u] >= 1){ // curr_sub -= curr_single_sum; // } } debug(curr_depth_sum); ll ans = curr_depth_sum-curr_sub*2; if(curr_leaf_cnt&1) ans = -1; cout << ans << endl; for(auto [ti,u] : b){ // val[u] = -leaf[u]; adj2[u].clear(); } } } int main() { fastio; int t = 1; // cin >> t; rep1(i, t) { solve(i); } return 0; }

Compilation message (stderr)

cleaning.cpp: In function 'void dfs1(ll, ll)':
cleaning.cpp:166:43: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
  166 |         leaves[u] = leaf[u] = leaf_sum[u] = single[u] = 1;
cleaning.cpp: In function 'void solve(int)':
cleaning.cpp:46:18: warning: statement has no effect [-Wunused-value]
   46 | #define debug(x) 42
      |                  ^~
cleaning.cpp:238:5: note: in expansion of macro 'debug'
  238 |     debug(root);
      |     ^~~~~
cleaning.cpp:296:29: warning: unused variable 'p' [-Wunused-variable]
  296 |             ll u = b[i].ss, p = par[i];
      |                             ^
cleaning.cpp:46:18: warning: statement has no effect [-Wunused-value]
   46 | #define debug(x) 42
      |                  ^~
cleaning.cpp:335:9: note: in expansion of macro 'debug'
  335 |         debug(curr_depth_sum);
      |         ^~~~~
#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...