結果

問題 No.1002 Twotone
ユーザー tempura_pptempura_pp
提出日時 2020-02-23 02:52:06
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2,512 ms / 5,000 ms
コード長 1,860 bytes
コンパイル時間 2,876 ms
コンパイル使用メモリ 212,456 KB
実行使用メモリ 48,512 KB
最終ジャッジ日時 2024-04-18 08:13:36
合計ジャッジ時間 20,806 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
8,300 KB
testcase_01 AC 6 ms
8,448 KB
testcase_02 AC 5 ms
8,320 KB
testcase_03 AC 407 ms
15,104 KB
testcase_04 AC 550 ms
17,408 KB
testcase_05 AC 565 ms
17,408 KB
testcase_06 AC 6 ms
8,320 KB
testcase_07 AC 258 ms
13,696 KB
testcase_08 AC 463 ms
17,348 KB
testcase_09 AC 466 ms
17,348 KB
testcase_10 AC 6 ms
8,320 KB
testcase_11 AC 546 ms
16,128 KB
testcase_12 AC 776 ms
18,376 KB
testcase_13 AC 816 ms
18,304 KB
testcase_14 AC 6 ms
8,192 KB
testcase_15 AC 355 ms
13,952 KB
testcase_16 AC 802 ms
18,048 KB
testcase_17 AC 782 ms
18,056 KB
testcase_18 AC 5 ms
8,320 KB
testcase_19 AC 693 ms
22,784 KB
testcase_20 AC 854 ms
31,488 KB
testcase_21 AC 852 ms
30,720 KB
testcase_22 AC 6 ms
8,192 KB
testcase_23 AC 633 ms
19,712 KB
testcase_24 AC 1,207 ms
28,032 KB
testcase_25 AC 1,166 ms
25,792 KB
testcase_26 AC 5 ms
8,320 KB
testcase_27 AC 163 ms
14,160 KB
testcase_28 AC 271 ms
16,724 KB
testcase_29 AC 260 ms
16,720 KB
testcase_30 AC 5 ms
8,320 KB
testcase_31 AC 250 ms
16,460 KB
testcase_32 AC 272 ms
16,636 KB
testcase_33 AC 247 ms
16,596 KB
testcase_34 AC 2,512 ms
48,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

vector<pair<int,int>> v[202020];
int sz[202020], used[202020];

void szdfs(int x,int p){
    sz[x]=1;
    for(auto [to,color] : v[x]){
        if(to==p||used[to])continue;
        szdfs(to,x);
        sz[x]+=sz[to];
    }
}
int centroid(int x,int p,int total){
    for(auto [to,color] : v[x]){
        if(to==p||used[to])continue;
        if(2*sz[to]>total)return centroid(to,x,total);
    }
    return x;
}

map<pair<int,int>, int> MP, mp;
map<int,int> ONE, one, TWO, two;
int ONES, ones;
ll ans;

void dfs(int x,int p, int fi, int se){
    if(fi && se){
        ans += MP[minmax(fi,se)]++-mp[minmax(fi,se)]++;
        ans += ONE[fi]-one[fi];
        ans += ONE[se]-one[se];
        ++TWO[fi];++TWO[se];
        ++two[fi];++two[se];
    }
    else if(fi){
        ans += TWO[fi]-two[fi];
        ans += ONES++-ONE[fi]++;
        ans -= ones++-one[fi]++;
    }
    for(auto [to,color] : v[x]){
        if(to==p||used[to])continue;
        if(!se){
            if(color==fi)dfs(to, x, fi, se);
            else dfs(to,x,fi,color);
        }
        else if(fi==color||se==color)dfs(to, x, fi, se);
    }
}
void solve(int x){
    szdfs(x,-1);
    if(sz[x]==1)return ; 
    x =centroid(x,-1,sz[x]);
    used[x]=1;
    MP.clear();ONE.clear();TWO.clear();
    ONES = 0;
    for(auto [to,color]:v[x]){
        mp.clear();one.clear();two.clear();
        ones = 0;
        if(used[to])continue;
        dfs(to, x, color,0);
    }
    for(auto e:MP)ans += e.second;
    for(auto [to,color]:v[x]){
        if(used[to])continue;
        solve(to);
    }
}
int main(){
    int n,k;
    cin>>n>>k;
    for(int i=0;i<n-1;i++){
        int x,y,z;
        cin>>x>>y>>z;
        --x;--y;
        v[x].emplace_back(y,z);
        v[y].emplace_back(x,z);
    }
    solve(0);
    cout<<ans<<endl;
    return 0;
}
0