library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub firiexp/library

:heavy_check_mark: test/yosupo_maximum_independent_set.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/maximum_independent_set"
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using uint = unsigned;
using ull = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

#include "../graph/independentset.cpp"

int main() {
    int n, m;
    cin >> n >> m;
    IndependentSet G(n);
    for (int i = 0; i < m; ++i) {
        int l, r;
        cin >> l >> r;
        G.add_edge(l, r);
    }
    auto res = G.maximum_independent_set();
    cout << res.first << "\n";
    int cur = 0;
    for (int i = 0; i < n; ++i) {
        if(res.second & (1ull << i)){
            if(cur++) printf(" ");
            printf("%d", i);
        }
    }
    puts("");
    return 0;
}
#line 1 "test/yosupo_maximum_independent_set.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/maximum_independent_set"
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using uint = unsigned;
using ull = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

#line 1 "graph/independentset.cpp"
class IndependentSet {
    int n;
    vector<ull> G;
    pair<int, ull> dfs(ull R, ull P, ull X){
        if(!P && !X){
            return {__builtin_popcountll(R), R};
        }
        if(!P) return {-1, 0};
        pair<int, ull> res = {-1, 0};
        int pivot = __builtin_ctzll(P|X);
        ull z = P & ~G[pivot];
        for (int i = 0; i < n; ++i) {
            if(z & (1ull << i)){
                res = max(res, dfs(R|(1ull << i), P&G[i], X&G[i]));
                P ^= 1ull << i;
                X |= 1ull << i;
            }
        }
        return res;
    }


public:
    explicit IndependentSet(int n): n(n), G(n) {
        for (int i = 0; i < n; ++i) {
            G[i] = ((1ull << n)-1)^(1ull << i);
        }
    }
    void add_edge(int u, int v){
        G[u] &= ~(1ull << v);
        G[v] &= ~(1ull << u);
    }
    pair<int, ull> maximum_independent_set() {
        return dfs(0, (1ull << n)-1, 0);
    }
};
#line 21 "test/yosupo_maximum_independent_set.test.cpp"

int main() {
    int n, m;
    cin >> n >> m;
    IndependentSet G(n);
    for (int i = 0; i < m; ++i) {
        int l, r;
        cin >> l >> r;
        G.add_edge(l, r);
    }
    auto res = G.maximum_independent_set();
    cout << res.first << "\n";
    int cur = 0;
    for (int i = 0; i < n; ++i) {
        if(res.second & (1ull << i)){
            if(cur++) printf(" ");
            printf("%d", i);
        }
    }
    puts("");
    return 0;
}
Back to top page