結果
問題 |
No.872 All Tree Path
|
ユーザー |
![]() |
提出日時 | 2019-08-31 00:52:03 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 76 ms / 3,000 ms |
コード長 | 1,138 bytes |
コンパイル時間 | 716 ms |
コンパイル使用メモリ | 79,348 KB |
実行使用メモリ | 13,056 KB |
最終ジャッジ日時 | 2024-11-22 07:07:13 |
合計ジャッジ時間 | 2,407 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 18 |
ソースコード
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <cstdio> #include <cstring> #include <cmath> using namespace std; using ll = long long; ll r = 0; struct Graph { struct Vertex { int n; }; struct Edge { int i, n, c; }; Graph(int n, int m) : v(n, { -1 }), e(m), n(n), m(0) {} void add_edge(int i, int j, int c) { e[m] = { j, v[i].n, c }; v[i].n = m; m++; } int dfs(int i, int p) { int k = 1; for (int j = v[i].n; j >= 0; j = e[j].n) { Edge &o = e[j]; if (o.i == p) continue; int l = dfs(o.i, i); r += (ll)l * (n - l) * o.c; k += l; } return k; } vector<Vertex> v; vector<Edge> e; int n, m; }; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; Graph g(n, (n - 1) * 2); for (int i = 0; i < n - 1; i++) { int a, b, c; cin >> a >> b >> c; a--; b--; g.add_edge(a, b, c); g.add_edge(b, a, c); } g.dfs(0, -1); cout << r * 2 << endl; return 0; }