結果

問題 No.1002 Twotone
ユーザー beetbeet
提出日時 2020-02-28 22:56:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4,867 ms / 5,000 ms
コード長 4,719 bytes
コンパイル時間 4,481 ms
コンパイル使用メモリ 238,044 KB
実行使用メモリ 75,720 KB
最終ジャッジ日時 2024-04-21 20:08:35
合計ジャッジ時間 57,009 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 5 ms
5,504 KB
testcase_02 AC 4 ms
5,376 KB
testcase_03 AC 997 ms
43,648 KB
testcase_04 AC 1,319 ms
53,888 KB
testcase_05 AC 1,341 ms
56,448 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 994 ms
35,888 KB
testcase_08 AC 1,844 ms
57,812 KB
testcase_09 AC 1,905 ms
57,496 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 2,130 ms
45,412 KB
testcase_12 AC 2,951 ms
54,796 KB
testcase_13 AC 2,994 ms
56,792 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 1,527 ms
35,844 KB
testcase_16 AC 3,206 ms
60,736 KB
testcase_17 AC 3,116 ms
58,724 KB
testcase_18 AC 5 ms
6,272 KB
testcase_19 AC 2,274 ms
53,760 KB
testcase_20 AC 2,633 ms
63,616 KB
testcase_21 AC 2,632 ms
63,232 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 2,371 ms
48,240 KB
testcase_24 AC 4,867 ms
75,720 KB
testcase_25 AC 4,815 ms
75,572 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 537 ms
40,376 KB
testcase_28 AC 1,288 ms
56,180 KB
testcase_29 AC 881 ms
57,204 KB
testcase_30 AC 3 ms
5,376 KB
testcase_31 AC 852 ms
56,740 KB
testcase_32 AC 1,411 ms
56,052 KB
testcase_33 AC 984 ms
57,844 KB
testcase_34 AC 1,188 ms
68,080 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)};
}


template<typename V>
V compress(V v){
  sort(v.begin(),v.end());
  v.erase(unique(v.begin(),v.end()),v.end());
  return v;
}
template<typename T>
map<T, Int> dict(const vector<T> &v){
  map<T, Int> res;
  for(Int i=0;i<(Int)v.size();i++)
    res[v[i]]=i;
  return res;
}
map<char, Int> dict(const string &v){
  return dict(vector<char>(v.begin(),v.end()));
}

//INSERT ABOVE HERE
signed main(){
  Int n,k;
  cin>>n>>k;

  using P = pair<Int, Int>;
  vector< map<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]);

  Int ans=0,all=0;
  vector<Int> cnt(k,0),uku(k,0);
  while(!que.empty()){
    Int r=que.front();que.pop();
    auto calc=
      [&](P p,Int q)->P{
        assert(~p.first);
        if(p.first==q||p.second==q) return p;
        if(p.second==-1) return minmax({p.first,q});
        return P(-1,-1);
      };

    vector<P> vp;
    for(Int t:G.G[r]){
      if(!G.alive(t)) continue;

      MFP([&](auto dfs,Int v,Int p,P st)->void{
            if(st.first<0) 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.second!=-1){
              vp.emplace_back(st);
            }
          })(t,r,P(col[r][t],-1));
    }
    vp=compress(vp);
    auto idx=[&](P p){return lower_bound(vp.begin(),vp.end(),p)-vp.begin();};

    vector<Int> num(vp.size());
    for(Int t:G.G[r]){
      if(!G.alive(t)) continue;

      // count
      MFP([&](auto dfs,Int v,Int p,P st)->void{
            if(st.first<0) 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.second==-1){
              ans+=all-cnt[st.first];
              ans+=uku[st.first];
            }else{
              ans+=1;
              ans+=cnt[st.first];
              ans+=cnt[st.second];
              ans+=num[idx(st)];
            }
          })(t,r,P(col[r][t],-1));

      // paint
      MFP([&](auto dfs,Int v,Int p,P st)->void{
            if(st.first<0) 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.second==-1){
              cnt[st.first]++;
              all++;
            }else{
              num[idx(st)]++;
              uku[st.first]++;
              uku[st.second]++;
            }
          })(t,r,P(col[r][t],-1));
    }

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

      // clear
      MFP([&](auto dfs,Int v,Int p,P st)->void{
            if(st.first<0) 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.second==-1){
              cnt[st.first]--;
              all--;
            }else{
              uku[st.first]--;
              uku[st.second]--;
            }
          })(t,r,P(col[r][t],-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