結果

問題 No.1769 Don't Stop the Game
ユーザー 👑 NachiaNachia
提出日時 2021-11-27 14:04:25
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 183 ms / 3,000 ms
コード長 2,590 bytes
コンパイル時間 1,084 ms
コンパイル使用メモリ 94,228 KB
実行使用メモリ 32,840 KB
最終ジャッジ日時 2023-09-12 16:21:17
合計ジャッジ時間 7,027 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 132 ms
17,872 KB
testcase_09 AC 82 ms
13,612 KB
testcase_10 AC 123 ms
17,116 KB
testcase_11 AC 95 ms
13,924 KB
testcase_12 AC 182 ms
21,088 KB
testcase_13 AC 173 ms
21,532 KB
testcase_14 AC 161 ms
21,380 KB
testcase_15 AC 171 ms
21,360 KB
testcase_16 AC 179 ms
21,272 KB
testcase_17 AC 174 ms
21,372 KB
testcase_18 AC 175 ms
21,384 KB
testcase_19 AC 169 ms
21,324 KB
testcase_20 AC 183 ms
21,356 KB
testcase_21 AC 179 ms
21,272 KB
testcase_22 AC 171 ms
21,348 KB
testcase_23 AC 138 ms
21,372 KB
testcase_24 AC 153 ms
21,532 KB
testcase_25 AC 87 ms
22,652 KB
testcase_26 AC 99 ms
22,444 KB
testcase_27 AC 119 ms
32,840 KB
testcase_28 AC 131 ms
32,760 KB
testcase_29 AC 124 ms
27,944 KB
testcase_30 AC 127 ms
27,996 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
0