結果

問題 No.872 All Tree Path
ユーザー treeonetreeone
提出日時 2019-08-30 21:55:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 131 ms / 3,000 ms
コード長 908 bytes
コンパイル時間 2,600 ms
コンパイル使用メモリ 206,584 KB
実行使用メモリ 28,356 KB
最終ジャッジ日時 2024-05-01 17:39:12
合計ジャッジ時間 3,878 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
19,912 KB
testcase_01 AC 120 ms
19,780 KB
testcase_02 AC 128 ms
19,908 KB
testcase_03 AC 81 ms
28,356 KB
testcase_04 AC 3 ms
8,388 KB
testcase_05 AC 131 ms
19,844 KB
testcase_06 AC 126 ms
19,780 KB
testcase_07 AC 131 ms
19,784 KB
testcase_08 AC 12 ms
9,316 KB
testcase_09 AC 13 ms
10,056 KB
testcase_10 AC 12 ms
9,324 KB
testcase_11 AC 11 ms
9,404 KB
testcase_12 AC 13 ms
9,544 KB
testcase_13 AC 3 ms
8,640 KB
testcase_14 AC 3 ms
8,896 KB
testcase_15 AC 3 ms
8,640 KB
testcase_16 AC 3 ms
8,072 KB
testcase_17 AC 3 ms
8,264 KB
testcase_18 AC 3 ms
9,544 KB
testcase_19 AC 3 ms
8,900 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