結果
| 問題 |
No.872 All Tree Path
|
| コンテスト | |
| ユーザー |
siman
|
| 提出日時 | 2021-08-02 18:37:33 |
| 言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 353 ms / 3,000 ms |
| コード長 | 1,203 bytes |
| コンパイル時間 | 1,230 ms |
| コンパイル使用メモリ | 143,352 KB |
| 実行使用メモリ | 47,232 KB |
| 最終ジャッジ日時 | 2024-09-16 14:41:11 |
| 合計ジャッジ時間 | 4,959 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 18 |
ソースコード
#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>
using namespace std;
typedef long long ll;
const int MAX_N = 200010;
struct Edge {
int v;
ll w;
Edge(int v = -1, ll w = -1) {
this->v = v;
this->w = w;
}
};
vector<Edge> E[MAX_N];
vector<int> V[MAX_N];
ll ans;
int N;
int dfs1(int u, int parent = -1) {
int cnt = 0;
for (Edge &e : E[u]) {
if (e.v == parent) continue;
int v_cnt = dfs1(e.v, u);
cnt += v_cnt;
V[u].push_back(v_cnt);
}
return cnt + 1;
}
void dfs2(int u, int v_cnt = 0, int parent = -1) {
int t_cnt = v_cnt;
for (int c : V[u]) {
t_cnt += c;
}
int i = 0;
for (Edge &e : E[u]) {
if (e.v == parent) continue;
int cnt = V[u][i];
ans += 2 * e.w * (N - cnt) * cnt;
dfs2(e.v, t_cnt - cnt + 1, u);
i += 1;
}
}
int main() {
cin >> N;
int u, v;
ll w;
for (int i = 0; i < N - 1; ++i) {
cin >> u >> v >> w;
E[u].push_back(Edge(v, w));
E[v].push_back(Edge(u, w));
}
ans = 0;
dfs1(1);
dfs2(1);
cout << ans << endl;
return 0;
}
siman