大意: n只狐狸, 要求分成若干个环, 每个环的狐狸不少于三只, 相邻狐狸年龄和为素数.
狐狸年龄都>=2, 那么素数一定为奇数, 相邻必须是一奇一偶, 也就是一个二分图, 源点向奇数点连容量为2的边, 偶数点向汇点连容量为2的边, 和为偶数的奇数点向偶数点连容量为1的边, 看能否满流即可.
#include <iostream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #include <string> #include <string.h> #include <bitset> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl ' ' #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;} //head const int N = 1e5+10; int n, m, S, T, cnt; struct _ { int from,to,w; void pr() { printf("fro=%d,to=%d,w=%d ",from,to,w); } }; vector<_> E; vector<int> g[N]; int a[N], pre[N], vis[N], d[N]; void add(int x, int y, int w) { g[x].pb(E.size()); E.pb({x,y,w}); g[y].pb(E.size()); E.pb({y,x,0}); } void seive(int n) { int mx = sqrt(n+0.5); vis[1] = 1; REP(i,2,mx) if (!vis[i]) { for (int j=i*i; j<=n; j+=i) vis[j]=1; } } void build() { S = n+1, T = n+2; REP(i,1,n) { if (a[i]&1) add(S,i,2); else add(i,T,2); } REP(i,1,n) if (a[i]&1) { REP(j,1,n) if (!vis[a[i]+a[j]]) add(i,j,1); } } int Maxflow(int s, int t) { int ans = 0; for (; ; ) { REP(i,1,n+2) d[i] = 0; queue<int> q; q.push(s); d[s] = INF; while (!q.empty()) { int x = q.front();q.pop(); for (auto &t:g[x]) { auto &e = E[t]; if (!d[e.to]&&e.w>0) { pre[e.to] = t; d[e.to] = min(d[x], e.w); q.push(e.to); } } if (d[t]) break; } if (!d[t]) break; for (int u=t; u!=s; u=E[pre[u]].from) { E[pre[u]].w -= d[t]; E[pre[u]^1].w += d[t]; } ans += d[t]; } return ans; } vector<int> gg[N]; vector<int> ans[N]; void dfs(int x) { if (vis[x]) return; vis[x] = 1; ans[cnt].pb(x); for (int y:gg[x]) dfs(y); } int main() { scanf("%d", &n); REP(i,1,n) scanf("%d", a+i); seive(20000); build(); if (Maxflow(S,T)!=n) return puts("Impossible"),0; REP(i,1,n) if (a[i]&1^1) { for (auto &&j:g[i]) if (E[j].w==1) { gg[E[j].from].pb(E[j].to); gg[E[j].to].pb(E[j].from); } } memset(vis,0,sizeof vis); REP(i,1,n) if (!vis[i]) ++cnt,dfs(i); printf("%d ", cnt); REP(i,1,cnt) { printf("%d ",int(ans[i].size())); for (auto &&t:ans[i]) printf("%d ",t);hr; } }