题意:n个人,把他们围成几个圆圈,每个圆圈至少有3个人,并且相邻的两个人的标号只和为质数。
思路:首先按照奇偶拆分成二分图,然后原点向每个左部点连流量为2的边,每个右部点向汇点连流量为2的边。代表每个点左右有两个人。
这题要求输出方案。从左部点到右部点 edge[ cure ] = 0 代表这条有向边被选中,如果从右部点到左部点,edge[ cure ^1 ] = 0,代表这条边的反向边被选中。
#include <bits/stdc++.h>
using namespace std;
typedef int LL;
typedef LL lint;
const lint inf = 0x3f3f3f3f;
const int maxn = 1000;
int ans,n;
int a[maxn];
vector<int> ve[maxn];
struct dinic{
static const int N = 1000;
static const int M = 50000;
static const lint inf = 0x3f3f3f3f;
int he[N],ne[M],ver[M],d[N];
lint edge[M];
int s,t,tot;
queue<int> que;
void init( int n ){
for( int i = 0;i <= n;i++ ){
he[i] = 0;
}
tot = 1;
}
void add( int x,int y,lint z ){
ver[++tot] = y; ne[ tot ] = he[x]; he[x] = tot; edge[ tot ] = z;
ver[++tot] = x; ne[tot] = he[y]; he[y] = tot;edge[tot] = 0;
}
bool bfs(){
memset( d,0,sizeof( d ) );
while( que.size() ) que.pop();
que.push( s );
d[s] = 1;
while( que.size() ){
int x = que.front();
que.pop();
for( int cure = he[x];cure;cure = ne[cure] ){
int y = ver[cure];
if( edge[cure] && !d[y] ){
que.push( y );
d[ y ] = d[x] + 1;
if( y == t ) return 1;
}
}
}
return 0;
}
lint dfs( int x,lint flow ){
if( x== t ) return flow;
lint rest = flow,k;
for( int cure = he[x];cure && rest;cure = ne[cure] ){
int y = ver[cure];
if( edge[cure] && d[ y ] == d[x] + 1 ){
k = dfs( y,min( rest,edge[cure] ) );
if( !k ) d[ y ] = 0;
edge[cure] -= k;
edge[ cure^1 ] += k;
rest -= k;
}
}
return flow - rest;
}
lint max_flow( int x,int y ){
s = x; t = y;
lint maxflow = 0;
lint flow = 0;
while( bfs() )
while( flow = dfs( s,inf ) ) maxflow += flow;
return maxflow;
}
int v[maxn];
void dfs( int x ){
if(x != 0 && x != n+1)ve[ans].push_back(x);
v[x] = 1;
for( int cure = he[x];cure;cure = ne[cure] ){
if( (edge[cure] == 0 && a[x]&1) || (edge[cure^1] == 0 && a[x]%2 == 0) ) {
int y = ver[cure];
if (v[y]) continue;
dfs(y);
}
}
}
} g;
struct Primes{
static const int N = 20015;
int v[N],prime[N],m;
void primes( int n ){
memset( v,0,sizeof( v ) );
m = 0;
for( int i = 2;i <= n;i++ ){
if( v[i] == 0 ){
v[i] = i;
prime[++m] = i;
//num[i] = m;
}
for( int j = 1;j <= m;j++ ){
if( prime[j] > v[i] || prime[j] > n / i ) break;
v[ i * prime[j] ] = prime[j];
}
}
}
}p;
int main(){
p.primes( 20005 );
scanf("%d",&n);
for( int i = 1;i <= n;i++ ){
scanf("%d",&a[i]);
}
int S = 0,T = n+1;
g.init(T);
for( int i = 1;i <= n;i++ ){
if( a[i]&1 ){
g.add( S,i,2 );
}else{
g.add( i,T,2 );
continue;
}
for( int j = 1;j <= n;j++ ){
int sum = a[i] + a[j];
if( p.v[sum] == sum ){
g.add( i,j,1 );
}
}
}
int maxflow = g.max_flow(S,T);
if( maxflow != n ){
puts("Impossible");
return 0;
}
ans = 0;
for( int i = 1;i <= n;i++ ){
if( g.v[i] ) continue;
ans++;
g.dfs( i );
}
printf("%d\n",ans);
for( int i = 1;i <= ans;i++ ){
printf("%d ",ve[i].size());
for( int j = 0;j < ve[i].size();j++ ){
printf("%d ",ve[i][j]);
}
puts("");
}
return 0;
}