結果

問題 No.872 All Tree Path
ユーザー hipopohipopo
提出日時 2020-01-18 18:52:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 268 ms / 3,000 ms
コード長 1,236 bytes
コンパイル時間 1,188 ms
コンパイル使用メモリ 109,480 KB
実行使用メモリ 27,288 KB
最終ジャッジ日時 2023-09-10 09:03:17
合計ジャッジ時間 4,868 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 258 ms
18,044 KB
testcase_01 AC 254 ms
18,052 KB
testcase_02 AC 260 ms
17,908 KB
testcase_03 AC 184 ms
27,288 KB
testcase_04 AC 5 ms
8,500 KB
testcase_05 AC 261 ms
17,916 KB
testcase_06 AC 268 ms
17,980 KB
testcase_07 AC 262 ms
17,976 KB
testcase_08 AC 23 ms
9,688 KB
testcase_09 AC 23 ms
9,768 KB
testcase_10 AC 23 ms
9,700 KB
testcase_11 AC 23 ms
9,704 KB
testcase_12 AC 22 ms
9,624 KB
testcase_13 AC 5 ms
8,508 KB
testcase_14 AC 5 ms
8,300 KB
testcase_15 AC 5 ms
8,324 KB
testcase_16 AC 5 ms
8,280 KB
testcase_17 AC 5 ms
8,396 KB
testcase_18 AC 4 ms
8,432 KB
testcase_19 AC 5 ms
8,492 KB
権限があれば一括ダウンロードができます

ソースコード

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 * 2 << endl;
}   
0