結果
問題 | No.1769 Don't Stop the Game |
ユーザー | 👑 Nachia |
提出日時 | 2021-11-26 23:03:48 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 444 ms / 3,000 ms |
コード長 | 5,570 bytes |
コンパイル時間 | 1,625 ms |
コンパイル使用メモリ | 107,520 KB |
実行使用メモリ | 70,496 KB |
最終ジャッジ日時 | 2024-06-29 18:28:14 |
合計ジャッジ時間 | 10,627 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 3 ms
5,376 KB |
testcase_08 | AC | 277 ms
41,888 KB |
testcase_09 | AC | 178 ms
31,068 KB |
testcase_10 | AC | 284 ms
41,076 KB |
testcase_11 | AC | 180 ms
31,548 KB |
testcase_12 | AC | 360 ms
50,672 KB |
testcase_13 | AC | 368 ms
51,592 KB |
testcase_14 | AC | 373 ms
51,520 KB |
testcase_15 | AC | 366 ms
51,604 KB |
testcase_16 | AC | 381 ms
51,600 KB |
testcase_17 | AC | 404 ms
52,004 KB |
testcase_18 | AC | 414 ms
52,620 KB |
testcase_19 | AC | 422 ms
53,124 KB |
testcase_20 | AC | 444 ms
52,996 KB |
testcase_21 | AC | 425 ms
53,000 KB |
testcase_22 | AC | 435 ms
53,252 KB |
testcase_23 | AC | 373 ms
51,608 KB |
testcase_24 | AC | 380 ms
51,608 KB |
testcase_25 | AC | 142 ms
52,712 KB |
testcase_26 | AC | 168 ms
54,276 KB |
testcase_27 | AC | 191 ms
69,988 KB |
testcase_28 | AC | 246 ms
70,496 KB |
testcase_29 | AC | 219 ms
62,592 KB |
testcase_30 | AC | 236 ms
62,720 KB |
ソースコード
#include <iostream> #include <vector> #include <algorithm> using namespace std; using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; using u64 = uint64_t; #define rep(i,n) for(int i=0; i<(n); i++) struct heavy_light_decomposition { private: int N; vector<int> P; vector<int> PP; vector<int> PD; vector<int> D; public: vector<int> rangeL; vector<int> rangeR; heavy_light_decomposition(const vector<vector<int>>& E = { {} }) { N = E.size(); P.assign(N, -1); vector<int> I = { 0 }; rep(i, I.size()) { int p = I[i]; for (int e : E[p]) if (P[p] != e) { I.push_back(e); P[e] = p; } } vector<int> Z(N, 1); for (int i = N - 1; i >= 1; i--) Z[P[I[i]]] += Z[I[i]]; PP.resize(N); for (int i = 0; i < N; i++) PP[i] = i; vector<int> nx(N, -1); for (int p : I) if (p != 0) { if (nx[P[p]] == -1) nx[P[p]] = p; if (Z[nx[P[p]]] < Z[p]) nx[P[p]] = p; } rep(i, N) if (nx[i] != -1) PP[nx[i]] = i; for (int p : I) if (p != 0) PP[p] = PP[PP[p]]; PD.assign(N, N); PD[0] = 0; for (int p : I) if (p != 0) PD[p] = min(PD[PP[p]], PD[P[p]] + 1); D.assign(N, 0); for (int p : I) D[p] = (p == 0) ? 0 : (D[P[p]] + 1); rangeL.assign(N, -1); rangeR.assign(N, -1); vector<int> dfs; int ir = 0; dfs.push_back(0); while (dfs.size()) { int p = dfs.back(); dfs.pop_back(); if (p < 0) { rangeR[-1 - p] = ir; continue; } dfs.push_back(-1 - p); for (int e : E[p]) if (P[p] != e) if (e != nx[p]) dfs.push_back(e); if (nx[p] != -1) dfs.push_back(nx[p]); rangeL[p] = ir++; } } int lca(int u, int v) { if (PD[u] < PD[v]) swap(u, v); while (PD[u] > PD[v]) u = P[PP[u]]; while (PP[u] != PP[v]) { u = P[PP[u]]; v = P[PP[v]]; } return (D[u] > D[v]) ? v : u; } vector<pair<int, int>> getpath(int r, int c) { vector<pair<int, int>> res; while (PD[r] < PD[c]) { res.push_back({ rangeL[PP[c]], rangeL[c] + 1 }); c = P[PP[c]]; } if (PP[r] != PP[c]) return {}; if (D[r] > D[c]) return {}; res.push_back({ rangeL[r], rangeL[c] + 1 }); reverse(res.begin(), res.end()); return move(res); } }; class CoordinateCompress { using Elem = i64; static const Elem negInf = -1001001001001001001; vector<pair<Elem, int>> G; vector<int> res; vector<Elem> mRealval; Elem mMaxcoord; bool ok = true; void calc() { if (ok) return; sort(G.begin(), G.end()); res.resize(G.size()); mRealval.clear(); Elem x = negInf; int p = -1; rep(i, G.size()) { if (x != G[i].first) { x = G[i].first; mRealval.push_back(x); p++; } res[G[i].second] = p; } mMaxcoord = p; ok = true; } public: int push(Elem x) { ok = false; G.push_back({ x,(int)G.size() }); return G.back().second; } int operator[](int i) { calc(); return res[i]; } Elem realval(int x) { calc(); return mRealval[x]; } Elem maxcoord() { calc(); return mMaxcoord; } }; struct Edge{ int to; u32 h; }; int N; vector<vector<Edge>> E; vector<vector<int>> hldE; heavy_light_decomposition hld; vector<int> P; vector<int> Z; CoordinateCompress CCH; vector<u32> H; vector<vector<int>> black_stack; vector<int> prev_black; vector<int> block_size; vector<int> root_size; i64 ans; void dfs(int p = 0, int pre = -1){ if(pre == -1){ black_stack.assign(N,{0}); prev_black.assign(N,0); block_size.assign(N,0); root_size.assign(N,N); ans = 0; } int h = H[p]; block_size[p] = Z[p]; prev_black[p] = black_stack[h].back(); if(prev_black[p] != 0) block_size[prev_black[p]] -= Z[p]; else root_size[h] -= Z[p]; for(int e : hldE[p]) if(e != pre){ black_stack[H[p]].push_back(e); dfs(e,p); black_stack[H[p]].pop_back(); } } int main(){ cin >> N; E.resize(N); hldE.resize(N); rep(i,N-1){ int u,v; u32 c; cin >> u >> v >> c; u--; v--; E[u].push_back({ v,c }); E[v].push_back({ u,c }); hldE[u].push_back(v); hldE[v].push_back(u); } hld = heavy_light_decomposition(hldE); vector<int> I = {0}; P.assign(N,-1); H.assign(N,0); rep(i,I.size()){ int p = I[i]; for(auto e : E[p]){ if(P[p] == e.to) continue; I.push_back(e.to); P[e.to] = p; H[e.to] = H[p] ^ e.h; } } Z.assign(N,1); for(int i=N-1; i>=1; i--) Z[P[I[i]]] += Z[I[i]]; rep(i,N) H[i] = CCH.push(H[i]); rep(i,N) H[i] = CCH[H[i]]; dfs(); //rep(i,N) cout << prev_black[i] << " "; cout << endl; //rep(i,N) cout << block_size[i] << " "; cout << endl; i64 ans = 0; for(int p=1; p<N; p++) ans += block_size[p]; for(int p=1; p<N; p++){ if(prev_black[p] != 0) ans += block_size[prev_black[p]]; else ans += root_size[H[p]]; } cout << ans << endl; return 0; } struct ios_do_not_sync { ios_do_not_sync() { ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_instance;