結果

問題 No.872 All Tree Path
ユーザー treeonetreeone
提出日時 2019-08-30 21:55:58
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 192 ms / 3,000 ms
コード長 908 bytes
コンパイル時間 2,104 ms
コンパイル使用メモリ 202,612 KB
実行使用メモリ 28,432 KB
最終ジャッジ日時 2023-08-14 04:52:53
合計ジャッジ時間 5,565 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 188 ms
19,820 KB
testcase_01 AC 192 ms
19,780 KB
testcase_02 AC 191 ms
19,792 KB
testcase_03 AC 89 ms
28,432 KB
testcase_04 AC 3 ms
8,072 KB
testcase_05 AC 177 ms
20,048 KB
testcase_06 AC 172 ms
19,812 KB
testcase_07 AC 189 ms
19,792 KB
testcase_08 AC 14 ms
9,504 KB
testcase_09 AC 14 ms
9,208 KB
testcase_10 AC 13 ms
9,284 KB
testcase_11 AC 14 ms
9,256 KB
testcase_12 AC 13 ms
9,196 KB
testcase_13 AC 4 ms
8,332 KB
testcase_14 AC 3 ms
8,068 KB
testcase_15 AC 3 ms
8,068 KB
testcase_16 AC 3 ms
8,112 KB
testcase_17 AC 4 ms
8,032 KB
testcase_18 AC 3 ms
8,072 KB
testcase_19 AC 3 ms
8,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define int long long
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e18;

int n;
vector<P> G[200010];
int sz[200010];
int ans = 0;

int dfs(int v, int p){
    int cnt = 1;
    for(auto i : G[v]){
        if(i.first == p) continue;
        cnt += dfs(i.first, v);
    }
    sz[v] = cnt;
    return cnt;
}

void dfs2(int v, int p){
    for(auto i : G[v]){
        if(i.first == p) continue;
        ans += i.second * sz[i.first] * (n - sz[i.first]);
        dfs2(i.first, v);
    }
}

signed main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin >> n;
    rep(i, 0, n - 1){
        int u, v, w;
        cin >> u >> v >> w;
        u--; v--;
        G[u].push_back({v, w});
        G[v].push_back({u, w});
    }
    dfs(0, -1);
    dfs2(0, -1);
    cout << ans * 2 << endl;
}
0