結果

問題 No.1002 Twotone
ユーザー tempura_pptempura_pp
提出日時 2020-02-23 02:25:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,311 bytes
コンパイル時間 1,405 ms
コンパイル使用メモリ 114,300 KB
実行使用メモリ 9,344 KB
最終ジャッジ日時 2024-04-18 08:10:55
合計ジャッジ時間 8,620 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,888 KB
testcase_01 AC 4 ms
5,888 KB
testcase_02 AC 3 ms
6,016 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 WA -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 WA -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 WA -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 WA -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 WA -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 WA -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 WA -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'void szdfs(int, int)':
main.cpp:32:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   32 |     for(auto [to,color] : v[x]){
      |              ^
main.cpp: In function 'int centroid(int, int, int)':
main.cpp:40:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   40 |     for(auto [to,color] : v[x]){
      |              ^
main.cpp: In function 'void dfs(int, int, int, int)':
main.cpp:69:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   69 |     for(auto [to,color] : v[x]){
      |              ^
main.cpp: In function 'void solve(int, int)':
main.cpp:86:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   86 |     for(auto [to,color]:v[x]){
      |              ^
main.cpp:93:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   93 |     for(auto [to,color]:v[x]){
      |              ^

ソースコード

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[101010];
int sz[101010];
bool used[101010];

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||to==color)dfs(to, x, fi, se);
    }
}

void solve(int x,int p){
    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(to==p||used[to])continue;
        dfs(to, x, color,0);
    }
    for(auto e:MP)ans += e.second;
    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