結果
| 問題 | No.872 All Tree Path | 
| コンテスト | |
| ユーザー |  beet | 
| 提出日時 | 2019-08-30 21:37:15 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 342 ms / 3,000 ms | 
| コード長 | 1,113 bytes | 
| コンパイル時間 | 2,468 ms | 
| コンパイル使用メモリ | 201,796 KB | 
| 最終ジャッジ日時 | 2025-01-07 15:47:45 | 
| ジャッジサーバーID (参考情報) | judge3 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 18 | 
ソースコード
#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;}
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)};
}
//INSERT ABOVE HERE
signed main(){
  Int n;
  cin>>n;
  using P = pair<Int, Int>;
  vector< vector< P > > G(n);
  for(Int i=1;i<n;i++){
    Int u,v,w;
    cin>>u>>v>>w;
    u--;v--;
    G[u].emplace_back(v,w);
    G[v].emplace_back(u,w);
  }
  Int ans=0;
  vector<Int> sz(n,0);
  MFP([&](auto dfs,Int v,Int p)->void{
        sz[v]=1;
        for(auto e:G[v]){
          Int u,c;
          tie(u,c)=e;
          if(u==p) continue;
          dfs(u,v);
          sz[v]+=sz[u];
          ans+=(n-sz[u])*sz[u]*c*2;
        }
      })(0,-1);
  cout<<ans<<endl;
  return 0;
}
            
            
            
        