Submission #4074152


Source Code Expand

#pragma GCC optimize ("O3")
#include "bits/stdc++.h"

using namespace std;
using ll = long long int;

#define debugos cout
#define debug(v) {printf("L%d %s > ",__LINE__,#v);debugos<<(v)<<endl;}
#define debugv(v) {printf("L%d %s > ",__LINE__,#v);for(auto e:(v)){debugos<<e<<" ";}debugos<<endl;}
#define debuga(m,w) {printf("L%d %s > ",__LINE__,#m);for(int x=0;x<(w);x++){debugos<<(m)[x]<<" ";}debugos<<endl;}
#define debugaa(m,h,w) {printf("L%d %s >\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){debugos<<(m)[y][x]<<" ";}debugos<<endl;}}
#define ALL(v) (v).begin(),(v).end()
#define repeat(cnt,l) for(auto cnt=decltype(l)();(cnt)<(l);++(cnt))
#define rrepeat(cnt,l) for(auto cnt=(l)-1;0<=(cnt);--(cnt))
#define iterate(cnt,b,e) for(auto cnt=(b);(cnt)!=(e);++(cnt))
#define diterate(cnt,b,e) for(auto cnt=(b);(cnt)!=(e);--(cnt))
const ll MD = 1000000007ll; const long double PI = 3.1415926535897932384626433832795L;
inline void assert_call(bool assertion, function<void()> f) { if (!assertion) { cerr << "assertion fault:" << endl; f(); abort(); } }
template<typename T1, typename T2> inline ostream& operator <<(ostream &o, const pair<T1, T2> p) { o << '(' << p.first << ':' << p.second << ')'; return o; }
template<typename Vec> inline ostream& _ostream_vecprint(ostream& os, const Vec& a) {
    os << '['; for (const auto& e : a) os << ' ' << e << ' '; os << ']'; return os;
}
template<typename T> inline ostream& operator<<(ostream& o, const vector<T>& v) { return _ostream_vecprint(o, v); }
template<typename T, size_t S> inline ostream& operator<<(ostream& o, const array<T, S>& v) { return _ostream_vecprint(o, v); }
template<typename T> inline T& chmax(T& to, const T& val) { return to = max(to, val); }
template<typename T> inline T& chmin(T& to, const T& val) { return to = min(to, val); }
void bye(string s, int code = 0) { cout << s << endl; exit(code); }
mt19937_64 randdev(8901016);
template<typename T, typename Random = decltype(randdev), typename enable_if<is_integral<T>::value>::type* = nullptr>
inline T rand(T l, T h, Random& rand = randdev) { return uniform_int_distribution<T>(l, h)(rand); }
template<typename T, typename Random = decltype(randdev), typename enable_if<is_floating_point<T>::value>::type* = nullptr>
inline T rand(T l, T h, Random& rand = randdev) { return uniform_real_distribution<T>(l, h)(rand); }

