結果

問題 No.872 All Tree Path
ユーザー firiexpfiriexp
提出日時 2019-08-30 21:49:59
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 205 ms / 3,000 ms
コード長 1,524 bytes
コンパイル時間 1,827 ms
コンパイル使用メモリ 107,596 KB
実行使用メモリ 20,828 KB
最終ジャッジ日時 2023-08-15 23:53:06
合計ジャッジ時間 5,307 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 193 ms
20,828 KB
testcase_01 AC 195 ms
20,768 KB
testcase_02 AC 189 ms
20,380 KB
testcase_03 AC 88 ms
20,352 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 205 ms
20,780 KB
testcase_06 AC 189 ms
20,684 KB
testcase_07 AC 189 ms
20,460 KB
testcase_08 AC 13 ms
4,768 KB
testcase_09 AC 12 ms
4,704 KB
testcase_10 AC 13 ms
4,832 KB
testcase_11 AC 13 ms
4,772 KB
testcase_12 AC 12 ms
4,904 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <limits>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template <typename T>
struct edge {
    int from, to; T cost;
    edge(int to, T cost) : from(-1), to(to), cost(cost) {}
    edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
};

int main() {
    int n;
    cin >> n;
    vector<vector<int>> G(n);
    vector<edge<ll>> e;
    for (int i = 0; i < n-1; ++i) {
        int u, v, w;
        scanf("%d %d %d", &u, &v, &w);
        u--; v--;
        G[u].emplace_back(v);
        G[v].emplace_back(u);
        e.emplace_back(u, v, w);
    }
    ll ans = 0;
    deque<int> Q;
    stack<int> s;
    int cnt = 0;
    vector<int> visited(n, 0), num(n);
    s.emplace(0);
    while(!s.empty()){
        int a = s.top(); s.pop();
        visited[a]++;
        num[a] = cnt++;
        Q.emplace_front(a);
        for (auto &&i : G[a]) {
            if(!visited[i]) s.emplace(i);
        }
    }
    vector<int> dp(n, 1);
    for (auto &&i : Q) {
        for (auto &&j : G[i]) {
            if(num[i] > num[j]) dp[j] += dp[i];
        }
    }
    for (auto &&i : e) {
        ans += (ll)(n-min(dp[i.from], dp[i.to]))*min(dp[i.from], dp[i.to])*i.cost;
    }
    cout << ans*2 << "\n";
    return 0;
}
0