結果

問題 No.2677 Minmax Independent Set
ユーザー 👑 hos.lyrichos.lyric
提出日時 2024-03-15 21:28:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 186 ms / 2,000 ms
コード長 4,534 bytes
コンパイル時間 1,200 ms
コンパイル使用メモリ 119,416 KB
実行使用メモリ 40,172 KB
最終ジャッジ日時 2024-03-15 21:28:13
合計ジャッジ時間 8,563 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 142 ms
23,276 KB
testcase_06 AC 140 ms
23,532 KB
testcase_07 AC 138 ms
23,788 KB
testcase_08 AC 146 ms
24,172 KB
testcase_09 AC 141 ms
25,068 KB
testcase_10 AC 112 ms
22,140 KB
testcase_11 AC 113 ms
22,144 KB
testcase_12 AC 172 ms
40,172 KB
testcase_13 AC 170 ms
28,156 KB
testcase_14 AC 145 ms
28,252 KB
testcase_15 AC 186 ms
32,012 KB
testcase_16 AC 176 ms
21,612 KB
testcase_17 AC 177 ms
21,612 KB
testcase_18 AC 91 ms
15,072 KB
testcase_19 AC 116 ms
17,260 KB
testcase_20 AC 29 ms
8,320 KB
testcase_21 AC 136 ms
18,668 KB
testcase_22 AC 9 ms
6,676 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 2 ms
6,676 KB
testcase_28 AC 94 ms
22,204 KB
testcase_29 AC 165 ms
33,256 KB
testcase_30 AC 2 ms
6,676 KB
testcase_31 AC 2 ms
6,676 KB
testcase_32 AC 2 ms
6,676 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 2 ms
6,676 KB
testcase_35 AC 2 ms
6,676 KB
testcase_36 AC 2 ms
6,676 KB
testcase_37 AC 2 ms
6,676 KB
testcase_38 AC 2 ms
6,676 KB
testcase_39 AC 3 ms
6,676 KB
testcase_40 AC 3 ms
6,676 KB
testcase_41 AC 4 ms
6,676 KB
testcase_42 AC 7 ms
6,676 KB
testcase_43 AC 14 ms
6,676 KB
testcase_44 AC 28 ms
7,936 KB
testcase_45 AC 60 ms
12,416 KB
testcase_46 AC 168 ms
21,360 KB
testcase_47 AC 168 ms
21,612 KB
testcase_48 AC 169 ms
21,612 KB
testcase_49 AC 171 ms
21,612 KB
testcase_50 AC 32 ms
10,644 KB
testcase_51 AC 4 ms
6,676 KB
testcase_52 AC 77 ms
19,084 KB
testcase_53 AC 76 ms
17,788 KB
testcase_54 AC 3 ms
6,676 KB
testcase_55 AC 117 ms
22,320 KB
testcase_56 AC 111 ms
22,252 KB
testcase_57 AC 118 ms
22,228 KB
testcase_58 AC 120 ms
22,324 KB
testcase_59 AC 114 ms
22,196 KB
testcase_60 AC 74 ms
21,612 KB
testcase_61 AC 79 ms
21,740 KB
testcase_62 AC 78 ms
25,196 KB
testcase_63 AC 83 ms
31,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0