結果

問題 No.872 All Tree Path
ユーザー kotatsugamekotatsugame
提出日時 2019-09-01 05:34:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 290 ms / 3,000 ms
コード長 645 bytes
コンパイル時間 635 ms
コンパイル使用メモリ 73,964 KB
実行使用メモリ 40,832 KB
最終ジャッジ日時 2024-05-05 10:04:19
合計ジャッジ時間 4,973 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 285 ms
16,896 KB
testcase_01 AC 284 ms
16,780 KB
testcase_02 AC 288 ms
16,896 KB
testcase_03 AC 209 ms
40,832 KB
testcase_04 AC 5 ms
9,600 KB
testcase_05 AC 284 ms
16,896 KB
testcase_06 AC 290 ms
16,896 KB
testcase_07 AC 285 ms
16,896 KB
testcase_08 AC 25 ms
10,220 KB
testcase_09 AC 24 ms
10,368 KB
testcase_10 AC 25 ms
10,316 KB
testcase_11 AC 25 ms
10,388 KB
testcase_12 AC 25 ms
10,368 KB
testcase_13 AC 5 ms
9,600 KB
testcase_14 AC 5 ms
9,600 KB
testcase_15 AC 5 ms
9,728 KB
testcase_16 AC 5 ms
9,472 KB
testcase_17 AC 5 ms
9,600 KB
testcase_18 AC 4 ms
9,648 KB
testcase_19 AC 5 ms
9,592 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:28:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   28 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;
int N;
vector<pair<int,int> >G[2<<17];
long ans;
pair<long,int>dfs(int u,int p)
{
	vector<pair<long,int> >A;
	int cnt=1;
	for(pair<int,int>q:G[u])
	{
		int v=q.first;
		if(v==p)continue;
		pair<long,int>r=dfs(v,u);
		r.first+=q.second*r.second;
		A.push_back(r);
		cnt+=r.second;
	}
	long sum=0;
	for(pair<long,int>q:A)
	{
		sum+=q.first;
		ans+=q.first*(cnt-q.second);
	}
	return make_pair(sum,cnt);
}
main()
{
	cin>>N;
	for(int i=1;i<N;i++)
	{
		int u,v,w;cin>>u>>v>>w;
		u--,v--;
		G[u].push_back(make_pair(v,w));
		G[v].push_back(make_pair(u,w));
	}
	dfs(0,-1);
	cout<<ans*2<<endl;
}
0