結果

問題 No.382 シャイな人たち (2)
ユーザー koprickykopricky
提出日時 2019-07-23 02:02:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,281 ms / 8,000 ms
コード長 12,100 bytes
コンパイル時間 3,222 ms
コンパイル使用メモリ 194,672 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-24 00:29:01
合計ジャッジ時間 26,469 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,281 ms
5,248 KB
testcase_01 AC 1,211 ms
5,376 KB
testcase_02 AC 1,123 ms
5,376 KB
testcase_03 AC 1,063 ms
5,376 KB
testcase_04 AC 1,068 ms
5,376 KB
testcase_05 AC 1,151 ms
5,376 KB
testcase_06 AC 1,000 ms
5,376 KB
testcase_07 AC 1,139 ms
5,376 KB
testcase_08 AC 1,010 ms
5,376 KB
testcase_09 AC 1,002 ms
5,376 KB
testcase_10 AC 1,059 ms
5,376 KB
testcase_11 AC 956 ms
5,376 KB
testcase_12 AC 952 ms
5,376 KB
testcase_13 AC 1,058 ms
5,376 KB
testcase_14 AC 1,129 ms
5,376 KB
testcase_15 AC 1,051 ms
5,376 KB
testcase_16 AC 1,046 ms
5,376 KB
testcase_17 AC 899 ms
5,376 KB
testcase_18 AC 1,136 ms
5,376 KB
testcase_19 AC 927 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx,ssse3,sse4a")
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#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 srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

template<typename T> class ListIterator;

template<typename T>
class List {
public:
    using iterator = ListIterator<T>;

private:
    struct block {
        block *prev, *next;
        T data;
        block() : prev(this), next(this){}
        block(T _data) : prev(this), next(this), data(_data){}
        block(block *_prev, block *_next) : prev(_prev), next(_next){}
    };
    int N;
    size_t sz;
    vector<block> container;
    friend ListIterator<T>;
    // cur を nx の前に挿入
    iterator insert(block* const nx, block* const cur){
        ++sz;
        cur->prev = nx->prev, cur->next = nx;
        nx->prev->next = cur, nx->prev = cur;
        return iterator(cur);
    }
    // cur を削除
    iterator erase(block* const cur){
        --sz;
        cur->prev->next = cur->next, cur->next->prev = cur->prev;
        return iterator(cur->next);
    }

public:
    iterator insert(const iterator nx, const iterator cur){ return insert(nx.ls_ptr, cur.ls_ptr); }
    iterator insert(const iterator nx, int cur_index){ return insert(nx.ls_ptr, &container[cur_index]); }
    iterator insert(int next_index, int cur_index){ return iterator(&container[next_index], &container[cur_index]); }
    iterator erase(const iterator cur){ return erase(cur.ls_ptr); }
    iterator erase(int cur_index){ return erase(&container[cur_index]); }

public:
    List() : N(0), sz(0){}
    List(int _N) : N(_N), sz(N), container(_N+1){}
    List(const vector<T>& _data) : List((int)_data.size()){ build(_data); }
    List(const List& ls) : N(ls.N), sz(ls.sz), container(ls.container){}
    void build(const vector<T>& _data){
        for(int i = 0; i <= N; i++){
            container[i].prev = &container[(i+N)%(N+1)];
            container[i].next = &container[(i+1)%(N+1)];
            if(i < N) container[i].data = _data[i];
        }
    }
    void push_back(const T _data){ ++N, ++sz, container.push_back(_data); }
    void build(){
        container.push_back(T());
        for(int i = 0; i <= N; i++){
            container[i].prev = &container[(i+N)%(N+1)];
            container[i].next = &container[(i+1)%(N+1)];
        }
    }
    friend ostream& operator<< (ostream& os, List& ls){
        for(auto& val : ls) os << val << ' ';
        return os;
    }
    const T& operator[](size_t index) const { return container[index].data; }
    T& operator[](size_t index){ return container[index].data; }
    size_t size() const { return sz; }
    bool empty() const { return (size() == 0); }
    T& front(){ return container[N].next->data; }
    T& back(){ return container[N].prev->data; }
    iterator begin(){ return iterator(container[N].next); }
    iterator end(){ return iterator(&container[N]); }
};

