結果

問題 No.872 All Tree Path
ユーザー outlineoutline
提出日時 2021-01-06 08:36:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 170 ms / 3,000 ms
コード長 1,663 bytes
コンパイル時間 1,602 ms
コンパイル使用メモリ 140,656 KB
実行使用メモリ 22,912 KB
最終ジャッジ日時 2024-04-24 10:37:19
合計ジャッジ時間 4,046 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 169 ms
16,000 KB
testcase_01 AC 170 ms
16,000 KB
testcase_02 AC 160 ms
16,000 KB
testcase_03 AC 85 ms
22,912 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 159 ms
16,000 KB
testcase_06 AC 163 ms
16,000 KB
testcase_07 AC 166 ms
16,000 KB
testcase_08 AC 12 ms
5,376 KB
testcase_09 AC 11 ms
5,376 KB
testcase_10 AC 12 ms
5,376 KB
testcase_11 AC 11 ms
5,376 KB
testcase_12 AC 12 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vector<vector<P>> g(n);
    for(int i = 1; i < n; ++i){
        int u, v, w;
        cin >> u >> v >> w;
        --u, --v;
        g[u].emplace_back(v, w);
        g[v].emplace_back(u, w);
    }

    vector<int> subtree_size(n, 1);
    ll ans = 0;
    auto dfs = [&](auto&& self, int from = 0, int par = -1) -> int {
        for(auto [to, cost] : g[from]){
            if(to == par)   continue;
            subtree_size[from] += self(self, to, from);
            ans += (ll)cost * subtree_size[to] * (n - subtree_size[to]);
        }
        return subtree_size[from];
    };
    dfs(dfs);
    
    cout << ans * 2 << endl;

    return 0;
}
0