結果

問題 No.872 All Tree Path
ユーザー hedwig100
提出日時 2020-04-14 12:52:29
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 853 bytes
コンパイル時間 1,691 ms
コンパイル使用メモリ 173,824 KB
実行使用メモリ 29,764 KB
最終ジャッジ日時 2024-10-01 16:56:50
合計ジャッジ時間 4,706 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 6 WA * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i ++)
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PL;
typedef pair<int,int> P;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const vector<int> dy = {-1,0,1,0};
const vector<int> dx = {0,1,0,-1};

const int MAXN = 200005;
vector<vector<P>> G(MAXN);

ll ans = 0;
int dfs(int i,int n,int p = -1) {
    int ret = 1;
    for (P pa:G[i]) {
        int e = pa.first;
        int c = pa.second;
        if (e == p) continue;
        int tmp = dfs(e,n,i);
        ans += 2 * tmp * (n - tmp) * c;
        ret += tmp;
    }
    return ret;
}

int main() {
    int n; cin >> n;
    rep(_,n - 1) {
        int a,b,c; cin >> a >> b >> c;
        a --; b--;
        G[a].push_back(P(b,c));
        G[b].push_back(P(a,c));
    }
    dfs(0,n);
    cout << ans <<  endl;
    return 0;
}
0