結果
問題 |
No.2677 Minmax Independent Set
|
ユーザー |
![]() |
提出日時 | 2024-03-16 12:15:50 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 349 ms / 2,000 ms |
コード長 | 3,162 bytes |
コンパイル時間 | 4,385 ms |
コンパイル使用メモリ | 259,640 KB |
最終ジャッジ日時 | 2025-02-20 06:47:06 |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 61 |
ソースコード
#include <bits/stdc++.h> using namespace std; #include <atcoder/all> using namespace atcoder; #define rep(i, n) for(int i=0;i<(n);++i) #define rep1(i, n) for(int i=1;i<=(n);i++) #define ll long long using mint = modint; using P = pair<ll,ll>; using lb = long double; #ifdef LOCAL # include <debug_print.hpp> # define dbg(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else # define dbg(...) (static_cast<void>(0)) #endif int n; vector<vector<int>> dp(2e5+5, vector<int>(2)); vector<int> ans(2e5+5); vector<vector<int>> g(2e5+5); int m; struct Edge { int to, cost; Edge(int to, int cost) : to(to), cost(cost) {} }; template <class S, S (*op)(S, S), S (*e)(S)> struct ReRooting { public: ReRooting() : ReRooting(0) {} ReRooting(int n) : ReRooting(std::vector<S>(n)) {} ReRooting(const std::vector<S>& v) : _n(int(v.size())) { dp = std::vector<S>(_n); g = std::vector<vector<Edge>>(_n); ans = std::vector<S>(_n); } void addEdge(int a, int b, int w = 1) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); g[a].push_back(Edge(b, w)); g[b].push_back(Edge(a, w)); } void init(){ dfs1(0); rep(i,_n){ dbg(dp[i].black,dp[i].white); } dfs2(0); } std::vector<S> ans; private: int _n, size, log; std::vector<S> dp; std::vector<vector<Edge>> g; S dfs1(int u, int p=-1){ for(auto [v, cost] : g[u]) { if(v==p) continue; dp[u] = op(dp[u], dfs1(v,u)); } dbg(u,dp[u].white, dp[u].black); return dp[u] = addRoot(dp[u]); } void dfs2(int u, int p = -1){ int N = g[u].size(); if(N==0) return; vector<S> l(N); vector<S> r(N); rep(i,N){ ans[u] = op(ans[u], dp[g[u][i].to]); } ans[u] = addRoot(ans[u]); l[0] = dp[g[u][0].to]; r[N-1] = dp[g[u][N-1].to]; rep(i,N){ int v = g[u][i].to; if(i-1>=0) { l[i] = op(l[i-1],dp[v]); } } for(int i=N-1;i>=0;i--){ int v = g[u][i].to; if(i+1<N) { r[i] = op(r[i+1], dp[v]); } } rep(i,N){ int v = g[u][i].to; if(v==p) continue; dp[u] = {0,0}; if(i-1>=0) { dp[u] = op(dp[u], l[i-1]); } if(i+1<N) { dp[u] = op(dp[u], r[i+1]); } dp[u] = addRoot(dp[u]); dfs2(v, u); } } }; struct S{ int white, black; }; S op(S a, S b) { return S{a.white+b.white, max(a.black,a.white)+max(b.black,b.white)}; } S addRoot(S a) { return S{max(a.black,a.white),a.white+1}; } int main() { cin >> n; if(n==1){ cout<<1<<endl; return 0; } ReRooting<S, op, addRoot> rt(n); rep(i,n-1){ int u, v; cin >> u >> v; --u;--v; rt.addEdge(u,v); } rt.init(); int f = 1e9; rep(i,n){ f = min(f, rt.ans[i].black); } cout<<f<<endl; return 0; }