結果
問題 | No.2677 Minmax Independent Set |
ユーザー |
![]() |
提出日時 | 2024-03-15 23:06:24 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 334 ms / 2,000 ms |
コード長 | 3,165 bytes |
コンパイル時間 | 3,149 ms |
コンパイル使用メモリ | 279,224 KB |
実行使用メモリ | 65,676 KB |
最終ジャッジ日時 | 2024-09-30 02:41:05 |
合計ジャッジ時間 | 13,718 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 61 |
ソースコード
#include<bits/stdc++.h> namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include<atcoder/all> #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair<int,int>; using VI = vector<int>; using VVI = vector<VI>; using VL = vector<ll>; using VVL = vector<VL>; template<class S, class E> struct Rerooting { const vector<vector<E>>& g; const int n; const int root; vector<vector<S>> dp, sl, sr; Rerooting(const vector<vector<E>>& g, int root=0): g(g), n(g.size()), root(root), dp(n), sl(n), sr(n) { dfs1(root); dfs2(root); } S dfs1(int u, int p=-1) { const int sz = g[u].size(); dp[u].resize(sz); sl[u].resize(sz + 1); sr[u].resize(sz + 1); S res; for(int i = 0; i < sz; i++) { const E& e = g[u][i]; int v = dest(e); if (v == p) continue; dp[u][i] = dfs1(v, u).apply(e); res = res.merge(dp[u][i]); } return res; } void dfs2(int u, int p=-1) { const int sz = g[u].size(); { S s; for(int i = 0; i < sz; i++) { s = s.merge(dp[u][i]); sl[u][i + 1] = s; } } { S s; for(int i = sz - 1; i >= 0; i--) { s = dp[u][i].merge(s); sr[u][i] = s; } } for(int i = 0; i < sz; i++) { int v = dest(g[u][i]); if (v == p) continue; const int sz_v = g[v].size(); for(int j = 0; j < sz_v; j++) { const E& e = g[v][j]; int w = dest(e); if (w != u) continue; dp[v][j] = sl[u][i].merge(sr[u][i + 1]).apply(e); break; } dfs2(v, u); } } S get_acc(int v) { return sr[v][0]; } S get_res(int v, E e) { return sr[v][0].apply(e); } private: int dest(const E& e) { if constexpr (is_same<E, int>::value) return e; else return e.to; }; }; struct S { int v0 = 0, v1 = 0; S apply(int) const { return S{max(v0, v1), v0 + 1}; } S merge(const S& rhs) const { return S{v0 + rhs.v0, max(v0, v1) + max(rhs.v0, rhs.v1)}; } }; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; VVI to(n); rep(i, n - 1) { int u, v; cin >> u >> v; u--, v--; to[u].emplace_back(v); to[v].emplace_back(u); } Rerooting<S, int> rt(to); int ans = 1001001001; rep(u, n) { int cnt = 1; for (auto [v0, v1] : rt.dp[u]) { cnt += v0; } chmin(ans, cnt); } cout << ans << '\n'; }