結果

問題 No.872 All Tree Path
ユーザー 👑 kmjpkmjp
提出日時 2019-08-30 21:29:07
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 152 ms / 3,000 ms
コード長 1,122 bytes
コンパイル時間 1,524 ms
コンパイル使用メモリ 170,436 KB
実行使用メモリ 27,608 KB
最終ジャッジ日時 2023-08-14 03:53:20
合計ジャッジ時間 4,021 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 152 ms
16,372 KB
testcase_01 AC 149 ms
16,224 KB
testcase_02 AC 142 ms
16,264 KB
testcase_03 AC 84 ms
27,608 KB
testcase_04 AC 5 ms
8,108 KB
testcase_05 AC 149 ms
16,236 KB
testcase_06 AC 148 ms
16,164 KB
testcase_07 AC 138 ms
16,252 KB
testcase_08 AC 13 ms
8,908 KB
testcase_09 AC 13 ms
8,916 KB
testcase_10 AC 13 ms
8,892 KB
testcase_11 AC 13 ms
8,972 KB
testcase_12 AC 13 ms
8,912 KB
testcase_13 AC 4 ms
8,096 KB
testcase_14 AC 5 ms
8,172 KB
testcase_15 AC 4 ms
8,152 KB
testcase_16 AC 5 ms
8,176 KB
testcase_17 AC 4 ms
8,156 KB
testcase_18 AC 4 ms
8,096 KB
testcase_19 AC 4 ms
8,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0