結果

問題 No.872 All Tree Path
ユーザー hipopohipopo
提出日時 2020-01-18 18:52:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,232 bytes
コンパイル時間 1,146 ms
コンパイル使用メモリ 111,116 KB
実行使用メモリ 27,220 KB
最終ジャッジ日時 2023-09-10 09:01:12
合計ジャッジ時間 4,569 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using ll = long long;
const long long MOD = 1e9+7;

struct Edge {
    int u, v;
    ll w;
};

int max_v = 200000;
vector<vector<int>> graph(max_v);
vector<int> cnt_child(max_v);
vector<bool> done(max_v);
int dfs(int v) {
    if (done.at(v)) return 0;
    
    done.at(v) = true;
    int res = 1;
    for (int to: graph.at(v)) res += dfs(to);
    return cnt_child.at(v) = res;
}

int main() { 
    int n;
    cin >> n;
    vector<Edge> edges(n - 1);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        ll w;
        cin >> u >> v >> w;
        u--;
        v--;
        
        graph.at(u).push_back(v);
        graph.at(v).push_back(u);
        edges.at(i) = Edge{u, v, w};
    }

    dfs(0);
    
    ll sum = 0;
    for (Edge e: edges) {
        ll m = min(cnt_child.at(e.u), cnt_child.at(e.v));
        sum += m * (n - m) * e.w;
    }
    cout << sum << endl;
}   
0