CF510E: Fox And Dinner(最大流)

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 f1f2, ..., fk are sitting around table in clockwise order, then for 1 ≤ i ≤ k - 1fi 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
Copy
4
3 4 8 9
output
1
4 1 2 4 3
input
Copy
5
2 2 2 2 2
output
Impossible
input
Copy
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
Copy
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个带权的人,问能否分到若干张圆桌上,要求每张桌人数>=3,

且每张桌上相邻两个人的权值和为素数,输出其中一种方案。

思路:容易想到二分图匹配,但是控制不了每桌人数>=3。由于相加的和为素数,则必然是一奇一偶相加,那么分奇偶两边建图,源点-(2)-奇数-(1)-偶数-(2)-汇点跑最大流即可,输出路径dfs时用双端队列就行。

# include <bits/stdc++.h>
using namespace std;
const int maxn = 250000;
struct node{int v, next, w;}edge[maxn];
int Next[maxn], cnt=0, source, sink, dis[maxn], q[maxn];
int e[203], o[203], a[203], vis[203], cur;
list<int>g[203];
bool ok(int x)
{
    for(int i=2; i<=sqrt(x); ++i)
        if(x%i==0) return false;
    return true;
}
void add(int u, int v, int w)
{
    edge[cnt]={v,Next[u], w};
    Next[u]=cnt++;
    edge[cnt]={u,Next[v],0};
    Next[v]=cnt++;
}
bool bfs()
{
    memset(dis, -1, sizeof(dis));
    dis[source]=0;
    int l=0, r=0;
    q[r++]=source;
    while(l<r)
    {
        int u=q[l++];
        for(int i=Next[u]; ~i; i=edge[i].next)
        {
            int v=edge[i].v, w=edge[i].w;
            if(w>0 && dis[v]==-1)
            {
                dis[v] = dis[u]+1;
                q[r++]=v;
            }
        }
    }
    return dis[sink]!=-1;
}
int dfs(int u, int pre)
{
    if(u == sink) return pre;
    int ans=0, f=0;
    for(int i=Next[u]; ~i; i=edge[i].next)
    {
        int v=edge[i].v, w=edge[i].w;
        if(dis[v]==dis[u]+1 && w>0 && (f=dfs(v, min(w, pre))))
        {
            edge[i].w  -= f;
            edge[i^1].w += f;
            pre -= f;
            ans += f;
            if(!pre) break;
        }
    }
    if(ans) return ans;
    dis[u]=-1;
    return 0;
}
void dfs2(int u, int bl, int flag)
{
    vis[u] = 1;
    if(flag) g[bl].push_front(u);
    else g[bl].push_back(u);
    for(int i=Next[u]; ~i; i=edge[i].next)
    {
        int v=edge[i].v, w=edge[i].w;
        if(v==source || v==sink) continue;
        if((i&1) && w==1 && !vis[v]) dfs2(v, bl, flag);
        if(!(i&1) && w==0 && !vis[v]) dfs2(v, bl, (u==cur)?!flag:flag);
    }
}
int main()
{
    memset(Next, -1, sizeof(Next));
    int n, max_flow=0, tot=0;
    scanf("%d",&n);
    source=0, sink=n+1;
    for(int i=1; i<=n; ++i)
    {
        scanf("%d",&a[i]);
        if(a[i]&1) o[++o[0]]=i,add(source, i, 2);
        else e[++e[0]]=i, add(i, sink, 2);
    }
    if(o[0] != e[0]) return 0*puts("Impossible");
    for(int i=1; i<=o[0]; ++i)
        for(int j=1; j<=e[0]; ++j)
            if(ok(a[o[i]]+a[e[j]])) add(o[i], e[j], 1);
    while(bfs()) max_flow += dfs(source, 0x3f3f3f3f);
    if(max_flow != n) return 0*puts("Impossible");
    for(int i=1; i<=n; ++i)
    {
        if(vis[i]) continue;
        cur = i;
        dfs2(i, ++tot, 1);
    }
    printf("%d\n",tot);
    for(int i=1; i<=tot; ++i, puts(""))
    {
        printf("%d ",g[i].size());
        for(int j:g[i]) printf("%d ",j);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值