結果

問題 No.2677 Minmax Independent Set
ユーザー t98slidert98slider
提出日時 2024-03-15 22:52:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,819 bytes
コンパイル時間 2,227 ms
コンパイル使用メモリ 206,780 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-15 22:52:59
合計ジャッジ時間 6,451 ms
ジャッジサーバーID
(参考情報)
judge13 / 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 WA -
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 53 ms
6,676 KB
testcase_06 AC 50 ms
6,676 KB
testcase_07 AC 50 ms
6,676 KB
testcase_08 AC 50 ms
6,676 KB
testcase_09 AC 49 ms
6,676 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 53 ms
6,676 KB
testcase_13 AC 55 ms
6,676 KB
testcase_14 AC 52 ms
6,676 KB
testcase_15 AC 53 ms
6,676 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 42 ms
6,676 KB
testcase_29 AC 52 ms
6,676 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 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 AC 17 ms
6,676 KB
testcase_51 AC 3 ms
6,676 KB
testcase_52 AC 39 ms
6,676 KB
testcase_53 AC 36 ms
6,676 KB
testcase_54 AC 3 ms
6,676 KB
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 AC 43 ms
6,676 KB
testcase_61 WA -
testcase_62 AC 42 ms
6,676 KB
testcase_63 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<class T> istream& operator >> (istream& is, vector<T>& vec) {
    for(T& x : vec) is >> x;
    return is;
}

template<class T> ostream& operator << (ostream& os, const vector<T>& vec) {
    if(vec.empty()) return os;
    os << vec[0];
    for(auto it = vec.begin(); ++it != vec.end(); ) os << ' ' << *it;
    return os;
}

template <class T> struct xor_dsu {
    public:
    xor_dsu() : _n(0) {}
    xor_dsu(int n) : _n(n), num_component(n), parent_or_size(n, -1), diff_weight(n) {}

    bool merge(int a, int b, T w) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        w ^= diff_weight[a] ^ diff_weight[b];
        if(x == y) return w == 0;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        diff_weight[y] = w;
        num_component--;
        return true;
    }
    T diff(int a, int b) {
        if(!same(a, b)) return -1;
        return diff_weight[a] ^ diff_weight[b];
    }
    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }
    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
        int r = leader(parent_or_size[a]);
        diff_weight[a] ^= diff_weight[parent_or_size[a]];
        return parent_or_size[a] = r;
    }
    int size() {
        return num_component;
    }
    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }
    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            std::remove_if(result.begin(), result.end(),
                           [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }
    private:
        int _n, num_component;
        std::vector<int> parent_or_size;
        std::vector<T> diff_weight;
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    xor_dsu<int> uf(n);
    for(int i = 1; i < n; i++){
        int u, v;
        cin >> u >> v;
        u--, v--;
        uf.merge(u, v, 1);
    }
    int cnt = 0;
    for(int i = 0; i < n; i++) cnt += uf.diff(0, i);
    cout << min(cnt, n - cnt) << '\n';
}
0