Fox Ciel is participating in a party in Prime Kingdom. There are n foxes there (include Fox Ciel). The i-th fox is ai years old.

They will have dinner around some round tables. You want to distribute foxes such that:

  1. Each fox is sitting at some table.
  2. Each table has at least 3 foxes sitting around it.
  3. The sum of ages of any two adjacent foxes around each table should be a prime number.

If k foxes f1, f2, ..., fk are sitting around table in clockwise order, then for 1 ≤ i ≤ k - 1: fi and fi + 1 are adjacent, and f1 and fk are also adjacent.

If it is possible to distribute the foxes in the desired manner, find out a way to do that.

 

Input

The first line contains single integer n (3 ≤ n ≤ 200): the number of foxes in this party.

The second line contains n integers ai (2 ≤ ai ≤ 104).

 

Output

If it is impossible to do this, output "Impossible".

Otherwise, in the first line output an integer m (): the number of tables.

Then output m lines, each line should start with an integer k -=– the number of foxes around that table, and then k numbers — indices of fox sitting around that table in clockwise order.

If there are several possible arrangements, output any of them.

 

Sample Input

Input
4
3 4 8 9
Output
1
4 1 2 4 3
Input
5
2 2 2 2 2
Output
Impossible
Input
12
2 3 4 5 6 7 8 9 10 11 12 13
Output
1
12 1 2 3 6 5 12 9 8 7 10 11 4
Input
24
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Output
3
6 1 2 3 6 5 4
10 7 8 9 12 15 14 13 16 11 10
8 17 18 23 22 19 20 21 24
  建二分图,再用流量为2限制度数,即可用网络流AC。
 #include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int N=,M=,INF=;
int cnt,fir[N],fron[N],nxt[M],to[M],cap[M];
int dis[N],gap[N],path[N],vis[N],q[N],h,t;
int n,m,a[N],tp[N],tot;vector<int>ans[N];
struct Net_Flow{
void Init(){
memset(fir,,sizeof(fir));
memset(gap,,sizeof(gap));
memset(dis,,sizeof(dis));
h=t=cnt=;
}
void addedge(int a,int b,int c){
nxt[++cnt]=fir[a];to[fir[a]=cnt]=b;cap[cnt]=c;
nxt[++cnt]=fir[b];to[fir[b]=cnt]=a;cap[cnt]=;
}
bool BFS(int S,int T){
dis[q[h]=T]=;
while(h<=t){
int x=q[h++];
for(int i=fir[x];i;i=nxt[i])
if(!dis[to[i]])dis[q[++t]=to[i]]=dis[x]+;
}
return dis[S];
}
int ISAP(int S,int T){
if(!BFS(S,T))return ;
for(int i=S;i<=T;i++)gap[dis[i]]+=;
for(int i=S;i<=T;i++)fron[i]=fir[i];
int ret=,f,p=S,Min;
while(dis[S]<=T+){
if(p==T){f=INF;
while(p!=S){
f=min(f,cap[path[p]]);
p=to[path[p]^];
}ret+=f,p=T;
while(p!=S){
cap[path[p]]-=f;
cap[path[p]^]+=f;
p=to[path[p]^];
}
}
for(int &i=fron[p];i;i=nxt[i])
if(cap[i]&&dis[to[i]]==dis[p]-){
path[p=to[i]]=i;break;
}
if(!fron[p]){
if(!--gap[dis[p]])break;Min=T+;
for(int i=fir[p];i;i=nxt[i])
if(cap[i])Min=min(Min,dis[to[i]]);
gap[dis[p]=Min+]+=;fron[p]=fir[p];
if(p!=S)p=to[path[p]^];
}
}
return ret;
}
void DFS(int x,int id){
vis[x]=;ans[id].push_back(x);
for(int i=fir[x];i;i=nxt[i]){
if(vis[to[i]]||to[i]<||to[i]>n)continue;
if(a[to[i]]%==&&cap[i^]==)DFS(to[i],id);
if(a[to[i]]%!=&&cap[i]==)DFS(to[i],id);
}
}
void Solve(int S,int T){
for(int x=;x<=n;x++)
if(a[x]%==&&!vis[x])
DFS(x,++tot);
printf("%d\n",tot);
for(int i=;i<=tot;i++){
printf("%d ",ans[i].size());
for(int j=;j<ans[i].size();j++)
printf("%d ",ans[i][j]);
puts("");
}
}
}isap;
int S,T;
bool Check(int x){
for(int i=;i*i<=x;i++)
if(x%i==)return false;
return true;
}
int main(){
scanf("%d",&n);isap.Init();T=n+;
if(n%==){puts("Impossible");return ;}
for(int i=;i<=n;i++)scanf("%d",&a[i]);
for(int i=;i<=n;i++){
if(a[i]%==)isap.addedge(S,i,);
else isap.addedge(i,T,);
}
for(int i=;i<=n;i++)if(a[i]%==)
for(int j=;j<=n;j++)if(a[j]%)
if(Check(a[i]+a[j]))
isap.addedge(i,j,);
if(isap.ISAP(S,T)!=n){
puts("Impossible");
return ;
}
isap.Solve(S,T);
return ;
}