#if defined(_WIN32) || defined(_WIN64)
#define getchar_unlocked _getchar_nolock
#define putchar_unlocked _putchar_nolock
#elif defined(__GNUC__)
#else
#define getchar_unlocked getchar
#define putchar_unlocked putchar
#endif
namespace {
#define isvisiblechar(c) (0x21<=(c)&&(c)<=0x7E)
    class MaiScanner {
    public:
        template<typename T> void input_integer(T& var) noexcept {
            var = 0; T sign = 1;
            int cc = getchar_unlocked();
            for (; cc < '0' || '9' < cc; cc = getchar_unlocked())
                if (cc == '-') sign = -1;
            for (; '0' <= cc && cc <= '9'; cc = getchar_unlocked())
                var = (var << 3) + (var << 1) + cc - '0';
            var = var * sign;
        }
        inline int c() noexcept { return getchar_unlocked(); }
        inline MaiScanner& operator>>(int& var) noexcept { input_integer<int>(var); return *this; }
        inline MaiScanner& operator>>(long long& var) noexcept { input_integer<long long>(var); return *this; }
        inline MaiScanner& operator>>(string& var) {
            int cc = getchar_unlocked();
            for (; !isvisiblechar(cc); cc = getchar_unlocked());
            for (; isvisiblechar(cc); cc = getchar_unlocked())
                var.push_back(cc);
            return *this;
        }
        template<typename IT> inline void in(IT begin, IT end) { for (auto it = begin; it != end; ++it) *this >> *it; }
    };
    class MaiPrinter {
    public:
        template<typename T>
        void output_integer(T var) noexcept {
            if (var == 0) { putchar_unlocked('0'); return; }
            if (var < 0)
                putchar_unlocked('-'),
                var = -var;
            char stack[32]; int stack_p = 0;
            while (var)
                stack[stack_p++] = '0' + (var % 10),
                var /= 10;
            while (stack_p)
                putchar_unlocked(stack[--stack_p]);
        }
        inline MaiPrinter& operator<<(char c) noexcept { putchar_unlocked(c); return *this; }
        inline MaiPrinter& operator<<(int var) noexcept { output_integer<int>(var); return *this; }
        inline MaiPrinter& operator<<(long long var) noexcept { output_integer<long long>(var); return *this; }
        inline MaiPrinter& operator<<(char* str_p) noexcept { while (*str_p) putchar_unlocked(*(str_p++)); return *this; }
        inline MaiPrinter& operator<<(const string& str) {
            const char* p = str.c_str();
            const char* l = p + str.size();
            while (p < l) putchar_unlocked(*p++);
            return *this;
        }
        template<typename IT> void join(IT begin, IT end, char sep = ' ') { for (bool b = 0; begin != end; ++begin, b = 1) b ? *this << sep << *begin : *this << *begin; }
    };
}
MaiScanner scanner;
MaiPrinter printer;


//


class Graph {
public:
    size_t n;
    vector<vector<int>> vertex_to;

    Graph(size_t n = 1) :n(n), vertex_to(n) {}

    inline size_t size() const { return n; }
    void resize(size_t _n) { vertex_to.resize(n = _n); }
    void connect(int from, int to) {
        vertex_to[(size_t)from].emplace_back(to);
        vertex_to[(size_t)to].emplace_back(from);
    }
};


inline int bitcount(int x) {
    return bitset<31>(x).count();
}
 
