結果

問題 No.872 All Tree Path
ユーザー 沙耶花沙耶花
提出日時 2019-08-30 21:57:42
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 785 ms / 3,000 ms
コード長 1,181 bytes
コンパイル時間 1,821 ms
コンパイル使用メモリ 183,816 KB
実行使用メモリ 41,000 KB
最終ジャッジ日時 2023-08-14 04:56:34
合計ジャッジ時間 9,229 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 738 ms
40,812 KB
testcase_01 AC 723 ms
40,704 KB
testcase_02 AC 760 ms
40,656 KB
testcase_03 AC 563 ms
39,660 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 785 ms
40,852 KB
testcase_06 AC 746 ms
41,000 KB
testcase_07 AC 753 ms
40,776 KB
testcase_08 AC 47 ms
6,984 KB
testcase_09 AC 48 ms
6,688 KB
testcase_10 AC 47 ms
6,792 KB
testcase_11 AC 47 ms
6,716 KB
testcase_12 AC 48 ms
6,848 KB
testcase_13 AC 1 ms
4,384 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 1000000000000


int main(){
	
	int N;
	
	cin>>N;
	
	vector<vector<pair<int,int>>> E(N,vector<pair<int,int>>());
	
	vector<int> jisuu(N,0);
	
	for(int i=0;i<N-1;i++){
		int a,b,w;
		cin>>a>>b>>w;
		a--;b--;
		
		E[a].emplace_back(b,w);
		E[b].emplace_back(a,w);
		
		jisuu[a]++;
		jisuu[b]++;
		
	}
	
	queue<int> Q;
	for(int i=0;i<N;i++){
		if(jisuu[i]==1)Q.push(i);
	}
	
	map<pair<int,int>,int> M;
	
	while(Q.size()!=0){
		int now = Q.front();
		Q.pop();
		
		int cnt = 1;
		int ind = -1;
		for(int i=0;i<E[now].size();i++){
			if(M[make_pair(now,E[now][i].first)]==0){
				ind = E[now][i].first;
			}
			else{
				cnt+=M[make_pair(now,E[now][i].first)];
			}
		}
		if(ind==-1)continue;
		M[make_pair(now,ind)]=cnt;
		M[make_pair(ind,now)]=cnt;
		jisuu[ind]--;
		if(jisuu[ind]==1){
			Q.push(ind);
		}
	}
		
	long long ans = 0;
	
	for(int i=0;i<N;i++){
		for(int j=0;j<E[i].size();j++){
			long long a = M[make_pair(i,E[i][j].first)];
			long long x = a*(N-a);
			x *= E[i][j].second;
			ans += x;
		}
	}
	
	cout<<ans<<endl;	
	
	return 0;
}
0