E. Fox And Dinner
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples
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
Note

In example 1, they can sit around one table, their ages are: 3-8-9-4, adjacent sums are: 11, 17, 13 and 7, all those integers are primes.

In example 2, it is not possible: the sum of 2+2 = 4 is not a prime number.

题意:给你n只狐狸,每只狐狸的年龄,你可以任意选择狐狸组成一张桌子,使得满足条件,并且全部坐上桌子;

   条件:1、一张桌子最少最三只狐狸。二、相邻的两只狐狸年龄相加为素数,最后一只与第一只相邻。3都要坐上桌子。

思路:因为两两相加为素数,两个奇数不可能相加为素数(题目中每个数都大于2),所以相邻为一奇一偶,并且奇数个数等于偶数个数;

   这样就可以把奇数与偶数分开,建立二分图,每个点需要匹配两个点,使得条件满足;

   源点与奇数建流,并且流量为2,同理,偶数与汇点建流;最大流等于n满足答案的条件。

   最后dfs暴力找答案的环;

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define PI acos(-1.0)
const int maxn=1e5+,maxm=1e5+,inf=0x3f3f3f3f,mod=1e9+,N=1e5+;
const ll INF=1e18+; vector<vector<int> >Ans;
vector<int>out;
int flag[N];
struct Dinic
{
struct edge
{
int from,to,cap,flow;
};
vector<edge>es;
vector<int>G[maxn];
bool vis[maxn];
int dist[maxn];
int iter[maxn];
void init(int n)
{
for(int i=; i<=n+; i++) G[i].clear();
es.clear();
}
void addedge(int from,int to,int cap)
{
es.push_back((edge)
{
from,to,cap,
});
es.push_back((edge)
{
to,from,,
});
int x=es.size();
G[from].push_back(x-);
G[to].push_back(x-);
}
bool BFS(int s,int t)
{
memset(vis,,sizeof(vis));
queue <int> Q;
vis[s]=;
dist[s]=;
Q.push(s);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for (int i=; i<G[u].size(); i++)
{
edge &e=es[G[u][i]];
if (!vis[e.to]&&e.cap>e.flow)
{
vis[e.to]=;
dist[e.to]=dist[u]+;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int u,int t,int f)
{
if(u==t||f==) return f;
int flow=,d;
for(int &i=iter[u]; i<G[u].size(); i++)
{
edge &e=es[G[u][i]];
if(dist[u]+==dist[e.to]&&(d=DFS(e.to,t,min(f,e.cap-e.flow)))>)
{
e.flow+=d;
es[G[u][i]^].flow-=d;
flow+=d;
f-=d;
if (f==) break;
}
}
return flow;
}
int Maxflow(int s,int t)
{
int flow=;
while(BFS(s,t))
{
memset(iter,,sizeof(iter));
int d=;
while(d=DFS(s,t,inf)) flow+=d;
}
return flow;
}
void solve(int u)
{
out.push_back(u);
for(int i=;i<G[u].size();i++)
{
edge x=es[G[u][i]];
//cout<<u<<" "<<x.from<<" "<<x.to<<" "<<x.flow<<endl;
if(flag[x.to]||x.flow==)continue;
flag[x.to]=;
solve(x.to);
}
}
} Din;
int a[N];
int vis[N];
void prime()
{
for(int i=; i<=; i++)
{
if(vis[i])continue;
for(int j=i+i; j<=; j+=i)
vis[j]=;
}
}
int main()
{
prime();
int n;
scanf("%d",&n);
Din.init(n);
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
if(a[i]%)Din.addedge(,i,);
else Din.addedge(i,n+,);
}
for(int i=; i<=n; i++)
{
for(int j=i+; j<=n; j++)
{
if(a[i]%&&a[j]%==)
{
if(!vis[a[i]+a[j]])
Din.addedge(i,j,);
}
else if(a[i]%==&&a[j]%)
{
if(!vis[a[i]+a[j]])
Din.addedge(j,i,);
}
}
}
int ans=Din.Maxflow(,n+); if(ans==n){
flag[]=;
flag[n+]=;
for(int i=;i<=n;i++)
{
if(!flag[i])
{
out.clear();
flag[i]=;
Din.solve(i);
Ans.push_back(out);
}
}
printf("%d\n",Ans.size());
for(int i=;i<Ans.size();i++)
{
printf("%d",Ans[i].size());
for(int j=;j<Ans[i].size();j++)
printf(" %d",Ans[i][j]);
printf("\n");
}
}
else
printf("Impossible\n");
return ;
}

Codeforces Round #290 (Div. 2) E. Fox And Dinner 网络流建模的更多相关文章

