結果
| 問題 |
No.872 All Tree Path
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-08-30 22:48:30 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 170 ms / 3,000 ms |
| コード長 | 1,356 bytes |
| コンパイル時間 | 1,314 ms |
| コンパイル使用メモリ | 81,880 KB |
| 実行使用メモリ | 35,968 KB |
| 最終ジャッジ日時 | 2024-11-22 01:48:14 |
| 合計ジャッジ時間 | 3,871 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 18 |
ソースコード
#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
#define int long long
#define endl "\n"
const long long INF = (long long)1e18;
// const long long MOD = 1'000'000'007;
string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}
int N, result;
vector<vector<pair<int,int>>> tree;
vector<int> con, ans;
void dfs1(int per = -1, int now = 1){
con[now]++;
for(int i = 0; i < tree[now].size(); i++){
if(tree[now][i].first == per) continue;
dfs1(now, tree[now][i].first);
con[now] += con[tree[now][i].first];
ans[now] += ans[tree[now][i].first] + con[tree[now][i].first]*tree[now][i].second;
}
}
void dfs2(int per = -1, int now = 1, int sum = ans[1]){
int res = sum;
result += res;
for(pair<int,int> next : tree[now]){
if(per == next.first) continue;
dfs2(now, next.first, res + (N - con[next.first])*next.second - (con[next.first])*next.second);
}
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(false);
cout<<fixed<<setprecision(10);
cin>>N;
tree.resize(N+1);
con.resize(N+1);
ans.resize(N+1);
for(int i = 0; i < N-1; i++){
int u, v, w;
cin>>u>>v>>w;
tree[u].push_back(make_pair(v,w));
tree[v].push_back(make_pair(u,w));
}
dfs1();
dfs2();
cout<<result<<endl;
return 0;
}