結果

問題 No.872 All Tree Path
ユーザー yukudoyukudo
提出日時 2019-08-31 21:08:51
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 161 ms / 3,000 ms
コード長 1,310 bytes
コンパイル時間 1,334 ms
コンパイル使用メモリ 147,080 KB
実行使用メモリ 32,240 KB
最終ジャッジ日時 2023-08-16 17:53:26
合計ジャッジ時間 4,240 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 157 ms
17,340 KB
testcase_01 AC 156 ms
17,412 KB
testcase_02 AC 160 ms
17,380 KB
testcase_03 AC 100 ms
32,240 KB
testcase_04 AC 5 ms
10,120 KB
testcase_05 AC 156 ms
17,424 KB
testcase_06 AC 161 ms
17,492 KB
testcase_07 AC 155 ms
17,436 KB
testcase_08 AC 16 ms
10,872 KB
testcase_09 AC 15 ms
10,816 KB
testcase_10 AC 15 ms
10,812 KB
testcase_11 AC 16 ms
11,016 KB
testcase_12 AC 15 ms
10,880 KB
testcase_13 AC 6 ms
10,144 KB
testcase_14 AC 5 ms
10,188 KB
testcase_15 AC 5 ms
10,148 KB
testcase_16 AC 5 ms
10,064 KB
testcase_17 AC 5 ms
10,280 KB
testcase_18 AC 6 ms
10,216 KB
testcase_19 AC 5 ms
10,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}

int nextInt() { int x; scanf("%d", &x); return x;}

struct Edge {
  int to;
  int w;
};
const int MAX_N = 212345;
vector<Edge> g[MAX_N];

int N;
ll size[MAX_N];
ll ans;
void dfs(int cur, int prev, int d) {
  size[cur] = 1;
  for (Edge e : g[cur]) if (e.to != prev) {
    dfs(e.to, cur, e.w);
    size[cur] += size[e.to];
  }
  if (prev != -1) {
    ans += (ll)d * size[cur] * (N - size[cur]) * 2;
  }
}
int main2() {
  REP(i, MAX_N) g[i].clear();
  CLR(size, 0);

  N = nextInt();
  REP(i, N-1) {
    int a = nextInt() - 1;
    int b = nextInt() - 1;
    int w = nextInt();
    g[a].push_back({b, w});
    g[b].push_back({a, w});
  }

  ans = 0;
  dfs(0, -1, 0);
  cout << ans << endl;
  return 0;
}

int main() {

#ifdef LOCAL
  for (;!cin.eof();cin>>ws)
#endif
    main2();
  return 0;
}
0