結果

問題 No.872 All Tree Path
ユーザー 438kujira
提出日時 2019-10-05 11:18:52
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 231 ms / 3,000 ms
コード長 912 bytes
コンパイル時間 1,547 ms
コンパイル使用メモリ 173,716 KB
実行使用メモリ 33,664 KB
最終ジャッジ日時 2024-10-05 10:12:18
合計ジャッジ時間 5,173 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

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