// 半分全列挙
pair<int, vector<int>> vertex_cover(const Graph& graph) {
    const int n = graph.n;
    const int n_A = n / 2;
    const int n_B = n - n_A;
    const int inf = 1e9;
 
    // group A : i <  n_A
    // group B : i >= n_A
 
    // dp[S] : S \subseteq V_A, 
    vector<int> ans_partial_A(1 << n_A, 0);
 
    // Sが独立集合なら0,独立集合でないならinfとなるように初期状態を構成する.
    for (int i = 0; i < n_A; ++i) {
        int bit = 1 << i;
        for (int j : graph.vertex_to[i])
            if (j < n_A)
                ans_partial_A[bit | (1 << j)] = inf; // 頂点数2
    }
    for (int bit = 3; bit < 1 << n_A; ++bit) {
        if (bitcount(bit) <= 1) continue;
        for (int i = 0; i < n_A; ++i) {
            ans_partial_A[bit | (1 << i)] |= ans_partial_A[bit];
        }
    }
 
    // groupA の部分集合の解を全列挙
    // ここでは groupA と groupB のカットエッジの被覆は考えない.
    for (int bit = 1; bit < 1 << n_A; ++bit) {
        for (int i = 0; i < n_A; ++i) {
            if (bit & (1 << i)) continue;
            ans_partial_A[bit | (1 << i)] = min(
                ans_partial_A[bit | (1 << i)],
                ans_partial_A[bit] + 1
            );
        }
    }
 
    // dp[S] Sは独立集合ではない
    vector<int8_t> not_independent_B(1 << n_B, 0);
    for (int _i = 0; _i < n_B; ++_i) {
        int bit = 1 << _i;
        for (int j : graph.vertex_to[n_A + _i])
            if (n_A <= j)
                not_independent_B[bit | (1 << (j - n_A))] = 1;
    }
    for (int bit = 3; bit < 1 << n_B; ++bit) {
        if (bitcount(bit) <= 1) continue;
        for (int _i = 0; _i < n_B; ++_i) {
            not_independent_B[bit | (1 << _i)] |= not_independent_B[bit];
        }
    }
 
    // dp[S] Sに隣接する頂点のbit配列
    vector<int> adjacent2B(1 << n_B, 0);
    for (int _i = 0; _i < n_B; ++_i) {
        int bit = 0;
        for (int j : graph.vertex_to[n_A + _i])
            if (j < n_A)
                bit |= 1 << j;
        adjacent2B[1 << _i] = bit;
    }
    for (int bit = 1; bit < 1 << n_B; ++bit) {
        for (int _i = 0; _i < n_B; ++_i) {
            adjacent2B[bit | (1 << _i)] |= adjacent2B[bit];
        }
    }
 
    // answer
    int best = inf;
    pair<int,int> vtx_selection;
 
    // groupB の部分集合を全列挙
    for (int bit = 0; bit < 1 << n_B; ++bit) {
 
        // choice_Bが頂点被覆でないなら,reject
        if (not_independent_B[((1 << n_B) - 1) ^ bit]) continue;
 
        int mask_A = (1 << n_A) - 1;
        int fix_A = 0;
 
        int adj = adjacent2B[((1 << n_B) - 1) ^ bit];
        for (int i = 0; i < n_A; ++i) {
            // choice_B で選んでいない頂点が,groupAと隣接するならば
            if (adj & (1 << i)) {
                // そのgroupAの頂点は必ず選択する.
                mask_A ^= 1 << i; // 頂点jは考慮しなくてよい
                ++fix_A;          // 必ず選択することにしたので
            }
        }
 
        int score = ans_partial_A[mask_A] + bitcount(bit) + fix_A;
        if (score < best) {
            best = score;
            vtx_selection.first = mask_A;
            vtx_selection.second = bit;
        }
    }
 
    // 選んだ頂点の復元
    vector<int> selection;
    for (int i = 0; i < n_B; ++i) {
        if (vtx_selection.second & (1 << i))
            selection.push_back(n_A + i);
    }
    for (int i = 0; i < n_A; ++i) {
        if ((vtx_selection.first & (1 << i)) == 0)
            selection.push_back(i);
    }
    {
        int m = vtx_selection.first;
        int curr = ans_partial_A[m];
        while (m > 0) {
            int bi;
            for (int i = 0; i < n_A; ++i) {
                if ((m & (1 << i)) == 0) continue;
                int a = ans_partial_A[m ^ (1 << i)];
                if (curr == a) {
                    bi = -1;
                    m ^= (1 << i);
                    break;
                }
                else if (curr - 1 == a) {
                    bi = i;
                }
            }
            if (bi >= 0) {
                m ^= (1 << bi);
                selection.push_back(bi);
                --curr;
            }
            
        }
    }
    sort(selection.begin(), selection.end());
    selection.shrink_to_fit();
 
    return make_pair(best, selection);
}



