ferinの競プロ帳

競プロについてのメモ

Codeforces Round #290 (Div. 1) C. Fox And Dinner

問題ページ

解法

2以外の偶数は素数にならないことからa[i]とa[j]の偶奇が一致するときa[i]+a[j]は素数にならない
a[i]+a[j]が素数であれば頂点iと頂点jを結んだグラフは二部グラフになる
二部グラフから各頂点の次数が2になるように辺集合を選ぶ問題に帰着できた
これは二部マッチングと同様にフローを使って求めることができる

source → 奇数 に容量2の辺
偶数 → sink に容量2の辺
奇数 → 偶数 に容量1の辺

maxflowがNであれば条件を満たす分割がある
フローの結果、流れている辺が辺集合に属すると判断できるので復元も可能

閉路で分割
→頂点の次数が全て2
→マッチングのような辺集合を見つければよい
→二部グラフならこれはできる

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
// #define int ll
using PII = pair<ll, ll>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()

template<typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template<typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }

template<typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts) {
    return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}
template<typename T,typename V> typename enable_if<is_class<T>::value==0>::type
fill_v(T &t, const V &v) { t=v; }
template<typename T,typename V> typename enable_if<is_class<T>::value!=0>::type
fill_v(T &t, const V &v ) { for(auto &e:t) fill_v(e,v); }

template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a) {
    out<<'('<<a.first<<','<<a.second<<')'; return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
    out<<'[';
    for(const T &i: a) out<<i<<',';
    out<<']';
    return out;
}
template<class T>
ostream &operator <<(ostream& out, const set<T>& a) {
    out<<'{';
    for(const T &i: a) out<<i<<',';
    out<<'}';
    return out;
}
template<class T, class S>
ostream &operator <<(ostream& out, const map<T,S>& a) {
    out<<'{';
    for(auto &i: a) out<<i<<',';
    out<<'}';
    return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; // DRUL
const int INF = 1<<30;
const ll LLINF = 1LL<<60;
const ll MOD = 1000000007;

struct FordFulkerson {
    struct edge {
        int to;
        ll cap;
        int rev;
        bool isrev;
    };

    vector<vector<edge>> g;
    vector<int> used;
    int timestamp;

    FordFulkerson() {}
    FordFulkerson(int n) : g(n), used(n, -1), timestamp(0) {}

    void add_edge(int from, int to, ll cap) {
        g[from].emplace_back((edge){to, cap, (int)g[to].size(), false});
        g[to].emplace_back((edge){from, 0, (int)g[from].size()-1, true});
    }

    ll dfs(int idx, const int t, ll flow) {
        if(idx == t) return flow;
        used[idx] = timestamp;
        for(auto &e : g[idx]) {
            if(e.cap > 0 && used[e.to] != timestamp) {
                ll d = dfs(e.to, t, min(flow, e.cap));
                if(d > 0) {
                    e.cap -= d;
                    g[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }

    ll max_flow(int s, int t) {
        ll flow = 0;
        ++timestamp;
        for(ll f; (f = dfs(s, t, INF)) > 0; timestamp++) {
            flow += f;
        }
        return flow;
    }
};
ostream &operator <<(ostream& out, const FordFulkerson& a){
    out << "-----" << endl;
    for(int i = 0; i < (ll)a.g.size(); i++) {
        for(auto &e : a.g[i]) {
            if(e.isrev) continue;
            auto &rev_e = a.g[e.to][e.rev];
            out << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << endl;
        }
    }
    out << "-----" << endl;
    return out;
}

vector<bool> eratosthenes(ll n=1000000) {
    vector<bool> prime(n, true);
    prime[0] = prime[1] = false;
    for (int i = 2; i * i <= n; i++) {
        if (prime[i]) {
            for (int j = 2 * i; j <= n; j += i) {
                prime[j] = false;
            }
        }
    }
    return prime;
}

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll n;
    cin >> n;
    vector<ll> a(n);
    REP(i, n) cin >> a[i];

    auto prime = eratosthenes(20001);

    FordFulkerson flow(n+2);
    ll s = n, t = n+1;
    REP(i, n) {
        if(a[i]%2) flow.add_edge(s, i, 2);
        else flow.add_edge(i, t, 2);
    }
    REP(i, n) REP(j, n) {
        if(a[i]%2 && a[j]%2==0 && prime[a[i]+a[j]]) {
            flow.add_edge(i, j, 1);
        }
    }

    if(flow.max_flow(s, t) != n) {
        cout << "Impossible" << endl;
        return 0;
    }

    vector<vector<ll>> v(n);
    REP(i, n) {
        for(auto e: flow.g[i]) {
            if(!e.isrev && e.cap == 0 && e.to < n) {
                // i と e.to は隣接
                v[i].push_back(e.to);
                v[e.to].push_back(i);
            }
        }
    }

    vector<bool> used(n);
    function<vector<ll>(ll,ll)> dfs = [&](ll ver, ll pre) {
        vector<ll> ret;
        used[ver] = true;

        if(v[ver][0]!=pre && !used[v[ver][0]]) ret = dfs(v[ver][0], ver);
        else if(v[ver][1]!= pre && !used[v[ver][1]]) ret = dfs(v[ver][1], ver);

        ret.push_back(ver);
        return ret;
    };

    vector<vector<ll>> ans;
    REP(i, n) if(!used[i]) ans.push_back(dfs(i, -1));

    cout << ans.size() << endl;
    REP(i, ans.size()) {
        cout << ans[i].size();
        for(auto j: ans[i]) cout << " " << j+1;
        cout << endl;
    }

    return 0;
}