結果
| 問題 |
No.3348 Tree Balance
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-10-29 16:55:07 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,492 bytes |
| コンパイル時間 | 1,230 ms |
| コンパイル使用メモリ | 120,084 KB |
| 実行使用メモリ | 42,560 KB |
| 最終ジャッジ日時 | 2025-11-13 21:04:17 |
| 合計ジャッジ時間 | 13,901 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | TLE * 1 -- * 24 |
ソースコード
#include <algorithm>
#include <cassert>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) begin(v), end(v)
template <class T> bool chmin(T &dst, T src) {
if (dst > src) {
dst = src;
return true;
}
return false;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<int> w(n);
rep(i, n) cin >> w[i];
vector<vector<int>> graph(n);
vector<pair<int, int>> edges(n - 1);
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
graph[a].emplace_back(b);
graph[b].emplace_back(a);
edges[i] = {a, b};
}
ll ans = 1LL << 60;
rep(i, n - 1) {
for (int j = i + 1; j < n - 1; ++j) {
set<int> remain;
rep(k, n) remain.insert(k);
auto dfs = [&](auto &&f, int v, int p) -> ll {
remain.erase(v);
ll sum = w[v];
for (auto &c : graph[v]) {
if (c == p) continue;
if (edges[i] == pair{v, c} || edges[i] == pair{c, v}) continue;
if (edges[j] == pair{v, c} || edges[j] == pair{c, v}) continue;
sum += f(f, c, v);
}
return sum;
};
vector<ll> result;
while (!remain.empty()) {
result.push_back(dfs(dfs, *remain.begin(), -1));
}
assert(result.size() == 3);
sort(all(result));
chmin(ans, result.back() - result.front());
}
}
cout << ans << endl;
}