pair<int, vector<int>> independentSet(const Graph& graph) {

    int bestScore = 0;
    vector<int8_t> bestSelection;
    vector<int8_t> selection(graph.size());

    int initScore = 0, initRem = graph.size();

    for (int i = 0; i < graph.size(); ++i) {
        if (graph.vertex_to[i].empty()) {
            selection[i] = 1;
            ++initScore;
            --initRem;
            continue;
        }
        if (graph.vertex_to[i].size() == 1 &&
            selection[i] == 0) {
            selection[i] = 1;
            if (selection[graph.vertex_to[i][0]] == 0) {
                --initRem;
                selection[graph.vertex_to[i][0]] = -1;
            }
            ++initScore;
            --initRem;
            
            continue;
        }

    }

    vector<int> sortedIdx(graph.size());
    iota(sortedIdx.begin(), sortedIdx.end(), 0);
    sort(sortedIdx.begin(), sortedIdx.end(),
        [&graph](int l, int r) {return graph.vertex_to[l].size() > graph.vertex_to[r].size(); });

    auto dfs = [&](auto dfs, int idxx, int rem, int score) -> void {

        if (idxx == graph.size()) {
            if (bestScore < score) {
                bestScore = score;
                bestSelection = selection;
            }
            return;
        }
        int idx = sortedIdx[idxx];
        if (score + rem < bestScore) return;

        if (selection[idx] != 0) {
            dfs(dfs, idxx + 1, rem, score);
            return;
        }

        // select idx
        int cnte = 0;
        selection[idx] = idx + 2;
        for (int e : graph.vertex_to[idx]) {
            cnte += selection[e] >= 0;
            if (idx < e && selection[e] == 0) {
                selection[e] = -idx - 2;
                --rem;
            }
        }
        dfs(dfs, idxx + 1, rem - 1, score + 1);
        selection[idx] = 0;

        for (int e : graph.vertex_to[idx]) {
            if (idx < e && selection[e] == -idx - 2) {
                selection[e] = 0;
                ++rem;
            }
        }
        if (cnte <= 1) return;
        dfs(dfs, idxx + 1, rem, score);
    };

    bestScore = initScore;
    bestSelection = selection;
    dfs(dfs, 0, initRem, initScore);

    vector<int> idxs;
    for (int i = 0; i < graph.size(); ++i)
        if (bestSelection[i] > 0) idxs.push_back(i);
    idxs.shrink_to_fit();
    return make_pair(bestScore, idxs);
}

int N, M;

Graph g;


int main2() {

    scanner >> N >> M;
    repeat(_, 99999){
        g = Graph(N);
        repeat(i, M) {
            int a, b;
            a = rand(0, N-1);
            b = rand(0, N-1);
            if (a == b) {--i;continue;}
            //--a; --b;
            g.connect(a, b);
        }
    
        for (auto& a : g.vertex_to) {
            sort(ALL(a));
            a.erase(unique(ALL(a)), a.end());
        }
    
        auto ans = independentSet(g);
        auto ans2 = vertex_cover(g);
        //debug(ans.first);
        //debug(ans2.first);
        if (ans.first != N - ans2.first){
            debug(ans);
            debug(ans2);
            repeat(i, N) {
                for (auto j :g.vertex_to[i])
                    if (i < j) cout << i+1 << " " << j+1 << endl;
            }
            
            abort();
        }
        assert(ans.first == ans.second.size());
        
    }

    //printer << ans.first << '\n';


    return 0;
}

int main() {

    scanner >> N >> M;
    g.resize(N);
    repeat(i, M) {
        int a, b;
        scanner >> a >> b;
        --a; --b;
        g.connect(a, b);
    }

    for (auto& a : g.vertex_to) {
        sort(ALL(a));
        a.erase(unique(ALL(a)), a.end());
    }

    auto ans = independentSet(g);
    // auto ans2 = vertex_cover(g);
    // assert(ans.first == N - ans2.first);
    // assert(ans.first == ans.second.size());

    printer << ans.first << '\n';


    return 0;
}

Submission Info

