結果
問題 | No.2677 Minmax Independent Set |
ユーザー |
👑 |
提出日時 | 2024-03-15 21:28:04 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 148 ms / 2,000 ms |
コード長 | 4,534 bytes |
コンパイル時間 | 1,143 ms |
コンパイル使用メモリ | 119,396 KB |
実行使用メモリ | 40,192 KB |
最終ジャッジ日時 | 2024-09-30 00:23:43 |
合計ジャッジ時間 | 6,932 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 61 |
ソースコード
#include <cassert>#include <cmath>#include <cstdint>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <bitset>#include <complex>#include <deque>#include <functional>#include <iostream>#include <limits>#include <map>#include <numeric>#include <queue>#include <random>#include <set>#include <sstream>#include <string>#include <unordered_map>#include <unordered_set>#include <utility>#include <vector>using namespace std;using Int = long long;template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i>= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }#define COLOR(s) ("\x1b[" s "m")// (without edge property)// sub[u]: inside subtree at u, rooted at u// bus[u]: outside subtree at u, rooted at par[u]// tot[u]: rooted at u// T: monoid representing information of a subtree.// T::init() should assign the identity.// T::pull(const T &l, const T &r) should assign the product.// T::up(int u) should attach vertex u to the product of the subtrees.template <class T> struct ForestDP {int n;vector<vector<int>> graph;vector<int> par;vector<T> sub, bus, tot;ForestDP() : n(0) {}explicit ForestDP(int n_) : n(n_), graph(n_) {}void ae(int u, int v) {assert(0 <= u); assert(u < n);assert(0 <= v); assert(v < n);graph[u].push_back(v);graph[v].push_back(u);}void run() {par.assign(n, -2);sub.resize(n);bus.resize(n);tot.resize(n);for (int u = 0; u < n; ++u) if (par[u] == -2) {dfs0(u, -1);dfs1(u, -1);}}void dfs0(int u, int p) {par[u] = p;const int deg = graph[u].size();int w = -1;for (int j = deg; --j >= 0; ) {const int v = graph[u][j];if (p != v) {dfs0(v, u);if (~w) {bus[v].pull(sub[v], bus[w]);} else {bus[v] = sub[v];}w = v;}}if (~w) {sub[u] = bus[w];} else {sub[u].init();}sub[u].up(u);}void dfs1(int u, int p) {const int deg = graph[u].size();int v = -1;for (int j = 0; j < deg; ++j) {const int w = graph[u][j];if (p != w) {if (~v) {bus[v].pull(tot[v], bus[w]);bus[v].up(u);tot[w].pull(tot[v], sub[v]);dfs1(v, u);} else {if (~p) {tot[w] = bus[u];} else {tot[w].init();}}v = w;}}if (~v) {bus[v] = tot[v];bus[v].up(u);tot[u].pull(tot[v], sub[v]);dfs1(v, u);} else {if (~p) {tot[u] = bus[u];} else {tot[u].init();}}tot[u].up(u);}};struct Data {int mx[2];Data() : mx{0, 0} {}friend ostream &operator<<(ostream &os, const Data &t) {return os << "[" << t.mx[0] << ", " << t.mx[1] << "]";}void init() {mx[0] = 0;mx[1] = 0;}void pull(const Data &a, const Data &b) {assert(this != &a);assert(this != &b);for (int s = 0; s < 2; ++s) {mx[s] = a.mx[s] + b.mx[s];}}void up(int u) {swap(mx[0], mx[1]);mx[1] += 1;chmax(mx[1], mx[0]);}};int N;vector<int> A, B;int main() {for (; ~scanf("%d", &N); ) {A.resize(N - 1);B.resize(N - 1);for (int i = 0; i < N - 1; ++i) {scanf("%d%d", &A[i], &B[i]);--A[i];--B[i];}ForestDP<Data> f(N);for (int i = 0; i < N - 1; ++i) {f.ae(A[i], B[i]);}f.run();// cerr<<"sub = "<<f.sub<<endl;// cerr<<"bus = "<<f.bus<<endl;int ans = N + 1;for (int u = 0; u < N; ++u) {int sum = 1;if (~f.par[u]) {sum += f.bus[u].mx[0];}for (const int v : f.graph[u]) if (f.par[u] != v) {sum += f.sub[v].mx[0];}// cerr<<u<<": "<<sum<<endl;chmin(ans, sum);}printf("%d\n", ans);}return 0;}