結果

問題 No.872 All Tree Path
ユーザー onakaT_TitaionakaT_Titai
提出日時 2019-06-15 16:51:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 254 ms / 3,000 ms
コード長 1,993 bytes
コンパイル時間 921 ms
コンパイル使用メモリ 106,288 KB
実行使用メモリ 24,872 KB
最終ジャッジ日時 2023-08-12 04:55:31
合計ジャッジ時間 4,228 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 246 ms
17,520 KB
testcase_01 AC 232 ms
17,496 KB
testcase_02 AC 254 ms
17,444 KB
testcase_03 AC 178 ms
24,872 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 237 ms
17,560 KB
testcase_06 AC 248 ms
17,508 KB
testcase_07 AC 241 ms
17,520 KB
testcase_08 AC 20 ms
4,536 KB
testcase_09 AC 19 ms
4,652 KB
testcase_10 AC 20 ms
4,640 KB
testcase_11 AC 20 ms
4,528 KB
testcase_12 AC 20 ms
4,464 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <numeric>
#include <functional>
#include <cctype>
#include <list>
#include <limits>
#include <cassert>
//#include <boost/multiprecision/cpp_int.hpp>

const double EPS = (1e-10);


using namespace std;
using Int = long long;
//using namespace boost::multiprecision;

const Int MOD = 1000000007;

long long mod_pow(long long x, long long n) {
    long long res = 1;
    for(int i = 0;i < 60; i++){
        if(n >> i & 1) res = res * x % MOD;
        x = x * x % MOD;
    }
    return res;
}

template<typename T>
T gcd(T a, T b) {
    return b != 0 ? gcd(b, a % b) : a;
}

template<typename T>
T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

void fast_input() {
    cin.tie(0);
    ios::sync_with_stdio(false);
}

struct Edge{
    int from, to, cost;
};

int v[200005];
int childCnt[200005];
vector<vector<Edge>> G;

int dfs(int crt) {
    v[crt] = 1;
    int numOfChild = 0;
    for (int i = 0; i < G[crt].size(); i++) {
        if (v[G[crt][i].to] == 0) {
            numOfChild += dfs(G[crt][i].to) + 1;
        }
    }
    return childCnt[crt] = numOfChild;
}

int main(void) {
    int N; cin >> N;
    G = vector<vector<Edge>>(N);
    for (int i = 0; i < N-1; i++) {
        int u, v, w; cin >> u >> v >> w;
        u--, v--;
        G[u].push_back({u, v, w});
        G[v].push_back({v, u, w});
    }
    
    dfs(0);
    long long ans = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < G[i].size(); j++) {
            Edge e = G[i][j];
            long long weight = min(childCnt[e.from], childCnt[e.to]) + 1;
            weight *= (N - weight);
            weight *= e.cost;
            ans += weight;
        }
    }
    cout << ans << endl;
}   
0