結果

問題 No.1002 Twotone
ユーザー tempura_pptempura_pp
提出日時 2020-02-23 03:16:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,542 bytes
コンパイル時間 1,704 ms
コンパイル使用メモリ 131,480 KB
実行使用メモリ 36,484 KB
最終ジャッジ日時 2024-04-18 08:15:17
合計ジャッジ時間 16,401 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
8,192 KB
testcase_01 AC 6 ms
8,448 KB
testcase_02 AC 6 ms
8,448 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;


vector<pair<int,int>> v[202020];
int sz[202020];
bool 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;
int ONE[202020], one[202020], TWO[202020], two[202020];
int ONES, ones;
ll ans;

void dfs(int x,int p, int fi, int se, vector<int>&used_c){
    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;
        used_c.push_back(color);
        if(!se){
            if(color==fi)dfs(to, x, fi, se,used_c);
            else dfs(to,x,fi,color,used_c);
        }
        else if(fi==color||se==color)dfs(to, x, fi, se,used_c);
    }
}

void solve(int x,int p){
    szdfs(x,-1);
    if(sz[x]==1)return ; 
    x =centroid(x,-1,sz[x]);
    used[x]=1;
    vector<int> USED_C;
    for(auto [to,color]:v[x]){
        ones = 0;
        if(to==p||used[to])continue;
        vector<int>used_c;
        dfs(to, x, color,0,used_c);
        mp.clear();
        for(auto e:used_c){
            USED_C.push_back(e);
            one[e]=two[e]=0;
        }
    }
    for(auto e:MP)ans += e.second;
    MP.clear();
    ONES=0;
    for(auto e:USED_C)ONE[e]=TWO[e]=0;
    for(auto [to,color]:v[x]){
        if(to==p||used[to])continue;
        solve(to,x);
    }
}


int main(){
    int n,k;
    cin>>n>>k;
    rep(i,n-1){
        int x,y,z;
        cin>>x>>y>>z;
        --x;--y;
        v[x].emplace_back(y,z);
        v[y].emplace_back(x,z);
    }
    solve(0,-1);
    cout<<ans<<endl;
    return 0;
}
0