結果

問題 No.1769 Don't Stop the Game
ユーザー 👑 NachiaNachia
提出日時 2021-11-27 00:25:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 208 ms / 3,000 ms
コード長 3,039 bytes
コンパイル時間 1,216 ms
コンパイル使用メモリ 95,844 KB
実行使用メモリ 41,672 KB
最終ジャッジ日時 2023-09-12 05:51:41
合計ジャッジ時間 6,829 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 142 ms
22,056 KB
testcase_09 AC 83 ms
16,256 KB
testcase_10 AC 141 ms
21,672 KB
testcase_11 AC 86 ms
16,244 KB
testcase_12 AC 172 ms
25,144 KB
testcase_13 AC 185 ms
25,380 KB
testcase_14 AC 208 ms
25,504 KB
testcase_15 AC 184 ms
25,340 KB
testcase_16 AC 184 ms
25,348 KB
testcase_17 AC 181 ms
26,288 KB
testcase_18 AC 183 ms
27,072 KB
testcase_19 AC 180 ms
27,308 KB
testcase_20 AC 175 ms
27,672 KB
testcase_21 AC 178 ms
27,596 KB
testcase_22 AC 180 ms
27,620 KB
testcase_23 AC 145 ms
25,556 KB
testcase_24 AC 159 ms
25,248 KB
testcase_25 AC 91 ms
26,104 KB
testcase_26 AC 107 ms
27,844 KB
testcase_27 AC 124 ms
39,928 KB
testcase_28 AC 136 ms
41,672 KB
testcase_29 AC 130 ms
35,548 KB
testcase_30 AC 133 ms
35,480 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;
    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<int> P;
vector<int> Z;

CoordinateCompress CCH;
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){
    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];

    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){
        int black_stack_tmp = black_stack[H[p]];
        black_stack[H[p]] = e.to;
        dfs(e.to,p);
        black_stack[H[p]] = black_stack_tmp;
    }
}


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]];

    rep(i,N) H[i] = CCH.push(H[i]);
    rep(i,N) H[i] = CCH[H[i]];

    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