結果

問題 No.872 All Tree Path
ユーザー 438kujira438kujira
提出日時 2019-10-05 11:18:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 308 ms / 3,000 ms
コード長 912 bytes
コンパイル時間 2,096 ms
コンパイル使用メモリ 169,732 KB
実行使用メモリ 33,664 KB
最終ジャッジ日時 2024-04-15 13:33:18
合計ジャッジ時間 5,906 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 302 ms
18,860 KB
testcase_01 AC 308 ms
18,904 KB
testcase_02 AC 299 ms
18,916 KB
testcase_03 AC 213 ms
33,664 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 294 ms
18,840 KB
testcase_06 AC 305 ms
18,816 KB
testcase_07 AC 299 ms
18,816 KB
testcase_08 AC 24 ms
5,632 KB
testcase_09 AC 23 ms
5,632 KB
testcase_10 AC 23 ms
5,632 KB
testcase_11 AC 23 ms
5,632 KB
testcase_12 AC 23 ms
5,504 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define LLINF 9223372036854775807
#define MOD ll(1e9+7)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cerr<<#x<<": "<<x<<endl

vector<vector<pll>> graph;
vector<int> visited(200005,-1);
ll ans = 0;

ll dfs(ll now, ll n, ll w){
    visited[now] = 1;
    ll ret = 1;
    for(int i = 0; i < graph[now].size(); i++){
        if(visited[graph[now][i].first] == -1){
            ret += dfs(graph[now][i].first, n, graph[now][i].second);
        }
    }
    ans += 2*ret*(n-ret)*w;
    return ret;
}

int main(){
    ll n;
    cin >> n;

    graph.resize(n);
    for(int i = 0; i < n-1; i++){
        ll a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        graph[a].push_back({b,c});
        graph[b].push_back({a,c});
    }

    dfs(0,n,0);
    cout << ans << endl;
    return 0;
}
0