結果

問題 No.1769 Don't Stop the Game
ユーザー 👑 NachiaNachia
提出日時 2021-11-27 00:36:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 243 ms / 3,000 ms
コード長 2,725 bytes
コンパイル時間 1,220 ms
コンパイル使用メモリ 93,636 KB
実行使用メモリ 32,880 KB
最終ジャッジ日時 2023-09-12 06:12:18
合計ジャッジ時間 7,396 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 175 ms
20,492 KB
testcase_09 AC 108 ms
14,080 KB
testcase_10 AC 162 ms
19,124 KB
testcase_11 AC 107 ms
14,268 KB
testcase_12 AC 220 ms
23,260 KB
testcase_13 AC 228 ms
23,064 KB
testcase_14 AC 221 ms
23,188 KB
testcase_15 AC 226 ms
23,332 KB
testcase_16 AC 224 ms
23,600 KB
testcase_17 AC 233 ms
22,968 KB
testcase_18 AC 239 ms
23,988 KB
testcase_19 AC 243 ms
23,688 KB
testcase_20 AC 241 ms
23,796 KB
testcase_21 AC 242 ms
23,728 KB
testcase_22 AC 238 ms
24,304 KB
testcase_23 AC 214 ms
23,984 KB
testcase_24 AC 222 ms
22,796 KB
testcase_25 AC 94 ms
25,812 KB
testcase_26 AC 107 ms
25,144 KB
testcase_27 AC 124 ms
32,776 KB
testcase_28 AC 137 ms
32,880 KB
testcase_29 AC 130 ms
28,000 KB
testcase_30 AC 136 ms
28,172 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 = i64;
    static const Elem negInf = -1001001001001001001;
    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<vector<int>> black_stack;
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){

    int h = H[p];
    block_size[p] = Z[p];
    prev_black[p] = black_stack[h];

    if(prev_black[p] != 0) block_size[prev_black[p]] -= Z[p];
    else root_size[h] -= 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);
    prev_black.assign(N,0);
    block_size.assign(N,0);
    root_size.assign(N,N);
    ans = 0;

    dfs();
    
    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;
0