結果
問題 | No.2677 Minmax Independent Set |
ユーザー |
![]() |
提出日時 | 2024-03-15 22:20:59 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 262 ms / 2,000 ms |
コード長 | 2,440 bytes |
コンパイル時間 | 4,379 ms |
コンパイル使用メモリ | 253,760 KB |
最終ジャッジ日時 | 2025-02-20 05:26:21 |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 61 |
ソースコード
#include<bits/stdc++.h>using namespace std;//* ATCODER#include<atcoder/all>using namespace atcoder;typedef modint998244353 mint;//*//* BOOST MULTIPRECISION#include<boost/multiprecision/cpp_int.hpp>using namespace boost::multiprecision;//*/typedef long long ll;#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)template <typename T> bool chmin(T &a, const T &b) {if (a <= b) return false;a = b;return true;}template <typename T> bool chmax(T &a, const T &b) {if (a >= b) return false;a = b;return true;}template <typename T> T max(vector<T> &a){assert(!a.empty());T ret = a[0];for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);return ret;}template <typename T> T min(vector<T> &a){assert(!a.empty());T ret = a[0];for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);return ret;}template <typename T> T sum(vector<T> &a){T ret = 0;for (int i=0; i<(int)a.size(); i++) ret += a[i];return ret;}int main(){ios_base::sync_with_stdio(false);cin.tie(NULL);int n; cin >> n;vector ikeru(n, vector<int>(0));rep(i,0,n-1){int u, v; cin >> u >> v;u--; v--;ikeru[u].push_back(v);ikeru[v].push_back(u);}vector<int> dp0(n);vector<int> dp1(n);auto dfs1 = [&](auto self, int i, int p) -> void {dp0[i] = 0;dp1[i] = 1;for (int j: ikeru[i]){if (j == p) continue;self(self, j, i);dp0[i] += max(dp1[j], dp0[j]);dp1[i] += dp0[j];}return;};dfs1(dfs1, 0, -1);vector<int> ans(n);auto dfs2 = [&](auto self, int i, int p, int up0, int up1) -> void {vector<int> rui_left0 = {0};vector<int> rui_left1 = {0};int now_left0 = 0;int now_left1 = 0;int cnt = 0;for (int j: ikeru[i]){if (j == p) continue;now_left0 += max(dp0[j], dp1[j]);now_left1 += dp0[j];cnt++;rui_left0.push_back(now_left0);rui_left1.push_back(now_left1);}reverse(ikeru[i].begin(), ikeru[i].end());now_left0 = 0;now_left1 = 0;for (int j: ikeru[i]){if (j == p) continue;cnt--;self(self, j, i,max(up0, up1) + rui_left0[cnt] + now_left0,up0 + rui_left1[cnt] + now_left1 + 1);now_left0 += max(dp0[j], dp1[j]);now_left1 += dp0[j];}ans[i] = now_left1 + up0 + 1;};dfs2(dfs2, 0, -1, 0, 0);/*rep(i,0,n){cout << ans[i] << ' ';}cout << '\n';*/cout << min(ans) << '\n';}