結果

問題 No.1769 Don't Stop the Game
ユーザー 👑 NachiaNachia
提出日時 2021-11-27 14:07:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 243 ms / 3,000 ms
コード長 1,955 bytes
コンパイル時間 1,193 ms
コンパイル使用メモリ 89,612 KB
実行使用メモリ 33,744 KB
最終ジャッジ日時 2023-09-12 16:25:45
合計ジャッジ時間 7,440 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,384 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,384 KB
testcase_08 AC 141 ms
18,476 KB
testcase_09 AC 97 ms
13,988 KB
testcase_10 AC 156 ms
17,696 KB
testcase_11 AC 91 ms
14,480 KB
testcase_12 AC 162 ms
22,044 KB
testcase_13 AC 172 ms
22,216 KB
testcase_14 AC 168 ms
22,208 KB
testcase_15 AC 169 ms
22,292 KB
testcase_16 AC 195 ms
22,212 KB
testcase_17 AC 215 ms
22,208 KB
testcase_18 AC 229 ms
22,212 KB
testcase_19 AC 235 ms
22,352 KB
testcase_20 AC 235 ms
22,328 KB
testcase_21 AC 234 ms
22,320 KB
testcase_22 AC 243 ms
22,288 KB
testcase_23 AC 176 ms
22,360 KB
testcase_24 AC 176 ms
22,296 KB
testcase_25 AC 78 ms
22,776 KB
testcase_26 AC 124 ms
22,676 KB
testcase_27 AC 105 ms
33,652 KB
testcase_28 AC 150 ms
33,744 KB
testcase_29 AC 145 ms
28,892 KB
testcase_30 AC 148 ms
28,920 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++)


struct Edge{ int to; int h; };

int N;
vector<vector<Edge>> E;
vector<int> P;
vector<int> Z;

vector<int> H;
vector<int> SH;

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,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]];
    
    SH = H;
    sort(SH.begin(), SH.end());
    SH.erase(unique(SH.begin(),SH.end()),SH.end());
    for(int& h : H) h = lower_bound(SH.begin(),SH.end(),h) - SH.begin();
    
    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