結果
問題 | No.2677 Minmax Independent Set |
ユーザー | 👑 hos.lyric |
提出日時 | 2024-03-15 21:28:04 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.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 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 107 ms
23,228 KB |
testcase_06 | AC | 112 ms
23,424 KB |
testcase_07 | AC | 112 ms
23,612 KB |
testcase_08 | AC | 105 ms
24,192 KB |
testcase_09 | AC | 111 ms
24,832 KB |
testcase_10 | AC | 100 ms
22,144 KB |
testcase_11 | AC | 92 ms
22,016 KB |
testcase_12 | AC | 132 ms
40,192 KB |
testcase_13 | AC | 118 ms
28,032 KB |
testcase_14 | AC | 116 ms
28,124 KB |
testcase_15 | AC | 130 ms
32,012 KB |
testcase_16 | AC | 132 ms
21,504 KB |
testcase_17 | AC | 126 ms
21,504 KB |
testcase_18 | AC | 66 ms
14,976 KB |
testcase_19 | AC | 90 ms
17,024 KB |
testcase_20 | AC | 27 ms
8,192 KB |
testcase_21 | AC | 91 ms
18,552 KB |
testcase_22 | AC | 9 ms
5,248 KB |
testcase_23 | AC | 2 ms
5,248 KB |
testcase_24 | AC | 2 ms
5,248 KB |
testcase_25 | AC | 1 ms
5,248 KB |
testcase_26 | AC | 2 ms
5,248 KB |
testcase_27 | AC | 2 ms
5,248 KB |
testcase_28 | AC | 83 ms
22,204 KB |
testcase_29 | AC | 140 ms
33,120 KB |
testcase_30 | AC | 2 ms
5,248 KB |
testcase_31 | AC | 2 ms
5,248 KB |
testcase_32 | AC | 2 ms
5,248 KB |
testcase_33 | AC | 2 ms
5,248 KB |
testcase_34 | AC | 1 ms
5,248 KB |
testcase_35 | AC | 2 ms
5,248 KB |
testcase_36 | AC | 2 ms
5,248 KB |
testcase_37 | AC | 2 ms
5,248 KB |
testcase_38 | AC | 2 ms
5,248 KB |
testcase_39 | AC | 3 ms
5,248 KB |
testcase_40 | AC | 3 ms
5,248 KB |
testcase_41 | AC | 4 ms
5,248 KB |
testcase_42 | AC | 8 ms
5,248 KB |
testcase_43 | AC | 14 ms
5,632 KB |
testcase_44 | AC | 26 ms
7,808 KB |
testcase_45 | AC | 50 ms
12,288 KB |
testcase_46 | AC | 118 ms
21,228 KB |
testcase_47 | AC | 132 ms
21,500 KB |
testcase_48 | AC | 148 ms
21,408 KB |
testcase_49 | AC | 143 ms
21,496 KB |
testcase_50 | AC | 31 ms
10,520 KB |
testcase_51 | AC | 4 ms
5,248 KB |
testcase_52 | AC | 66 ms
18,848 KB |
testcase_53 | AC | 65 ms
17,664 KB |
testcase_54 | AC | 3 ms
5,248 KB |
testcase_55 | AC | 92 ms
22,192 KB |
testcase_56 | AC | 87 ms
22,124 KB |
testcase_57 | AC | 92 ms
22,228 KB |
testcase_58 | AC | 95 ms
22,204 KB |
testcase_59 | AC | 88 ms
22,200 KB |
testcase_60 | AC | 69 ms
21,504 KB |
testcase_61 | AC | 74 ms
21,600 KB |
testcase_62 | AC | 75 ms
25,088 KB |
testcase_63 | AC | 80 ms
31,196 KB |
ソースコード
#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; }