結果
| 問題 |
No.872 All Tree Path
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-01-14 06:42:03 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 142 ms / 3,000 ms |
| コード長 | 1,992 bytes |
| コンパイル時間 | 1,214 ms |
| コンパイル使用メモリ | 123,640 KB |
| 実行使用メモリ | 31,444 KB |
| 最終ジャッジ日時 | 2024-12-24 04:18:17 |
| 合計ジャッジ時間 | 4,160 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 18 |
ソースコード
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const long long MAX = 5100000;
const long long INF = 1LL << 60;
const long long mod = 1000000007LL;
//const long long mod = 998244353LL;
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ll N;
vector<vector<int>> g;
vector<ll> cnt;
vector<ll> h;
void dfs1(ll cur, ll pre) {
for (auto next : g[cur]) {
if (next == pre) continue;
h[next] = h[cur] + 1;
dfs1(next, cur);
}
}
ll dfs2(ll cur, ll pre) {
ll res = 1;
for (auto next : g[cur]) {
if (next == pre) continue;
res += dfs2(next, cur);
}
return cnt[cur] = res;
}
class Edge {
public:
ll source, target, cost;
Edge(ll source = 0, ll target = 0, ll cost = 0) :
source(source), target(target), cost(cost) {}
bool operator<(const Edge &e)const {
return cost < e.cost;
}
};
int main()
{
/*
cin.tie(nullptr);
ios::sync_with_stdio(false);
*/
scanf("%lld", &N);
g.resize(N);
cnt = vector<ll>(N);
h = vector<ll>(N);
vector<Edge> edges;
for (ll i = 0; i < N - 1; i++) {
ll u, v, w; scanf("%lld %lld %lld", &u, &v, &w);
u--; v--;
if (u > v) swap(u, v);
g[u].emplace_back(v);
g[v].emplace_back(u);
edges.emplace_back(u, v, w);
}
dfs1(0, -1);
dfs2(0, -1);
ll res = 0;
for (auto p : edges) {
ll u = p.source;
ll v = p.target;
if (h[u] > h[v]) swap(u, v);
res += cnt[v] * (N - cnt[v]) * p.cost * 2;
}
cout << res << endl;
return 0;
}