Submission Time
Task G - Mixture Drug
User m_buyoh
Language C++14 (GCC 5.4.1)
Score 0
Code Size 14291 Byte
Status WA
Exec Time 2103 ms
Memory 256 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 600
Status
AC × 3
AC × 16
WA × 25
TLE × 10
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All sample_01.txt, sample_02.txt, sample_03.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_12.txt, subtask_1_13.txt, subtask_1_14.txt, subtask_1_15.txt, subtask_1_16.txt, subtask_1_17.txt, subtask_1_18.txt, subtask_1_19.txt, subtask_1_2.txt, subtask_1_20.txt, subtask_1_21.txt, subtask_1_22.txt, subtask_1_23.txt, subtask_1_24.txt, subtask_1_25.txt, subtask_1_26.txt, subtask_1_27.txt, subtask_1_28.txt, subtask_1_29.txt, subtask_1_3.txt, subtask_1_30.txt, subtask_1_31.txt, subtask_1_32.txt, subtask_1_33.txt, subtask_1_34.txt, subtask_1_35.txt, subtask_1_36.txt, subtask_1_37.txt, subtask_1_38.txt, subtask_1_39.txt, subtask_1_4.txt, subtask_1_40.txt, subtask_1_41.txt, subtask_1_42.txt, subtask_1_43.txt, subtask_1_44.txt, subtask_1_45.txt, subtask_1_46.txt, subtask_1_47.txt, subtask_1_48.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt
Case Name Status Exec Time Memory
sample_01.txt AC 1 ms 256 KB
sample_02.txt AC 1 ms 256 KB
sample_03.txt AC 1 ms 256 KB
subtask_1_1.txt AC 1 ms 256 KB
subtask_1_10.txt TLE 2103 ms 256 KB
subtask_1_11.txt AC 1 ms 256 KB
subtask_1_12.txt WA 1205 ms 256 KB
subtask_1_13.txt AC 1 ms 256 KB
subtask_1_14.txt AC 1 ms 256 KB
subtask_1_15.txt WA 1 ms 256 KB
subtask_1_16.txt AC 1 ms 256 KB
subtask_1_17.txt AC 5 ms 256 KB
subtask_1_18.txt WA 448 ms 256 KB
subtask_1_19.txt WA 2 ms 256 KB
subtask_1_2.txt AC 1 ms 256 KB
subtask_1_20.txt TLE 2103 ms 256 KB
subtask_1_21.txt TLE 2103 ms 256 KB
subtask_1_22.txt WA 379 ms 256 KB
subtask_1_23.txt AC 1 ms 256 KB
subtask_1_24.txt WA 115 ms 256 KB
subtask_1_25.txt AC 1 ms 256 KB
subtask_1_26.txt TLE 2103 ms 256 KB
subtask_1_27.txt WA 131 ms 256 KB
subtask_1_28.txt WA 4 ms 256 KB
subtask_1_29.txt WA 2 ms 256 KB
subtask_1_3.txt WA 2 ms 256 KB
subtask_1_30.txt WA 1 ms 256 KB
subtask_1_31.txt WA 1 ms 256 KB
subtask_1_32.txt WA 2 ms 256 KB
subtask_1_33.txt WA 3 ms 256 KB
subtask_1_34.txt WA 1 ms 256 KB
subtask_1_35.txt WA 255 ms 256 KB
subtask_1_36.txt WA 137 ms 256 KB
subtask_1_37.txt WA 31 ms 256 KB
subtask_1_38.txt WA 2 ms 256 KB
subtask_1_39.txt WA 2 ms 256 KB
subtask_1_4.txt WA 2 ms 256 KB
subtask_1_40.txt WA 361 ms 256 KB
subtask_1_41.txt WA 127 ms 256 KB
subtask_1_42.txt TLE 2103 ms 256 KB
subtask_1_43.txt TLE 2103 ms 256 KB
subtask_1_44.txt AC 1151 ms 256 KB
subtask_1_45.txt AC 843 ms 256 KB
subtask_1_46.txt AC 1 ms 256 KB
subtask_1_47.txt TLE 2103 ms 256 KB
subtask_1_48.txt AC 1 ms 256 KB
subtask_1_5.txt TLE 2103 ms 256 KB
subtask_1_6.txt TLE 2103 ms 256 KB
subtask_1_7.txt WA 564 ms 256 KB
subtask_1_8.txt WA 1860 ms 256 KB
subtask_1_9.txt TLE 2103 ms 256 KB