template<typename T>
class ListIterator : public std::iterator<bidirectional_iterator_tag, T, ptrdiff_t, T*, T&>{
private:
    friend List<T>;
    typename List<T>::block *ls_ptr;
    using base = std::iterator<bidirectional_iterator_tag, T, ptrdiff_t, T*, T&>;
    using value_type = typename base::value_type;
    using pointer = typename base::pointer;
    using reference = typename base::reference;

private:
    ListIterator(typename List<T>::block *ls) : ls_ptr(ls){}

public:
    ListIterator(){}
    ListIterator(const ListIterator& itr) : ls_ptr(itr.ls_ptr){}
    ListIterator& operator=(const ListIterator& itr) & { return ls_ptr = itr.ls_ptr, *this; }
    ListIterator& operator=(ListIterator&& itr) & { return ls_ptr = itr.ls_ptr, *this; }
    reference operator*() const { return ls_ptr->data; }
    pointer operator->() const { return &(ls_ptr->data); }
    ListIterator& operator++(){ return ls_ptr = ls_ptr->next, *this; }
    ListIterator operator++(int){
        ListIterator res = *this;
        return res.ls_ptr = ls_ptr->next, res;
    }
    ListIterator& operator--(){ return ls_ptr = ls_ptr->prev, *this; }
    ListIterator operator--(int){
        ListIterator res = *this;
        return res.ls_ptr = ls_ptr->prev, res;
    };
    bool operator==(const ListIterator& itr) const { return !(*this != itr); };
    bool operator!=(const ListIterator& itr) const { return ls_ptr != itr.ls_ptr; }
};

// 多重辺はなし
class MVC {
private:
    struct edge {
        int to, rev;
    };
    struct info {
        // from から to 番目に出ている辺が(を) next の前から消えた(に入れる)
        int from, to;
        List<edge>::iterator next;
        info(int ag1, int ag2, List<edge>::iterator ag3) :
            from(ag1), to(ag2), next(ag3){}
    };

    using LL = unsigned __int128;

    int V, rem_edge_size, fp_size;
    vector<List<edge> > G;
    List<int> rem_ver;
    vector<int> cand;
    vector<vector<info> > edge_info;
    LL small_deg_ver, use_ver;

