結果

問題 No.872 All Tree Path
ユーザー kotatsugamekotatsugame
提出日時 2019-09-01 05:34:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 223 ms / 3,000 ms
コード長 645 bytes
コンパイル時間 574 ms
コンパイル使用メモリ 73,816 KB
実行使用メモリ 40,784 KB
最終ジャッジ日時 2023-08-18 03:33:48
合計ジャッジ時間 4,455 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 216 ms
17,000 KB
testcase_01 AC 208 ms
16,832 KB
testcase_02 AC 211 ms
17,124 KB
testcase_03 AC 198 ms
40,784 KB
testcase_04 AC 4 ms
9,512 KB
testcase_05 AC 209 ms
16,832 KB
testcase_06 AC 219 ms
16,824 KB
testcase_07 AC 223 ms
16,904 KB
testcase_08 AC 21 ms
10,336 KB
testcase_09 AC 22 ms
10,312 KB
testcase_10 AC 21 ms
10,308 KB
testcase_11 AC 21 ms
10,296 KB
testcase_12 AC 21 ms
10,288 KB
testcase_13 AC 3 ms
9,464 KB
testcase_14 AC 4 ms
9,512 KB
testcase_15 AC 3 ms
9,676 KB
testcase_16 AC 3 ms
9,532 KB
testcase_17 AC 3 ms
9,488 KB
testcase_18 AC 4 ms
9,468 KB
testcase_19 AC 4 ms
9,512 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:28:1: 警告: ISO C++ では型の無い ‘main’ の宣言を禁止しています [-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