  1. Codeforces Round #290 (Div. 2) D. Fox And Jumping dp

    D. Fox And Jumping 题目连接: http://codeforces.com/contest/510/problem/D Description Fox Ciel is playing ...

  2. Codeforces Round #290 (Div. 2) C. Fox And Names dfs

    C. Fox And Names 题目连接: http://codeforces.com/contest/510/problem/C Description Fox Ciel is going to ...

  3. Codeforces Round #290 (Div. 2) B. Fox And Two Dots dfs

    B. Fox And Two Dots 题目连接: http://codeforces.com/contest/510/problem/B Description Fox Ciel is playin ...

  4. Codeforces Round #290 (Div. 2) A. Fox And Snake 水题

    A. Fox And Snake 题目连接: http://codeforces.com/contest/510/problem/A Description Fox Ciel starts to le ...

  5. Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)

    http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring&qu ...

  6. DFS Codeforces Round #290 (Div. 2) B. Fox And Two Dots

    题目传送门 /* DFS:每个点四处寻找,判断是否与前面的颜色相同,当走到已走过的表示成一个环 */ #include <cstdio> #include <iostream> ...

  7. 找规律 Codeforces Round #290 (Div. 2) A. Fox And Snake

    题目传送门 /* 水题 找规律输出 */ #include <cstdio> #include <iostream> #include <cstring> #inc ...

  8. 拓扑排序 Codeforces Round #290 (Div. 2) C. Fox And Names

    题目传送门 /* 给出n个字符串,求是否有一个“字典序”使得n个字符串是从小到大排序 拓扑排序 详细解释:http://www.2cto.com/kf/201502/374966.html */ #i ...

  9. Codeforces Round #228 (Div. 1) C. Fox and Card Game 博弈

    C. Fox and Card Game 题目连接: http://codeforces.com/contest/388/problem/C Description Fox Ciel is playi ...

随机推荐

  1. Python+OpenCV图像处理(十一)—— 图像金字塔

    简介:图像金字塔是图像中多尺度表达的一种,最主要用于图像的分割,是一种以多分辨率来解释图像的有效但概念简单的结构.简单来说,图像金字塔就是用来进行图像缩放的. 进行图像缩放可以用图像金字塔,也可以使用 ...

  2. Django之连接远程mysql数据库

    1.创建Django项目(test) 进入配置文件settings.py 192.168.83.129:所需要远程连接数据库的ip地址 2.进入到远程连接的主机,修改/etc/mysql/mysql. ...

  3. QQ项目(续)

    1.项目查找好友的原理 sql:select * from qquser where account in(select friendAccount from friend where userAcc ...

  4. log4j2笔记 #03# PatternLayout

    该类的目标是格式化LogEvent并返回(字符串)结果.结果的格式取决于具体的模式字符串(pattern string).这里的模式字符串与c语言中printf函数的转换模式非常相似.模式字符串由“转 ...

  5. VMware14安装centos7

    win10专业版 虚拟机:14 Pro 1. 新建虚拟机选择典型安装 2. 稍后安装操作系统 3. 选择Linux,版本选择centso7 64位(根据系统选择) 4. 设置虚拟机名称并选择安装位置 ...

  6. spring boot 1.x完整学习指南(含各种常见问题servlet、web.xml、maven打包,spring mvc差别及解决方法)

    spring boot 入门 关于版本的选择,spring boot 2.0开始依赖于 Spring Framework 5.1.0,而spring 5.x和之前的版本差距比较大,而且应该来说还没有广 ...

  7. k8s build new API

    apiserver-builder git hub api conventions storage api arch step by step,   we can follow it. api con ...

  8. html5-表单属性及<!DOCTYPE> 标签

    <!DOCTYPE> 标签定义和用法<!DOCTYPE> 声明必须位于HTML 5 文档中的第一行,也就是位于<html> 标签之前.该标签告知浏览器文档所使用的H ...

  9. 前端 --- 4 js

    一.js  描述 JavaScript 是脚本语言 JavaScript 是一种轻量级的编程语言.后来出现了node.js,可以作为后端语言来开发项目, js是一个既能做前端又能做后端的语言 Java ...

  10. uniGUI试用笔记(六)

    uniGUI提供了一个文件上传控件TUniFileUpload,进行数据的导入就变得比较容易.首先将TUniFileUpload控件放置在窗体上,按下导入按钮后,执行TUniFileUpload的文件 ...