結果

問題 No.2677 Minmax Independent Set
ユーザー KudeKude
提出日時 2024-03-15 23:06:24
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 427 ms / 2,000 ms
コード長 3,165 bytes
コンパイル時間 3,540 ms
コンパイル使用メモリ 278,500 KB
実行使用メモリ 65,680 KB
最終ジャッジ日時 2024-03-15 23:06:43
合計ジャッジ時間 16,952 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 330 ms
50,704 KB
testcase_06 AC 329 ms
50,960 KB
testcase_07 AC 356 ms
51,216 KB
testcase_08 AC 335 ms
51,600 KB
testcase_09 AC 331 ms
52,368 KB
testcase_10 AC 314 ms
51,936 KB
testcase_11 AC 301 ms
51,936 KB
testcase_12 AC 223 ms
65,680 KB
testcase_13 AC 371 ms
56,104 KB
testcase_14 AC 377 ms
56,876 KB
testcase_15 AC 371 ms
59,648 KB
testcase_16 AC 369 ms
49,772 KB
testcase_17 AC 362 ms
49,772 KB
testcase_18 AC 241 ms
33,028 KB
testcase_19 AC 256 ms
38,324 KB
testcase_20 AC 67 ms
15,616 KB
testcase_21 AC 292 ms
42,144 KB
testcase_22 AC 14 ms
6,656 KB
testcase_23 AC 3 ms
6,548 KB
testcase_24 AC 2 ms
6,548 KB
testcase_25 AC 2 ms
6,548 KB
testcase_26 AC 2 ms
6,548 KB
testcase_27 AC 2 ms
6,548 KB
testcase_28 AC 280 ms
52,400 KB
testcase_29 AC 352 ms
58,860 KB
testcase_30 AC 2 ms
6,548 KB
testcase_31 AC 2 ms
6,548 KB
testcase_32 AC 2 ms
6,548 KB
testcase_33 AC 2 ms
6,548 KB
testcase_34 AC 2 ms
6,548 KB
testcase_35 AC 2 ms
6,548 KB
testcase_36 AC 2 ms
6,548 KB
testcase_37 AC 2 ms
6,548 KB
testcase_38 AC 2 ms
6,548 KB
testcase_39 AC 3 ms
6,548 KB
testcase_40 AC 4 ms
6,548 KB
testcase_41 AC 6 ms
6,548 KB
testcase_42 AC 12 ms
6,548 KB
testcase_43 AC 26 ms
8,960 KB
testcase_44 AC 62 ms
14,592 KB
testcase_45 AC 154 ms
25,856 KB
testcase_46 AC 352 ms
48,460 KB
testcase_47 AC 385 ms
49,100 KB
testcase_48 AC 356 ms
49,100 KB
testcase_49 AC 357 ms
49,228 KB
testcase_50 AC 98 ms
21,844 KB
testcase_51 AC 6 ms
6,548 KB
testcase_52 AC 283 ms
44,064 KB
testcase_53 AC 222 ms
40,840 KB
testcase_54 AC 5 ms
6,548 KB
testcase_55 AC 396 ms
52,564 KB
testcase_56 AC 390 ms
52,556 KB
testcase_57 AC 427 ms
52,548 KB
testcase_58 AC 385 ms
52,560 KB
testcase_59 AC 394 ms
52,424 KB
testcase_60 AC 124 ms
48,924 KB
testcase_61 AC 136 ms
50,740 KB
testcase_62 AC 130 ms
52,624 KB
testcase_63 AC 136 ms
59,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;


template<class S, class E>
struct Rerooting {
    const vector<vector<E>>& g;
    const int n;
    const int root;
    vector<vector<S>> dp, sl, sr;

    Rerooting(const vector<vector<E>>& g, int root=0): g(g), n(g.size()), root(root), dp(n), sl(n), sr(n) {
        dfs1(root);
        dfs2(root);
    }

    S dfs1(int u, int p=-1) {
        const int sz = g[u].size();
        dp[u].resize(sz);
        sl[u].resize(sz + 1);
        sr[u].resize(sz + 1);

        S res;
        for(int i = 0; i < sz; i++) {
            const E& e = g[u][i];
            int v = dest(e);
            if (v == p) continue;
            dp[u][i] = dfs1(v, u).apply(e);
            res = res.merge(dp[u][i]);
        }
        return res;
    }

    void dfs2(int u, int p=-1) {
        const int sz = g[u].size();

        {
            S s;
            for(int i = 0; i < sz; i++) {
                s = s.merge(dp[u][i]);
                sl[u][i + 1] = s;
            }
        }
        {
            S s;
            for(int i = sz - 1; i >= 0; i--) {
                s = dp[u][i].merge(s);
                sr[u][i] = s;
            }
        }

        for(int i = 0; i < sz; i++) {
            int v = dest(g[u][i]);
            if (v == p) continue;
            const int sz_v = g[v].size();
            for(int j = 0; j < sz_v; j++) {
                const E& e = g[v][j];
                int w = dest(e);
                if (w != u) continue;
                dp[v][j] = sl[u][i].merge(sr[u][i + 1]).apply(e);
                break;
            }
            dfs2(v, u);
        }
    }

    S get_acc(int v) { return sr[v][0]; }
    S get_res(int v, E e) { return sr[v][0].apply(e); }

    private:
    int dest(const E& e) {
        if constexpr (is_same<E, int>::value) return e;
        else return e.to;
    };
};

struct S {
    int v0 = 0, v1 = 0;
    S apply(int) const {
      return S{max(v0, v1), v0 + 1};
    }
    S merge(const S& rhs) const {
      return S{v0 + rhs.v0, max(v0, v1) + max(rhs.v0, rhs.v1)};
    }
};

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  VVI to(n);
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    to[u].emplace_back(v);
    to[v].emplace_back(u);
  }
  Rerooting<S, int> rt(to);
  int ans = 1001001001;
  rep(u, n) {
    int cnt = 1;
    for (auto [v0, v1] : rt.dp[u]) {
      cnt += v0;
    }
    chmin(ans, cnt);
  }
  cout << ans << '\n';
}
0