結果

問題 No.872 All Tree Path
ユーザー ateate
提出日時 2019-11-20 14:08:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 322 ms / 3,000 ms
コード長 2,044 bytes
コンパイル時間 2,509 ms
コンパイル使用メモリ 206,332 KB
実行使用メモリ 26,624 KB
最終ジャッジ日時 2024-04-15 23:09:42
合計ジャッジ時間 6,815 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 319 ms
26,496 KB
testcase_01 AC 322 ms
26,624 KB
testcase_02 AC 321 ms
26,496 KB
testcase_03 AC 202 ms
25,216 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 322 ms
26,624 KB
testcase_06 AC 319 ms
26,496 KB
testcase_07 AC 319 ms
26,496 KB
testcase_08 AC 23 ms
5,504 KB
testcase_09 AC 24 ms
5,632 KB
testcase_10 AC 23 ms
5,632 KB
testcase_11 AC 23 ms
5,632 KB
testcase_12 AC 22 ms
5,504 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(ll i=0;i<(n);++i)
using ll = long long;
using pll = pair<ll,ll>;
constexpr ll INF = (1LL<<60);
constexpr ll MOD = (1e9+7);
//constexpr ll MOD = (998244353);
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}return 0;}
void dump(){cout<<endl;}
template<class T,class... Ts> void dump(T&& h, Ts&&... t){cout<<h<<", ";dump(forward<Ts>(t)...);}
template<class T> istream &operator>>(istream&is,vector<T>&v){for(auto &elemnt:v)is>>elemnt;return is;}
template<class T,class U> istream &operator>>(istream&is,pair<T,U>&p){is>>p.first>>p.second;return is;}
template<class T>vector<T> make_vector(size_t a){return vector<T>(a);}
template<class T, class... Ts>auto make_vector(size_t a, Ts... ts){return vector<decltype(make_vector<T>(ts...))>(a, make_vector<T>(ts...));}

void solve1();void solve2();
int main(){
  solve1();
  return 0;
}
using vertex = struct vertex{ll to,cost;vertex(ll to,ll cost):to(to),cost(cost){}};
using edge = struct edge{ll from,to,cost;edge(){}edge(ll from,ll to,ll cost):from(from),to(to),cost(cost){}};
using weighted_adjlist = vector<vector<vertex>>;
using weighted_edge = vector<edge>;

void solve1(){

  ll n;
  cin>>n;
  weighted_adjlist adj(n);
  weighted_edge edges(n-1);
  vector<ll> deg(n);
  rep(i,n-1){
    ll u,v,w;
    cin>>u>>v>>w;
    u--;v--;
    adj[u].emplace_back(v,w);
    adj[v].emplace_back(u,w);
    edges[i]=edge(u,v,w);
    deg[u]++;
    deg[v]++;
  }

  vector<ll> dist(n);
  queue<ll> que;
  rep(i,n){
    if(deg[i]==1){
      que.emplace(i);
    }
  }

  while(!que.empty()){
    ll now = que.front();
    que.pop();
    dist[now]++;
    for(auto nxt:adj[now]){
      if(deg[nxt.to]!=1)dist[nxt.to]+=dist[now];
      deg[nxt.to]--;
      if(deg[nxt.to]==1)que.emplace(nxt.to);
    }
  }

  ll ans = 0;
  for(auto e:edges){
    ll a = min(dist[e.from],dist[e.to]);
    ll b = n-a;
    ans += e.cost*a*b*2;
  }
  cout<<(ans)<<endl;

}
0