結果

問題 No.872 All Tree Path
ユーザー beetbeet
提出日時 2019-08-30 21:37:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 224 ms / 3,000 ms
コード長 1,113 bytes
コンパイル時間 2,261 ms
コンパイル使用メモリ 208,956 KB
実行使用メモリ 34,440 KB
最終ジャッジ日時 2024-05-01 16:56:17
合計ジャッジ時間 4,620 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 213 ms
19,664 KB
testcase_01 AC 212 ms
19,676 KB
testcase_02 AC 214 ms
19,672 KB
testcase_03 AC 185 ms
34,440 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 224 ms
19,736 KB
testcase_06 AC 221 ms
19,780 KB
testcase_07 AC 216 ms
19,616 KB
testcase_08 AC 19 ms
6,944 KB
testcase_09 AC 20 ms
6,944 KB
testcase_10 AC 20 ms
6,940 KB
testcase_11 AC 19 ms
6,940 KB
testcase_12 AC 20 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 1 ms
6,944 KB
testcase_17 AC 2 ms
6,948 KB
testcase_18 AC 1 ms
6,940 KB
testcase_19 AC 2 ms
6,940 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;}


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;
}
0