結果
問題 | No.2677 Minmax Independent Set |
ユーザー |
![]() |
提出日時 | 2024-03-15 21:55:43 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 232 ms / 2,000 ms |
コード長 | 4,205 bytes |
コンパイル時間 | 4,312 ms |
コンパイル使用メモリ | 260,144 KB |
最終ジャッジ日時 | 2025-02-20 05:03:31 |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 61 |
ソースコード
#include<bits/stdc++.h>#include<atcoder/all>using namespace std;using int64 = long long;const int64 infll = (1LL << 62) - 1;const int inf = (1 << 30) - 1;struct IoSetup {IoSetup() {cin.tie(nullptr);ios::sync_with_stdio(false);cout << fixed << setprecision(10);cerr << fixed << setprecision(10);}} iosetup;template< typename T1, typename T2 >ostream &operator<<(ostream &os, const pair< T1, T2 >& p) {os << p.first << " " << p.second;return os;}template< typename T1, typename T2 >istream &operator>>(istream &is, pair< T1, T2 > &p) {is >> p.first >> p.second;return is;}template< typename T >ostream &operator<<(ostream &os, const vector< T > &v) {for(int i = 0; i < (int) v.size(); i++) {os << v[i] << (i + 1 != v.size() ? " " : "");}return os;}template< typename T >istream &operator>>(istream &is, vector< T > &v) {for(T &in : v) is >> in;return is;}template< typename T1, typename T2 >inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }template< typename T1, typename T2 >inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }template< typename T = int64 >vector< T > make_v(size_t a) {return vector< T >(a);}template< typename T, typename... Ts >auto make_v(size_t a, Ts... ts) {return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));}template< typename T, typename V >typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {t = v;}template< typename T, typename V >typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {for(auto &e : t) fill_v(e, v);}template< typename F >struct FixPoint : F {explicit FixPoint(F &&f) : F(forward< F >(f)) {}template< typename... Args >decltype(auto) operator()(Args &&... args) const {return F::operator()(*this, forward< Args >(args)...);}};template< typename F >inline decltype(auto) MFP(F &&f) {return FixPoint< F >{forward< F >(f)};}#line 1 "graph/tree/rerooting.hpp"template< typename sum_t, typename key_t >struct ReRooting {struct Edge {int to;key_t data;sum_t dp, ndp;};using F = function< sum_t(sum_t, sum_t) >;using G = function< sum_t(sum_t, key_t) >;vector< vector< Edge > > g;const F f;const G gg;const sum_t ident;vector< sum_t > subdp, dp;ReRooting(int V, const F f, const G g, const sum_t &ident): g(V), f(f), gg(g), ident(ident), subdp(V, ident), dp(V, ident) {}void add_edge(int u, int v, const key_t &d) {g[u].emplace_back((Edge) {v, d, ident, ident});g[v].emplace_back((Edge) {u, d, ident, ident});}void add_edge_bi(int u, int v, const key_t &d, const key_t &e) {g[u].emplace_back((Edge) {v, d, ident, ident});g[v].emplace_back((Edge) {u, e, ident, ident});}void dfs_sub(int idx, int par) {for(auto &e : g[idx]) {if(e.to == par) continue;dfs_sub(e.to, idx);subdp[idx] = f(subdp[idx], gg(subdp[e.to], e.data));}}void dfs_all(int idx, int par, const sum_t &top) {sum_t buff{ident};for(int i = 0; i < (int) g[idx].size(); i++) {auto &e = g[idx][i];e.ndp = buff;e.dp = gg(par == e.to ? top : subdp[e.to], e.data);buff = f(buff, e.dp);}dp[idx] = buff;buff = ident;for(int i = (int) g[idx].size() - 1; i >= 0; i--) {auto &e = g[idx][i];if(e.to != par) dfs_all(e.to, idx, f(e.ndp, buff));e.ndp = f(e.ndp, buff);buff = f(buff, e.dp);}}vector< sum_t > build() {dfs_sub(0, -1);dfs_all(0, -1, ident);return dp;}};int main() {using pi = pair< int, int >;auto f = [](const pi& a, const pi& b) {return pi(a.first + b.first, a.second + b.second);};auto g = [](const pi& a, bool x) {return pi(max(a.first, a.second + 1), a.first);};int N;cin >> N;ReRooting< pi, bool > dp(N, f, g, pi());for(int i = 1; i < N; i++) {int u, v;cin >> u >> v;--u, --v;dp.add_edge(u, v, false);}auto ans = dp.build();int ret = inf;for(int i = 0; i < N; i++) {chmin(ret, ans[i].second + 1);}cout << ret << endl;}