結果

問題 No.3348 Tree Balance
コンテスト
ユーザー Nzt3
提出日時 2025-11-07 17:34:41
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,815 bytes
コンパイル時間 3,377 ms
コンパイル使用メモリ 297,308 KB
実行使用メモリ 54,496 KB
最終ジャッジ日時 2025-11-13 21:12:39
合計ジャッジ時間 12,035 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 2 TLE * 1 -- * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

// 重心と隣接する頂点を根とする
// どれか1つは通りそう
// TLE
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
#define rep3(i, a, b, c) for (ll i = (a); i < (b); i += (c))
#define rep2(i, a, b) rep3(i, a, b, 1)
#define rep1(i, n) rep2(i, 0, n)
#define rep0(n) rep1(aaaaa, n)
#define ov4(a, b, c, d, name, ...) name
#define rep(...) ov4(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)
#define per(i, a, b) for (ll i = (a) - 1; i >= (b); i--)
#define fore(e, v) for (auto &&e : v)
#define all(a) begin(a), end(a)
#define sz(a) (int)(size(a))
#define lb(v, x) (lower_bound(all(v), x) - begin(v))
#define eb emplace_back

template <typename T, typename S> bool chmin(T &a, const S &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T, typename S> bool chmax(T &a, const S &b) {
  return a < b ? a = b, 1 : 0;
}
const int INF = 1e9 + 100;
const ll INFL = 3e18 + 100;
#define i128 __int128_t
struct _ {
  _() { cin.tie(0)->sync_with_stdio(0), cout.tie(0); }
} __;

int main() {
  int N;
  cin >> N;
  vl W(N);
  fore(i, W) cin >> i;
  vector<vi> G(N);
  rep(N - 1) {
    int U, V;
    cin >> U >> V;
    --U, --V;
    G[U].push_back(V);
    G[V].push_back(U);
  }
  vl subtree = W;
  vi par(N);
  auto dfs_subtree = [&](auto f, int v, int p) -> ll {
    par[v] = p;
    for (int u : G[v]) {
      if (u != p)
        subtree[v] += f(f, u, v);
    }
    return subtree[v];
  };
  dfs_subtree(dfs_subtree, 0, -1);
  int gr = 0;
  while (1) {
    bool br = 1;
    for (int u : G[gr]) {
      if (u == par[gr])
        continue;
      if (subtree[u] * 2 >= subtree[0]) {
        gr = u;
        br = 0;
        break;
      }
    }
    if (br)
      break;
  }
  // 重心を根として部分木サイズを再計算
  subtree = W;
  dfs_subtree(dfs_subtree, gr, -1);

  ll ans = subtree[gr];
  set<ll> cut1;
  for (int s : G[gr]) {
    queue<int> bfs;
    bfs.push(s);
    vl d;
    while (bfs.size()) {
      int v = bfs.front();
      bfs.pop();
      d.push_back(subtree[v]);
      for (int u : G[v]) {
        if (u != par[v]) {
          bfs.push(u);
        }
      }
    }
    for (auto w : d) {
      ll mid = (subtree[gr] - w + 1) / 2;
      auto it = cut1.lower_bound(mid);
      if (it != end(cut1)) {
        chmin(ans, max({*it, w, subtree[gr] - w - (*it)}) -
                       min({*it, w, subtree[gr] - w - (*it)}));
      }
      if (it != begin(cut1))
        it--;
      if (it != end(cut1)) {
        chmin(ans, max({*it, w, subtree[gr] - w - (*it)}) -
                       min({*it, w, subtree[gr] - w - (*it)}));
      }
    }
    for (auto w : d) {
      cut1.insert(w);
    }
  }

  // 重心に隣接する頂点についても調べる
  for (int gr2 : G[gr]) {
    subtree = W;
    dfs_subtree(dfs_subtree, gr2, -1);

    set<ll> cut1;
    for (int s : G[gr2]) {
      queue<int> bfs;
      bfs.push(s);
      vl d;
      while (bfs.size()) {
        int v = bfs.front();
        bfs.pop();
        d.push_back(subtree[v]);
        for (int u : G[v]) {
          if (u != par[v]) {
            bfs.push(u);
          }
        }
      }
      for (auto w : d) {
        ll mid = (subtree[gr2] - w + 1) / 2;
        auto it = cut1.lower_bound(mid);
        if (it != end(cut1)) {
          chmin(ans, max({*it, w, subtree[gr2] - w - (*it)}) -
                         min({*it, w, subtree[gr2] - w - (*it)}));
        }
        if (it != begin(cut1))
          it--;
        if (it != end(cut1)) {
          chmin(ans, max({*it, w, subtree[gr2] - w - (*it)}) -
                         min({*it, w, subtree[gr2] - w - (*it)}));
        }
      }
      for (auto w : d) {
        cut1.insert(w);
      }
    }
  }

  cout << ans << '\n';
}
0