結果
問題 | No.872 All Tree Path |
ユーザー |
![]() |
提出日時 | 2019-08-30 22:28:02 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 155 ms / 3,000 ms |
コード長 | 1,290 bytes |
コンパイル時間 | 1,689 ms |
コンパイル使用メモリ | 175,648 KB |
実行使用メモリ | 36,136 KB |
最終ジャッジ日時 | 2024-11-22 00:43:56 |
合計ジャッジ時間 | 3,893 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 18 |
ソースコード
#define _USE_MATH_DEFINES#include <bits/stdc++.h>using namespace std;//template#define rep(i,a,b) for(int i=(a);i<(b);i++)#define rrep(i,a,b) for(int i=(a);i>(b);i--)#define ALL(v) (v).begin(),(v).end()typedef long long int ll; typedef pair<ll, ll> P;template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }template<typename A,size_t N,typename T>void Fill(A(&array)[N],const T &val){fill((T*)array, (T*)(array+N), val);}const int inf = INT_MAX / 2; const ll INF = LLONG_MAX / 2;//template endvector<vector<P>> g;ll b[200010];ll dfs(int v,int p){if(g[v].size()==0)return b[v]=1;ll res=1;for(P x:g[v])if(x.first!=p){res+=dfs(x.first,v);}return b[v]=res;}int main(){int n; scanf("%d",&n);g.resize(n); vector<pair<int,P>> es;rep(i,0,n-1){int u,v,w; scanf("%d%d%d",&u,&v,&w); u--; v--;g[u].push_back({v,w}); g[v].push_back({u,w});es.push_back({u,{v,w}});}ll ans=0; dfs(0,-1);rep(i,0,n-1){ll d=es[i].second.second;ll m=min(b[es[i].first],b[es[i].second.first]);ans+=m*(n-m)*d*2LL;}printf("%lld\n",ans);return 0;}