結果

問題 No.872 All Tree Path
ユーザー tarattata1tarattata1
提出日時 2019-08-31 01:47:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 178 ms / 3,000 ms
コード長 1,560 bytes
コンパイル時間 731 ms
コンパイル使用メモリ 81,304 KB
実行使用メモリ 47,892 KB
最終ジャッジ日時 2023-08-14 12:47:12
合計ジャッジ時間 3,421 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 178 ms
17,548 KB
testcase_01 AC 166 ms
17,568 KB
testcase_02 AC 165 ms
17,500 KB
testcase_03 AC 110 ms
47,892 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 174 ms
17,500 KB
testcase_06 AC 166 ms
17,560 KB
testcase_07 AC 167 ms
17,556 KB
testcase_08 AC 12 ms
4,380 KB
testcase_09 AC 12 ms
4,380 KB
testcase_10 AC 12 ms
4,380 KB
testcase_11 AC 12 ms
4,376 KB
testcase_12 AC 11 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <list>
#include <iterator>
#include <assert.h>
#pragma warning(disable:4996) 
 
typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
using namespace std;

vector<vector<pair<int,ll> > > g;

ll ans;

void dfs(int par, int curr, ll val[2])   // val[0]:num  val[1]:sum of dist
{
    int siz=(int)g[curr].size();
    vector<ll> val0(2*siz);
    int i;
    for(i=0; i<siz; i++) {
        int next=g[curr][i].first;
        ll dist=g[curr][i].second;
        if(next==par) continue;

        ll val0tmp[2]={0};
        dfs(curr, next, val0tmp);

        val0[2*i]=val0tmp[0];
        val0[2*i+1]=(val0tmp[1]+val0tmp[0]*dist);
        val[0]+=val0[2*i];
        val[1]+=val0[2*i+1];
    }
    for(i=0; i<siz; i++) {
        int next=g[curr][i].first;
        if(next==par) continue;

        ans += (val0[2*i+1])*(val[0]-val0[2*i]+1);
    }

    val[0]++;
    return;
}

int main(int argc, char* argv[])
{
    int n;
    scanf("%d", &n);

    g.resize(n);
    int i;
    for(i=0; i<n-1; i++) {
        int u,v,w;
        scanf("%d%d%d", &u, &v, &w);
        g[u-1].push_back(make_pair(v-1,w));
        g[v-1].push_back(make_pair(u-1,w));
    }

    ans=0;
    ll val[2]={0};
    dfs(-1, 0, val);

    printf("%lld\n", ans*2);

    return 0;
}
0