結果

問題 No.872 All Tree Path
ユーザー tarattata1tarattata1
提出日時 2019-08-31 01:45:02
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,566 bytes
コンパイル時間 787 ms
コンパイル使用メモリ 77,776 KB
実行使用メモリ 41,856 KB
最終ジャッジ日時 2024-05-02 01:01:11
合計ジャッジ時間 2,545 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 1 ms
5,376 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main(int, char**)’:
main.cpp:60:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   60 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
main.cpp:66:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   66 |         scanf("%d%d%d", &u, &v, &w);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~

ソースコード

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,int> > > g;

ll ans;

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

        int 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;
    int val[2]={0};
    dfs(-1, 0, val);

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

    return 0;
}
0