結果
| 問題 | No.872 All Tree Path | 
| コンテスト | |
| ユーザー |  kmjp | 
| 提出日時 | 2019-08-30 21:29:07 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 124 ms / 3,000 ms | 
| コード長 | 1,122 bytes | 
| コンパイル時間 | 1,602 ms | 
| コンパイル使用メモリ | 171,064 KB | 
| 実行使用メモリ | 27,648 KB | 
| 最終ジャッジ日時 | 2024-11-21 21:56:58 | 
| 合計ジャッジ時間 | 3,615 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 18 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------
int N;
vector<pair<int,int>> E[202020];
int C[202020];
ll ret;
int dfs(int cur,int pre) {
	C[cur]=1;
	int R=0;
	FORR(e,E[cur]) {
		if(e.first==pre) {
			R=e.second;
		}
		else {
			C[cur]+=dfs(e.first,cur);
		}
	}
	ret+=1LL*R*C[cur]*(N-C[cur]);
	
	return C[cur];
}
void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	FOR(i,N-1) {
		cin>>x>>y>>r;
		E[x-1].push_back({y-1,r});
		E[y-1].push_back({x-1,r});
	}
	
	dfs(0,-1);
	cout<<ret*2<<endl;
}
int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
            
            
            
        