結果

問題 No.1002 Twotone
ユーザー beetbeet
提出日時 2020-02-28 22:44:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3,492 ms / 5,000 ms
コード長 4,066 bytes
コンパイル時間 3,495 ms
コンパイル使用メモリ 246,576 KB
実行使用メモリ 84,864 KB
最終ジャッジ日時 2024-04-21 19:54:59
合計ジャッジ時間 44,003 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 4 ms
5,760 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 751 ms
53,596 KB
testcase_04 AC 960 ms
67,372 KB
testcase_05 AC 996 ms
69,712 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 960 ms
42,040 KB
testcase_08 AC 1,771 ms
68,372 KB
testcase_09 AC 1,796 ms
67,968 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 1,944 ms
53,248 KB
testcase_12 AC 2,589 ms
64,896 KB
testcase_13 AC 2,682 ms
66,860 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 1,405 ms
39,328 KB
testcase_16 AC 2,774 ms
65,408 KB
testcase_17 AC 2,684 ms
65,100 KB
testcase_18 AC 4 ms
6,144 KB
testcase_19 AC 1,613 ms
63,948 KB
testcase_20 AC 1,824 ms
75,200 KB
testcase_21 AC 1,899 ms
74,624 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 1,943 ms
54,212 KB
testcase_24 AC 3,459 ms
84,864 KB
testcase_25 AC 3,492 ms
84,708 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 196 ms
51,800 KB
testcase_28 AC 432 ms
71,228 KB
testcase_29 AC 328 ms
72,248 KB
testcase_30 AC 3 ms
5,376 KB
testcase_31 AC 313 ms
71,788 KB
testcase_32 AC 470 ms
71,096 KB
testcase_33 AC 349 ms
72,888 KB
testcase_34 AC 807 ms
81,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


struct Centroid{
  vector<int> sz,dead;
  vector< vector<int> > G;
  Centroid(){}
  Centroid(int n):sz(n,1),dead(n,0),G(n){}

  void add_edge(int u,int v){
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }

  int dfs(int v,int p){
    sz[v]=1;
    for(int u:G[v])
      if(u!=p&&!dead[u]) sz[v]+=dfs(u,v);
    return sz[v];
  }

  void find(int v,int p,int tmp,vector<int> &cs) {
    int ok=1;
    for (int u:G[v]){
      if(u==p||dead[u]) continue;
      find(u,v,tmp,cs);
      ok&=(sz[u]<=tmp/2);
    }
    ok&=(tmp-sz[v]<=tmp/2);
    if(ok) cs.push_back(v);
  }

  vector<int> build(int r) {
    int tmp=dfs(r,-1);
    vector<int> cs;
    find(r,-1,tmp,cs);
    return cs;
  }

  void disable(int v){
    dead[v]=1;
  }

  void enable(int v){
    dead[v]=0;
  }

  int alive(int v){
    return !dead[v];
  }
};


template<typename F>
struct FixPoint : F{
  FixPoint(F&& f):F(forward<F>(f)){}
  template<typename... Args>
  decltype(auto) operator()(Args&&... args) const{
    return F::operator()(*this,forward<Args>(args)...);
  }
};
template<typename F>
inline decltype(auto) MFP(F&& f){
  return FixPoint<F>{forward<F>(f)};
}

struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;


#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<typename T,typename U, typename H=hash<T> >
using gmap = cc_hash_table<T, U, H>;

//INSERT ABOVE HERE
signed main(){
  using ll = long long;

  int n,k;
  cin>>n>>k;
  k++;k++;

  vector< gmap<int, int> > col(n);
  Centroid G(n);
  for(int i=1;i<n;i++){
    int x,y,z;
    cin>>x>>y>>z;
    x--;y--;z--;
    G.add_edge(x,y);
    col[x][y]=z;
    col[y][x]=z;
  }

  queue<int> que;
  que.emplace(G.build(0)[0]);

  ll ans=0,all=0;
  vector<ll> cnt(k,0),uku(k,0);

  auto P=[&](int x,int y){return (ll)x*k+y;};
  auto calc=
    [&](ll p,int q)->ll{
      int x=p/k,y=p%k;
      if(x==q||y==q) return p;
      if(y==k-1) return P(min(x,q),max(x,q));
      return P(k-1,k-1);
    };

  while(!que.empty()){
    int r=que.front();que.pop();

    gmap<ll, int> num;
    for(int t:G.G[r]){
      if(!G.alive(t)) continue;

      // count
      MFP([&](auto dfs,int v,int p,ll st)->void{
            if(st==P(k-1,k-1)) return;
            for(int u:G.G[v]){
              if(u==p) continue;
              if(!G.alive(u)) continue;
              dfs(u,v,calc(st,col[u][v]));
            }
            if(st%k==k-1){
              ans+=all-cnt[st/k];
              ans+=uku[st/k];
            }else{
              ans+=1;
              ans+=cnt[st/k];
              ans+=cnt[st%k];
              ans+=num[st];
            }
          })(t,r,P(col[r][t],k-1));

      // paint
      MFP([&](auto dfs,int v,int p,ll st)->void{
            if(st==P(k-1,k-1)) return;
            for(int u:G.G[v]){
              if(u==p) continue;
              if(!G.alive(u)) continue;
              dfs(u,v,calc(st,col[u][v]));
            }
            if(st%k==k-1){
              cnt[st/k]++;
              all++;
            }else{
              num[st]++;
              uku[st/k]++;
              uku[st%k]++;
            }
          })(t,r,P(col[r][t],k-1));
    }

    for(int t:G.G[r]){
      if(!G.alive(t)) continue;

      // clear
      MFP([&](auto dfs,int v,int p,ll st)->void{
            if(st==P(k-1,k-1)) return;
            for(int u:G.G[v]){
              if(u==p) continue;
              if(!G.alive(u)) continue;
              dfs(u,v,calc(st,col[u][v]));
            }
            if(st%k==k-1){
              cnt[st/k]--;
              all--;
            }else{
              num[st]--;
              uku[st/k]--;
              uku[st%k]--;
            }
          })(t,r,P(col[r][t],k-1));
    }

    G.disable(r);
    for(int u:G.G[r])
      if(G.alive(u))
        que.emplace(G.build(u)[0]);
  }
  cout<<ans<<endl;
  return 0;
}
0