結果

問題 No.1002 Twotone
ユーザー betrue12betrue12
提出日時 2020-02-28 22:35:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,161 ms / 5,000 ms
コード長 3,133 bytes
コンパイル時間 2,362 ms
コンパイル使用メモリ 214,436 KB
実行使用メモリ 35,200 KB
最終ジャッジ日時 2024-04-21 19:44:06
合計ジャッジ時間 15,715 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,320 KB
testcase_01 AC 4 ms
8,320 KB
testcase_02 AC 5 ms
8,192 KB
testcase_03 AC 306 ms
15,616 KB
testcase_04 AC 404 ms
17,408 KB
testcase_05 AC 405 ms
18,048 KB
testcase_06 AC 5 ms
8,320 KB
testcase_07 AC 231 ms
13,824 KB
testcase_08 AC 439 ms
17,280 KB
testcase_09 AC 438 ms
17,408 KB
testcase_10 AC 5 ms
8,192 KB
testcase_11 AC 414 ms
16,512 KB
testcase_12 AC 749 ms
18,176 KB
testcase_13 AC 1,161 ms
18,816 KB
testcase_14 AC 6 ms
8,192 KB
testcase_15 AC 267 ms
13,824 KB
testcase_16 AC 563 ms
17,920 KB
testcase_17 AC 571 ms
18,048 KB
testcase_18 AC 5 ms
8,192 KB
testcase_19 AC 574 ms
19,840 KB
testcase_20 AC 679 ms
24,448 KB
testcase_21 AC 657 ms
24,064 KB
testcase_22 AC 5 ms
8,192 KB
testcase_23 AC 424 ms
19,200 KB
testcase_24 AC 760 ms
25,344 KB
testcase_25 AC 807 ms
25,344 KB
testcase_26 AC 6 ms
8,320 KB
testcase_27 AC 136 ms
14,776 KB
testcase_28 AC 213 ms
17,572 KB
testcase_29 AC 208 ms
17,952 KB
testcase_30 AC 4 ms
8,192 KB
testcase_31 AC 210 ms
17,320 KB
testcase_32 AC 216 ms
17,316 KB
testcase_33 AC 208 ms
17,312 KB
testcase_34 AC 622 ms
35,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

vector<pair<int, int>> edges[200000];
bitset<200000> dead;

struct Centroid{
    vector<int> centroid;
    int sz[200000];
    int all_sz;

    int get_size(int i, int prev){
        int sz = 1;
        for(auto& e : edges[i]){
            int j = e.first;
            if(j == prev || dead[j]) continue;
            sz += get_size(j, i);
        }
        return sz;
    }

    void dfs(int i, int prev){
        sz[i] = 1;
        bool is_centroid = true;
        for(auto& e : edges[i]){
            int j = e.first;
            if(j == prev || dead[j]) continue;
            dfs(j, i);
            sz[i] += sz[j];
            if(sz[j] > all_sz/2) is_centroid = false;
        }
        if(all_sz - sz[i] > all_sz/2) is_centroid = false;
        if(is_centroid) centroid.push_back(i);
    }

    int calculate(int s){
        centroid.clear();
        all_sz = get_size(s, -1);
        dfs(s, -1);
        assert(centroid.size() > 0);
        return centroid[0];
    }
};

int64_t ans = 0;

Centroid cent;
int dfs1(int i, int p, int c){
    int res = 1;
    for(auto& e : edges[i]){
        int j = e.first, col = e.second;
        if(j == p || dead[j]) continue;
        if(c == col) res += dfs1(j, i, c);
    }
    return res;
}
void dfs2(int i, int p, int c1, int c2, map<pair<int, int>, int>& mp){
    if(c2 != -1) mp[minmax(c1, c2)]++;
    for(auto& e : edges[i]){
        int j = e.first, col = e.second;
        if(j == p || dead[j]) continue;
        if(c2 != -1 && col != c1 && col != c2) continue;
        if(c1 == col){
            dfs2(j, i, c1, c2, mp);
        }else{
            dfs2(j, i, c1, col, mp);
        }
    }
}

int single_V[200000], single_C[200000];

void solve(int s){
    int c = cent.calculate(s);
    int64_t single_sum = 0;
    for(auto& e : edges[c]){
        int j = e.first, col = e.second;
        if(dead[j]) continue;
        int res = dfs1(j, c, col);
        single_V[j] = res;
        single_C[col] += res;
        single_sum += res;
        ans += res * (single_sum - single_C[col]);
    }
    map<pair<int, int>, int> mp;
    for(auto& e : edges[c]){
        int j = e.first, col = e.second;
        if(dead[j]) continue;
        single_C[col] -= single_V[j];

        map<pair<int, int>, int> mp2;
        dfs2(j, c, col, -1, mp2);
        for(auto& pp : mp2){
            auto& p = pp.first;
            int64_t num = 1 + single_C[p.first] + single_C[p.second];
            if(mp.count(p)) num += mp[p];
            ans += pp.second * num;
        }
        for(auto& pp : mp2) mp[pp.first] += pp.second;
        single_C[col] += single_V[j];
    }
    for(auto& e : edges[c]){
        int j = e.first, col = e.second;
        single_V[j] = single_C[col] = 0;
    }

    dead[c] = true;
    for(auto& e : edges[c]) if(!dead[e.first]) solve(e.first);
}



int main(){
    int N, K;
    cin >> N >> K;
    for(int i=0; i<N-1; i++){
        int a, b, c;
        cin >> a >> b >> c;
        a--; b--; c--;
        edges[a].emplace_back(b, c);
        edges[b].emplace_back(a, c);
    }

    solve(0);
    cout << ans << endl;
    return 0;
}
0