結果

問題 No.872 All Tree Path
ユーザー E31415926E31415926
提出日時 2019-08-30 22:27:09
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 287 ms / 3,000 ms
コード長 751 bytes
コンパイル時間 1,928 ms
コンパイル使用メモリ 171,132 KB
実行使用メモリ 29,992 KB
最終ジャッジ日時 2024-11-22 00:41:22
合計ジャッジ時間 5,454 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

#define int long long
using namespace std;
#define rep(i, n) for(int i=0;i<(n);++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound

int N;
vector<pii> G[200200];
int Ans;

int dfs(int v, int p) {
    int children = 0;
    for (pii edge:G[v]) {
        if (edge.first == p)continue;
        int ch = dfs(edge.first, v) + 1;
        Ans += edge.second * (N - ch) * ch;
        children += ch;
    }
    return children;
}

signed main() {
    cin >> N;
    rep(i, N - 1) {
        int u, v, w;
        cin >> u >> v >> w;
        --u, --v;
        G[u].emplace_back(v, w);
        G[v].emplace_back(u, w);
    }
    dfs(0, -1);
    cout << 2 * Ans << endl;
    return 0;
}
0