結果

問題 No.2677 Minmax Independent Set
ユーザー ei1333333ei1333333
提出日時 2024-03-15 21:55:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 223 ms / 2,000 ms
コード長 4,205 bytes
コンパイル時間 6,313 ms
コンパイル使用メモリ 272,748 KB
実行使用メモリ 57,156 KB
最終ジャッジ日時 2024-03-15 21:56:05
合計ジャッジ時間 13,211 ms
ジャッジサーバーID
(参考情報)
judge12 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 167 ms
27,716 KB
testcase_06 AC 164 ms
28,100 KB
testcase_07 AC 169 ms
28,484 KB
testcase_08 AC 169 ms
29,252 KB
testcase_09 AC 174 ms
30,532 KB
testcase_10 AC 129 ms
23,800 KB
testcase_11 AC 129 ms
23,804 KB
testcase_12 AC 219 ms
57,156 KB
testcase_13 AC 209 ms
35,416 KB
testcase_14 AC 184 ms
35,672 KB
testcase_15 AC 222 ms
42,040 KB
testcase_16 AC 209 ms
25,796 KB
testcase_17 AC 216 ms
25,796 KB
testcase_18 AC 122 ms
17,664 KB
testcase_19 AC 137 ms
20,292 KB
testcase_20 AC 39 ms
9,344 KB
testcase_21 AC 176 ms
22,084 KB
testcase_22 AC 11 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 108 ms
23,648 KB
testcase_29 AC 186 ms
44,992 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 2 ms
6,676 KB
testcase_40 AC 3 ms
6,676 KB
testcase_41 AC 5 ms
6,676 KB
testcase_42 AC 8 ms
6,676 KB
testcase_43 AC 16 ms
6,676 KB
testcase_44 AC 38 ms
9,088 KB
testcase_45 AC 93 ms
14,720 KB
testcase_46 AC 216 ms
26,104 KB
testcase_47 AC 218 ms
26,436 KB
testcase_48 AC 223 ms
26,436 KB
testcase_49 AC 213 ms
26,436 KB
testcase_50 AC 43 ms
11,380 KB
testcase_51 AC 4 ms
6,676 KB
testcase_52 AC 102 ms
21,876 KB
testcase_53 AC 93 ms
20,868 KB
testcase_54 AC 4 ms
6,676 KB
testcase_55 AC 144 ms
23,828 KB
testcase_56 AC 147 ms
23,744 KB
testcase_57 AC 141 ms
23,828 KB
testcase_58 AC 153 ms
23,844 KB
testcase_59 AC 150 ms
23,840 KB
testcase_60 AC 88 ms
25,452 KB
testcase_61 AC 94 ms
26,052 KB
testcase_62 AC 94 ms
30,916 KB
testcase_63 AC 108 ms
42,436 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>

using namespace std;

using int64 = long long;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 >& p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  explicit FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}
#line 1 "graph/tree/rerooting.hpp"
template< typename sum_t, typename key_t >
struct ReRooting {
  struct Edge {
    int to;
    key_t data;
    sum_t dp, ndp;
  };

  using F = function< sum_t(sum_t, sum_t) >;
  using G = function< sum_t(sum_t, key_t) >;

  vector< vector< Edge > > g;
  const F f;
  const G gg;
  const sum_t ident;
  vector< sum_t > subdp, dp;

  ReRooting(int V, const F f, const G g, const sum_t &ident)
      : g(V), f(f), gg(g), ident(ident), subdp(V, ident), dp(V, ident) {}

  void add_edge(int u, int v, const key_t &d) {
    g[u].emplace_back((Edge) {v, d, ident, ident});
    g[v].emplace_back((Edge) {u, d, ident, ident});
  }

  void add_edge_bi(int u, int v, const key_t &d, const key_t &e) {
    g[u].emplace_back((Edge) {v, d, ident, ident});
    g[v].emplace_back((Edge) {u, e, ident, ident});
  }

  void dfs_sub(int idx, int par) {
    for(auto &e : g[idx]) {
      if(e.to == par) continue;
      dfs_sub(e.to, idx);
      subdp[idx] = f(subdp[idx], gg(subdp[e.to], e.data));
    }
  }

  void dfs_all(int idx, int par, const sum_t &top) {
    sum_t buff{ident};
    for(int i = 0; i < (int) g[idx].size(); i++) {
      auto &e = g[idx][i];
      e.ndp = buff;
      e.dp = gg(par == e.to ? top : subdp[e.to], e.data);
      buff = f(buff, e.dp);
    }
    dp[idx] = buff;
    buff = ident;
    for(int i = (int) g[idx].size() - 1; i >= 0; i--) {
      auto &e = g[idx][i];
      if(e.to != par) dfs_all(e.to, idx, f(e.ndp, buff));
      e.ndp = f(e.ndp, buff);
      buff = f(buff, e.dp);
    }
  }

  vector< sum_t > build() {
    dfs_sub(0, -1);
    dfs_all(0, -1, ident);
    return dp;
  }
};

int main() {
  using pi = pair< int, int >;
  auto f = [](const pi& a, const pi& b) {
    return pi(a.first + b.first, a.second + b.second);
  };
  auto g = [](const pi& a, bool x) {
    return pi(max(a.first, a.second + 1), a.first);
  };
  int N;
  cin >> N;
  ReRooting< pi, bool > dp(N, f, g, pi());
  for(int i = 1; i < N; i++) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    dp.add_edge(u, v, false);
  }
  auto ans = dp.build();
  int ret = inf;
  for(int i = 0; i < N; i++) {
    chmin(ret, ans[i].second + 1);
  }
  cout << ret << endl;
}
0