网络流(最大流)CodeForces 512C:Fox And Dinner的更多相关文章

  1. 【Codeforces】512C Fox and Dinner

    [解析]欧拉筛法,奇偶分析.建二分图,网络流 [Analysis] http://blog.csdn.net/qq574857122/article/details/43453087. 所谓的连通块就 ...

  2. codeforces 510E. Fox And Dinner 网络流

    题目链接 给出n个人, 以及每个人的值, 要求他们坐在一些桌子上面, 每个桌子如果有人坐, 就必须做3个人以上. 并且相邻的两个人的值加起来必须是素数.每个人的值都>=2. 由大于等于2这个条件 ...

  3. CodeForces 510E Fox And Dinner

    网络流. 原点到偶数连边,容量为2, 奇数到汇点连边,容量为2, 偶数到与之能凑成素数的奇数连边,容量为1 如果奇数个数不等于偶数个数,输出不可能 如果原点到偶数的边不满流,输出不可能 剩下的情况有解 ...

  4. Codeforces Round #290 (Div. 2) E. Fox And Dinner 网络流建模

    E. Fox And Dinner time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  5. CF510E. Fox And Dinner

    CF510E. Fox And Dinner https://codeforces.com/contest/510 分析: 由于\(a_i>2\), 相邻两个数一定一奇一偶,按奇偶建立二分图. ...

  6. POJ 1459-Power Network(网络流-最大流-ISAP)C++

    Power Network 时间限制: 1 Sec  内存限制: 128 MB 题目描述 A power network consists of nodes (power stations, cons ...

  7. [POJ1273][USACO4.2]Drainage Ditches (网络流最大流)

    题意 网络流最大流模板 思路 EK也不会超时 所以说是一个数据比较水的模板题 但是POJ有点坑,多组数据,而且题目没给 哭得我AC率直掉 代码 用的朴素Dinic #include<cstdio ...

  8. HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)

    HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...

  9. HDU1532 网络流最大流【EK算法】(模板题)

    <题目链接> 题目大意: 一个农夫他家的农田每次下雨都会被淹,所以这个农夫就修建了排水系统,还聪明的给每个排水管道设置了最大流量:首先输入两个数n,m ;n为排水管道的数量,m为节点的数量 ...

  10. Redraw Beautiful Drawings(hdu4888)网络流+最大流

    Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...

随机推荐

  1. CSS自动控制图片大小的代码

    img { max-width: 800px; height: auto; } 代码中的max-width:800px限制图片的最大宽度为800像素,而下面的hight:auto很关键,可以保证图片有 ...

  2. MySQL数字类型中的三种常用种类

    数字类型 MySQL数字类型按照我的分类方法分为三类:整数类.小数类和数字类. MySQL数字类型之一我所谓的“数字类” 就是指 DECIMAL 和 NUMERIC,它们是同一种类型.它严格的说不是一 ...

  3. 学习java随笔第六篇:数组

    一维数组 创建一维数组并输出 public class OneDimensionalArray { public static void main(String argas[]) { int i[]= ...

  4. GPS定位,经纬度附近地点查询–C#实现方法

    目前的工作是需要手机查找附近N米以内的商户,功能如下图 数据库中记录了商家在百度标注的经纬度(如:116.412007, 39.947545), 最初想法  以圆心点为中心点,对半径做循环,半径每增加 ...

  5. 面向报文(UDP)和面向字节流(TCP)的区别

    转载:http://blog.csdn.net/wanghaobo920/article/details/11877589 面向报文(UDP)和面向字节流(TCP)的区别 面向报文的传输方式是应用层交 ...

  6. doj常用包

    dojo.raise               抛出一个异常 dojo.errorToString将异常转换为字符串 dojo.render      系统环境对象 dojo.hostenv. ...

  7. 2.Oracle11g体系结构

    2.1逻辑存储结构 2.1.1数据块(Data Blocks) 数据块是Oracle逻辑结构中最小的逻辑单位,也是执行数据库输入输出最小的存储单位.通常Oracle数据库是操作系统块的整数倍,如果操作 ...

  8. jquery 过滤器

    1.基本选择器 基本选择器是JQuery中最常用的选择器,也是最简单的选择器,它通过元素id.class 和标签名来查找DOM元素.这个非常重要,下面的内容都是以此为基础,逐级提高的. 1).“$(“ ...

  9. 实习之vim基本学习

    最近实习学到了写vim的基本用法,记录一下 批量注释 ctrl+v进入列模式,按“I”进入插入模式,按// #等在每行开头插入注释,esc 批量去除注释 ctrl + v 进入列模式,按“x”即可. ...

  10. Activity启动模式图文详解

    转载自:http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0520/2897.html  英文原文:Understand Android A ...