結果
| 問題 | No.3514 Majority Driven Tree |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-04-25 19:34:48 |
| 言語 | C++23 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,817 bytes |
| 記録 | |
| コンパイル時間 | 6,133 ms |
| コンパイル使用メモリ | 379,400 KB |
| 実行使用メモリ | 7,976 KB |
| 最終ジャッジ日時 | 2026-04-25 19:35:01 |
| 合計ジャッジ時間 | 12,238 ms |
|
ジャッジサーバーID (参考情報) |
judge2_0 / judge1_1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 14 WA * 23 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using mint = modint998244353;
// using mint = modint1000000007;
template <typename T> using vec = vector<T>;
template <typename T> using pa = pair<T, T>;
template <typename T> using mipq = priority_queue<T, vec<T>, greater<T>>;
#define REP(_1, _2, _3, _4, name, ...) name
#define REP1(i, n) for (auto i = decay_t<decltype(n)>{}; (i) < (n); ++(i))
#define REP2(i, l, r) for (auto i = (l); (i) < (r); ++(i))
#define REP3(i, l, r, d) for (auto i = (l); (i) < (r); i += (d))
#define rep(...) REP(__VA_ARGS__, REP3, REP2, REP1)(__VA_ARGS__)
#define rrep(i, r, l) for (int i = (r); i >= (l); --i)
template <typename T> bool chmax(T &a, const T &b) { return (a < b ? a = b, true : false); }
template <typename T> bool chmin(T &a, const T &b) { return (a > b ? a = b, true : false); }
constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr int mov[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
void solve();
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
int T = 1;
// cin >> T;
while (T--) solve();
}
// 木dp的ななにか...?
// 根をコスト付きで塗るか場合分けする
// dpで計算するのは、親が塗られてたとき/塗られてないときの最小コスト
// 根を塗るなら、ぜんぶで親が塗られてたときのコストになる
// 根を塗らないなら、まず過半数を塗る必要があって(ここで親が塗られてた/塗られてないの差が発生)、
// 残りは塗られてたときのコストでできる
// うーん、ほんとか?根の選択に依存しないのか?
// よさそうな雰囲気はある、順番があまり関係しないから...?
void solve() {
int N;
cin >> N;
vec<vec<int>> G(N);
rep(i, N - 1) {
int u, v;
cin >> u >> v;
u--; v--;
G[u].push_back(v);
G[v].push_back(u);
}
auto dfs = [&] (this auto self, int u, int p) -> pa<int> {
// 過半数を選ぶ部分では、根が塗られたときの差分が小さいやつをたくさん選べばいい
int s = 0;
vec<int> diff;
for (auto v : G[u]) {
if (v == p) continue;
auto [c1, c2] = self(v, u);
s += c1;
diff.push_back(c1 - c2);
}
int n = diff.size();
ranges::sort(diff);
vec<int> cum(n + 1);
rep(i, n) cum[i + 1] = cum[i] + diff[i];
int res1 = s + 1 - cum[n], res2 = res1;
if (n > 0) chmin(res1, s - (cum[n] - cum[n / 2 + 1]));
chmin(res2, s - (cum[n] - cum[(n + 1) / 2]));
return {res1, res2};
};
cout << dfs(0, -1).first << '\n';
}