結果
| 問題 |
No.1769 Don't Stop the Game
|
| コンテスト | |
| ユーザー |
Nachia
|
| 提出日時 | 2021-11-27 14:04:25 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 283 ms / 3,000 ms |
| コード長 | 2,590 bytes |
| コンパイル時間 | 4,439 ms |
| コンパイル使用メモリ | 124,680 KB |
| 最終ジャッジ日時 | 2025-01-26 02:18:26 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 28 |
ソースコード
#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++)
class CoordinateCompress {
using Elem = int;
static const Elem negInf = -1001001001;
vector<pair<Elem, int>> G;
vector<int> res;
bool ok = true;
public:
int push(Elem x) {
ok = false;
G.push_back({ x,(int)G.size() });
return G.back().second;
}
void calc() {
if (ok) return;
sort(G.begin(), G.end());
res.resize(G.size());
Elem x = negInf;
int p = -1;
rep(i, G.size()) {
if (x != G[i].first) { x = G[i].first; p++; }
res[G[i].second] = p;
}
ok = true;
}
int operator[](int i) {
calc();
return res[i];
}
};
struct Edge{ int to; u32 h; };
int N;
vector<vector<Edge>> E;
vector<int> P;
vector<int> Z;
vector<u32> H;
vector<int> black_stack;
vector<int> prev_block;
vector<int> block_size;
void dfs(int p = 0, int pre = -1){
int h = H[p];
int prev_black_p = black_stack[h];
block_size[p] = Z[p];
prev_block[p] = prev_black_p;
block_size[prev_black_p] -= Z[p];
for(auto e : E[p]) if(e.to != pre){
black_stack[h] = e.to;
dfs(e.to,p);
black_stack[h] = prev_black_p;
}
}
int main(){
cin >> N;
E.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 });
}
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]];
{
CoordinateCompress CCH;
rep(i,N) H[i] = CCH.push(H[i]);
CCH.calc();
rep(i,N) H[i] = CCH[H[i]];
}
black_stack.assign(N,0);
rep(i,N) black_stack[i] = N + i;
prev_block.assign(N,0);
rep(i,N) prev_block[i] = N + H[i];
block_size.assign(2*N,N);
dfs();
i64 ans = 0;
for(int p=1; p<N; p++) ans += block_size[p];
for(int p=1; p<N; p++) ans += block_size[prev_block[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;
Nachia