結果

問題 No.872 All Tree Path
ユーザー NOSSNOSS
提出日時 2019-08-30 22:03:39
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 270 ms / 3,000 ms
コード長 1,150 bytes
コンパイル時間 1,814 ms
コンパイル使用メモリ 171,408 KB
実行使用メモリ 29,544 KB
最終ジャッジ日時 2023-08-14 05:12:11
合計ジャッジ時間 5,528 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 265 ms
17,904 KB
testcase_01 AC 270 ms
17,868 KB
testcase_02 AC 268 ms
18,096 KB
testcase_03 AC 191 ms
29,544 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 262 ms
17,972 KB
testcase_06 AC 264 ms
17,688 KB
testcase_07 AC 264 ms
18,100 KB
testcase_08 AC 21 ms
4,632 KB
testcase_09 AC 20 ms
4,540 KB
testcase_10 AC 21 ms
4,536 KB
testcase_11 AC 20 ms
4,532 KB
testcase_12 AC 20 ms
4,688 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 1 ms
4,352 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,356 KB
testcase_17 AC 1 ms
4,356 KB
testcase_18 AC 2 ms
4,356 KB
testcase_19 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> P3;
typedef pair<P, P> PP;
constexpr ll MOD = ll(1e9 + 7);
constexpr int IINF = INT_MAX;
constexpr ll LLINF = LLONG_MAX;
constexpr int MAX_N = int(1e5) + 5;
constexpr double EPS = 1e-9;
constexpr int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), (v).end()

vector<vector<P> > g;
ll n;
ll ans = 0;

ll dfs(int v, int p){
    ll res = 0;
    for(auto e : g[v]){
        int u = e.first;
        if(u == p) continue;
        ll cnt = dfs(u, v);
        ans += cnt*(n-cnt)*e.second;
        res += cnt;
    }
    return res+1;
}


int main() {
    cin >> n;
    g.resize(n);
    for(int i=0;i<n-1;i++){
        ll u, v, w;
        cin >> u >> v >> w;
        u--; v--;
        g[u].push_back({v, w});
        g[v].push_back({u, w});
    }
    dfs(0,-1);
    cout << ans*2 << endl;
    return 0;
}
0