結果

問題 No.872 All Tree Path
ユーザー E31415926E31415926
提出日時 2019-08-30 22:27:09
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 262 ms / 3,000 ms
コード長 751 bytes
コンパイル時間 1,543 ms
コンパイル使用メモリ 168,920 KB
実行使用メモリ 30,044 KB
最終ジャッジ日時 2023-08-14 05:59:42
合計ジャッジ時間 4,977 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 259 ms
18,180 KB
testcase_01 AC 259 ms
18,276 KB
testcase_02 AC 262 ms
18,528 KB
testcase_03 AC 190 ms
30,044 KB
testcase_04 AC 4 ms
8,156 KB
testcase_05 AC 256 ms
18,176 KB
testcase_06 AC 256 ms
18,232 KB
testcase_07 AC 262 ms
18,232 KB
testcase_08 AC 23 ms
9,016 KB
testcase_09 AC 23 ms
9,136 KB
testcase_10 AC 23 ms
9,024 KB
testcase_11 AC 22 ms
9,020 KB
testcase_12 AC 22 ms
9,004 KB
testcase_13 AC 4 ms
8,204 KB
testcase_14 AC 4 ms
7,992 KB
testcase_15 AC 5 ms
8,004 KB
testcase_16 AC 5 ms
8,008 KB
testcase_17 AC 5 ms
8,072 KB
testcase_18 AC 5 ms
8,004 KB
testcase_19 AC 4 ms
8,208 KB
権限があれば一括ダウンロードができます

ソースコード

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