    int get_first_element(LL num){
        int x = __builtin_ffsll(num & (((LL)1 << 64) - 1));
        int y = __builtin_ffsll((num >> 64) & (((LL)1 << 64) - 1));
        return x ? (x - 1) : (y + 63);
    }
    bool push_cand(int u, int& add_size){
        add_size++, cand.push_back(u);
        return (int)cand.size() >= ans_size;
    }
    bool erase(int u, bool use, bool alr_use, int& add_size, vector<pair<int, List<int>::iterator> >& erase_ver){
        if(use && !alr_use){
            if(push_cand(u, add_size)) return true;
        }
        erase_ver.emplace_back(u, rem_ver.erase(u));
        for(auto& e : G[u]){
            edge_info[u].emplace_back(e.to, e.rev, G[e.to].erase(e.rev)), rem_edge_size--;
            if(G[e.to].empty()) erase_ver.emplace_back(e.to, rem_ver.erase(e.to));
            if(!use){
                if(use_ver >> e.to & 1) continue;
                if(push_cand(e.to, add_size)) return true;
                if(!G[e.to].empty()) use_ver |= (LL)1 << e.to;
            }else{
                if((int)G[e.to].size() == 1 && !(use_ver >> e.to & 1)) small_deg_ver |= (LL)1 << e.to;
            }
        }
        return false;
    }
    bool erase_rec_set(LL& st, bool flag, int& add_size, vector<pair<int, List<int>::iterator> >& erase_ver){
        while(st != 0){
            int v = get_first_element(st);
            st ^= (LL)1 << v;
            if(G[v].empty()) continue;
            if(erase(v, flag, flag, add_size, erase_ver)) return true;
        }
        return false;
    }
    bool reset(){
        small_deg_ver = use_ver = 0;
        return true;
    }
    bool erase_rec(int u, bool use, int& add_size, vector<pair<int, List<int>::iterator> >& erase_ver){
        if(erase(u, use, false, add_size, erase_ver)) return reset();
        while(small_deg_ver != 0 || use_ver != 0){
            if(erase_rec_set(small_deg_ver, false, add_size, erase_ver)) return reset();
            if(erase_rec_set(use_ver, true, add_size, erase_ver)) return reset();
        }
        return false;
    }
    bool preprocessing(int K, int& add_size, vector<pair<int, List<int>::iterator> >& erase_ver){
        for(int u : rem_ver){
            if((int)G[u].size() > K){
                if(!erase_rec(u, true, add_size, erase_ver)) return true;
            }
        }
        return false;
    }
    void postprocessing(int K, int add_size, vector<pair<int, List<int>::iterator> >& erase_ver){
        if(K == fp_size) return;
        for(int i = 0; i < add_size; i++){
            cand.pop_back();
        }
        for(int i = (int)erase_ver.size() - 1; i >= 0; i--){
            auto& p = erase_ver[i];
            while(!edge_info[p.first].empty()){
                auto& dat = edge_info[p.first].back();
                G[dat.from].insert(dat.next, dat.to), rem_edge_size++;
                edge_info[p.first].pop_back();
            }
            rem_ver.insert(p.second, p.first);
        }
    }
    bool check(){
        if(rem_edge_size == 0){
            if((int)cand.size() < ans_size){
                ans_size = (int)cand.size(), ans_set = cand;
            }
            return true;
        }
        return rem_ver.empty();
    }
    void transition(int K, int next_ver, bool use){
        int pl_add_size = 0;
        vector<pair<int, List<int>::iterator> > pl_erase_ver;
        if(erase_rec(next_ver, use, pl_add_size, pl_erase_ver)){
            return postprocessing(K-1, pl_add_size, pl_erase_ver);
        }
        if(!check()) judge(K-1);
        return postprocessing(K-1, pl_add_size, pl_erase_ver);
    }
    int find_max_degree_ver(){
        int max_deg = -1, next_ver = -1;
        for(int v : rem_ver){
            if((int)G[v].size() > max_deg) max_deg = (int)G[v].size(), next_ver = v;
        }
        if(2 * rem_edge_size > max_deg * (int)rem_ver.size()) return -1;
        return next_ver;
    }
    void judge(int K){
        int add_size = 0;
        vector<pair<int, List<int>::iterator> > erase_ver;
        if(preprocessing(K, add_size, erase_ver)) return postprocessing(K, add_size, erase_ver);
        if(check()) return postprocessing(K, add_size, erase_ver);
        int next_ver = find_max_degree_ver();
        if(next_ver < 0) return postprocessing(K, add_size, erase_ver);
        // 使う場合
        if((int)G[next_ver].size() > 1) transition(K, next_ver, true);
        // 使わない場合
        if((int)cand.size() + (int)G[next_ver].size() < ans_size) transition(K, next_ver, false);
        return postprocessing(K, add_size, erase_ver);
    }
public:
    vector<int> ans_set;
    int ans_size;

    MVC(int node_size) :
        V(node_size), rem_edge_size(0), G(V), rem_ver(V), edge_info(V), small_deg_ver(0), use_ver(0){
        vector<int> _init(V);
        iota(_init.begin(), _init.end(), 0);
        rem_ver.build(_init);
    }
    void add_edge(int u, int v){
        G[u].push_back((edge){v, (int)G[v].size()});
        G[v].push_back((edge){u, (int)G[u].size() - 1});
        rem_edge_size++;
    }
    bool solve(int K){
        for(int i = 0; i < V; ++i) G[i].build();
        fp_size = K, ans_size = fp_size + 1;
        judge(fp_size);
        return ans_size <= fp_size;
    }
};

bool used[121];

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll S;
    cin >> S;
    S = S * 12345LL % 1000003LL;
    int n = (S % 120) + 2, m = 0;
    MVC mvc(n);
    S = S * 12345LL % 1000003LL;
    ll P = S;
    rep(i,n){
        srep(j,i+1,n){
            S = S * 12345LL % 1000003LL;
            if(S >= P){
                mvc.add_edge(i, j);
                m++;
            }
        }
    }
    if(m == 0){
        cout << "-1\n";
    }else{
		mvc.solve(n);
        cout << n - mvc.ans_size + 1 << "\n";
		each(v, mvc.ans_set){
			used[v] = true;
		}
		vector<int> ans;
		rep(i,n){
			if(!used[i]) ans.pb(i);
		}
		rep(i,len(ans)-1){
			cout << ans[i] + 1 << " ";
		}
		cout << ans.back() + 1 << "\n";
    }
    return 0;
}
0