結果

問題 No.872 All Tree Path
ユーザー onakaT_TitaionakaT_Titai
提出日時 2019-06-15 16:45:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,981 bytes
コンパイル時間 928 ms
コンパイル使用メモリ 105,992 KB
実行使用メモリ 19,456 KB
最終ジャッジ日時 2023-08-12 04:35:14
合計ジャッジ時間 8,151 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 AC 2 ms
4,376 KB
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 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[100005];
int childCnt[100005];
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);
    int ans = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < G[i].size(); j++) {
            Edge e = G[i][j];
            int weight = min(childCnt[e.from], childCnt[e.to]) + 1;
            weight *= (N - weight);
            weight *= e.cost;
            ans += weight;
        }
    }
    cout << ans << endl;
}   
0