Codeforces Round #290 (Div. 2) E. Fox And Dinner 网络流 最大流

原创 2015年07月07日 19:50:11
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.

Sample test(s)
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个数,分成几桌,要求一桌上的数字相邻数字之和是质数,数字首尾相连。

由题意可知,每个数字大于等于2,则和为质数说明两个数一个是奇数一个是偶数,则奇数偶数的个数相等,则把奇数 偶数分成两堆,建一个二分图,起点连接所有的奇数,权值为2,终点连接所有的偶数,权值为2,奇数与偶数之和为质数,则连一条1的边,求最大流,如果最终的结果,从起点出发有权值为1的边,则说明最多只有两个人能连起来,不合题意,无解,每个奇点 偶点都有两个边相连,用dfs找出这个路径,如果大于3个连接点,则输出答案即可!这里用EK算法bfs实现求最大流!

#define INF			9000000000
#define EPS			(double)1e-9
#define mod			1000000007
#define PI			3.14159265358979
//*******************************************************************************/
#endif
#define N 205
#define M 100005
#define maxn 205
#define MOD 1000000000000000007
int n,pri[N],ansNum;
bool vis[N],prime[M];
vector<int> ans[N];
struct Edge{
    int from,to,cap,flow;
    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct EdmondsKarp{
    int n,m;
    vector<Edge> edges;//存边 边的两倍
    vector<int> G[maxn];//邻接表,图
    int a[maxn];//起点到i的可改进量
    int p[maxn];//最短路入弧号
    void init(int n){
        FI(n) G[i].clear();
        edges.clear();
    }
    void AddEdge(int from,int to,int cap){
        edges.push_back(Edge(from,to,cap,0));
        edges.push_back(Edge(to,from,0,0));//反向
        m = edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }
    int Maxflow(int s,int t){
        int flow = 0;
        for(;;){
            memset(a,0,sizeof(a));
            queue<int> Q;
            Q.push(s);
            a[s] = INF;
            while(!Q.empty()){
                int x = Q.front();Q.pop();
                FI(G[x].size()){
                    Edge & e = edges[G[x][i]];
                    if(!a[e.to]&&e.cap > e.flow){
                        p[e.to] = G[x][i];
                        a[e.to] = min(a[x],e.cap - e.flow);
                        Q.push(e.to);
                    }
                }
                if(a[t]) break;
            }
            if(!a[t]) break;
            for(int u = t;u !=s;u = edges[p[u]].from){
                edges[p[u]].flow += a[t];
                edges[p[u] ^ 1].flow -= a[t];
            }
            flow += a[t];
        }
        return flow;
    }
};
EdmondsKarp Ek;
void InitPrime(){
    memset(prime,true,sizeof(prime));
    prime[0] = prime[1] = false;
    for(int i=2;i<M;i++){
        if(prime[i]){
            for(int j=i+i;j<M;j+=i){
                prime[j] = false;
            }
        }
    }
}
void FindPath(int s,int isOdd,int ansi){
    vis[s] = true;
    ans[ansi].push_back(s);
    for(int i=0;i<Ek.G[s].size();i++){
            Edge e = Ek.edges[Ek.G[s][i]];
            if(e.to != (n+1) && e.to != (n)&& !vis[e.to] && (e.cap == isOdd)&& (abs(e.flow) == 1)){
                FindPath(e.to,isOdd ^ 1,ansi);
                return ;
            }
    }
}
int main()
{
    InitPrime();
    while(S(n)!=EOF)
    {
        FI(n){
            S(pri[i]);
        }
        Ek.init(n+2);
        FI(n){
            if(pri[i] & 1)
                Ek.AddEdge(n,i,2);
            else
                Ek.AddEdge(i,n+1,2);
            for(int j=0;j<n;j++){
                if( j != i && (pri[i] & 1) && prime[pri[i] + pri[j]])
                    Ek.AddEdge(i,j,1);
            }
        }
        Ek.Maxflow(n,n+1);
        bool flag = true;
        for(int i=0;i<Ek.G[n].size() && flag;i++){
            Edge e = Ek.edges[Ek.G[n][i]];
            if(e.cap && abs(e.flow) != 2){
                flag = false;
            }
        }
        for(int i=0;i<Ek.G[n+1].size() && flag;i++){
            Edge e = Ek.edges[Ek.G[n+1][i]];
            if(!e.cap && abs(e.flow) != 2){
                flag = false;
            }
        }
        if(flag){
            ansNum = 0;
            memset(vis,false,sizeof(vis));
            for(int i=0;i<Ek.G[n].size();i++){
                Edge e = Ek.edges[Ek.G[n][i]];
                if(!vis[e.to]){
                    ans[ansNum].clear();
                    FindPath(e.to,1,ansNum);
                    if(ans[ansNum].size()<=2){
                        ansNum = 0;
                        break;
                    }
                    ansNum++;
                }
            }
            if(ansNum == 0){
                printf("Impossible\n");
                continue;
            }
            printf("%d\n",ansNum);
            FI(ansNum){
                printf("%d",ans[i].size());
                FJ(ans[i].size()){
                    printf(" %d",ans[i][j]+1);
                }
                printf("\n");
            }
        }
        else {
            printf("Impossible\n");
        }
    }
    return 0;
}

Dinic 算法实现最大流
#define INF			9000000000
#define EPS			(double)1e-9
#define mod			1000000007
#define PI			3.14159265358979
//*******************************************************************************/
#endif
#define N 205
#define M 100005
#define maxn 205
#define MOD 1000000000000000007
int n,pri[N],ansNum;
bool vis[N],prime[M];
vector<int> ans[N];
struct Edge{
    int from,to,cap,flow;
    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct EdmondsKarp{
    int n,m;
    vector<Edge> edges;//存边 边的两倍
    vector<int> G[maxn];//邻接表,图
    int a[maxn];//起点到i的可改进量
    int p[maxn];//最短路入弧号
    void init(int n){
        FI(n) G[i].clear();
        edges.clear();
    }
    void AddEdge(int from,int to,int cap){
        edges.push_back(Edge(from,to,cap,0));
        edges.push_back(Edge(to,from,0,0));//反向
        m = edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }
    int Maxflow(int s,int t){
        int flow = 0;
        for(;;){
            memset(a,0,sizeof(a));
            queue<int> Q;
            Q.push(s);
            a[s] = INF;
            while(!Q.empty()){
                int x = Q.front();Q.pop();
                FI(G[x].size()){
                    Edge & e = edges[G[x][i]];
                    if(!a[e.to]&&e.cap > e.flow){
                        p[e.to] = G[x][i];
                        a[e.to] = min(a[x],e.cap - e.flow);
                        Q.push(e.to);
                    }
                }
                if(a[t]) break;
            }
            if(!a[t]) break;
            for(int u = t;u !=s;u = edges[p[u]].from){
                edges[p[u]].flow += a[t];
                edges[p[u] ^ 1].flow -= a[t];
            }
            flow += a[t];
        }
        return flow;
    }
};
struct Dinic{
    int n,m,s,t;
    vector<Edge> edges;//存边 边的两倍
    vector<int> G[maxn];//邻接表,图
    bool vis[maxn];//BFS使用
    int d[maxn];//起点到i的距离
    int cur[maxn];//当前弧下标
    void init(int n){
        FI(n) G[i].clear();
        edges.clear();
    }
    void AddEdge(int from,int to,int cap){
        edges.push_back(Edge(from,to,cap,0));
        edges.push_back(Edge(to,from,0,0));//反向
        m = edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }
    bool BFS(){
        memset(vis,0,sizeof(vis));
        queue<int> Q;
        Q.push(s);
        d[s] = 0;
        vis[s] = 1;
        while(!Q.empty()){
            int x = Q.front();Q.pop();
            for(int i=0;i<G[x].size();i++){
                Edge & e  = edges[G[x][i]];
                if(!vis[e.to] && e.cap > e.flow){
                    vis[e.to] = 1;
                    d[e.to] = d[x] + 1;
                    Q.push(e.to);
                }
            }
        }
        return vis[t];
    }
    int DFS(int x,int a){
        if(x == t || a== 0) return a;
        int flow = 0,f;
        for(int  i= cur[x];i<G[x].size();i++){
            Edge & e = edges[G[x][i]];
            if(d[x] + 1 == d[e.to] && ( f= DFS(e.to,min(a,e.cap - e.flow)))>0){
                e.flow += f;
                edges[G[x][i]^1].flow -= f;
                flow += f;
                a -= f;
                if( a== 0)break;
            }
        }
        return flow;
    }
    int Maxflow(int s,int t){
        this->s = s;this-> t = t;
        int flow = 0;
        while(BFS()){
            memset(cur,0,sizeof(cur));
            flow += DFS(s,INF);
        }
        return flow;
    }
};
//EdmondsKarp Ek;
Dinic Ek;
void InitPrime(){
    memset(prime,true,sizeof(prime));
    prime[0] = prime[1] = false;
    for(int i=2;i<M;i++){
        if(prime[i]){
            for(int j=i+i;j<M;j+=i){
                prime[j] = false;
            }
        }
    }
}
void FindPath(int s,int isOdd,int ansi){
    vis[s] = true;
    ans[ansi].push_back(s);
    for(int i=0;i<Ek.G[s].size();i++){
            Edge e = Ek.edges[Ek.G[s][i]];
            if(e.to != (n+1) && e.to != (n)&& !vis[e.to] && (e.cap == isOdd)&& (abs(e.flow) == 1)){
                FindPath(e.to,isOdd ^ 1,ansi);
                return ;
            }
    }
}
int main()
{
    InitPrime();
    while(S(n)!=EOF)
    {
        FI(n){
            S(pri[i]);
        }
        Ek.init(n+2);
        FI(n){
            if(pri[i] & 1)
                Ek.AddEdge(n,i,2);
            else
                Ek.AddEdge(i,n+1,2);
            for(int j=0;j<n;j++){
                if( j != i && (pri[i] & 1) && prime[pri[i] + pri[j]])
                    Ek.AddEdge(i,j,1);
            }
        }
        Ek.Maxflow(n,n+1);
        bool flag = true;
        for(int i=0;i<Ek.G[n].size() && flag;i++){
            Edge e = Ek.edges[Ek.G[n][i]];
            if(e.cap && abs(e.flow) != 2){
                flag = false;
            }
        }
        for(int i=0;i<Ek.G[n+1].size() && flag;i++){
            Edge e = Ek.edges[Ek.G[n+1][i]];
            if(!e.cap && abs(e.flow) != 2){
                flag = false;
            }
        }
        if(flag){
            ansNum = 0;
            memset(vis,false,sizeof(vis));
            for(int i=0;i<Ek.G[n].size();i++){
                Edge e = Ek.edges[Ek.G[n][i]];
                if(!vis[e.to]){
                    ans[ansNum].clear();
                    FindPath(e.to,1,ansNum);
                    if(ans[ansNum].size()<=2){
                        ansNum = 0;
                        break;
                    }
                    ansNum++;
                }
            }
            if(ansNum == 0){
                printf("Impossible\n");
                continue;
            }
            printf("%d\n",ansNum);
            FI(ansNum){
                printf("%d",ans[i].size());
                FJ(ans[i].size()){
                    printf(" %d",ans[i][j]+1);
                }
                printf("\n");
            }
        }
        else {
            printf("Impossible\n");
        }
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Codeforces 510 E Fox And Dinner【预处理素数+建图+最大流Dinic】好题

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

【网络流】 Codeforces Round #290 (Div. 1) C. Fox And Dinner

题意:给出n个数,分成一些桌,每桌大于3个人,使得每人分别与左右两人的和均为素数,并输出一种方案。。。 解法:源点向偶数连容量为2的边,奇数向汇点连容量为2的边,偶数向能构成素数的奇数连容量为1的边...

Codeforces Round #304 (Div. 2)E. Soldier and Traveling 网络流

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1442题意:n个城市,m条边, 每个点刚开始有ai个人,问最后能不能有bi...
  • yp_2013
  • yp_2013
  • 2016年02月24日 21:12
  • 156

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

B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input st...

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

思路:对相邻两行,当s1[i]≠s2[i]时,新的字母表中s1[i]必须出现在s2[i]的前面,显然是拓扑排序。所以枚举相邻的两行,构造有向边统计入度。 拓扑排序: 每次从图中拿走一个入度为0的节点,...
  • ww32zz
  • ww32zz
  • 2015年08月03日 15:28
  • 288

Codeforces Round #290 (Div. 2) - C. Fox And Names(最短路)

题目: http://codeforces.com/problemset/problem/545/E 题意: n个点m条边的无向图,求出从1->n的最短路,使得其路径上标记为0的路径最少,除了这条最...

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

题意:在n*m的地图上,每个格子涂有不同的颜色(A~Z),问是否有某一种颜色能组成一个回路且长度至少为4. 思路:我用的bfs,从一个未被访问的点开始bfs,走同一种颜色的格子,同时不能走回头路,如果...

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

题意:给n个字符串,它们按照某个字典序从小到大排列,问这个字典序是否存在,存在就输出任意一个满足条件的字典序,否则输出“Impossible”。 裸的topsort,结果在终判时挂了,就因为没有特判,...

Codeforces Round #304 (Div. 2) 546 E - Soldier and Traveling 最大流

题意:有n个城市m条路,每个城市里有A[i]个士兵,士兵可以留在自己的城市或者邻近的城市,最后是人数是否能为B[i] 思路: 入门最大流#include #include #include #in...

【Codeforces Round 375 (Div 2) E】【欧拉回路Fleury算法 或网络流】One-Way Reform 每条边定向使得最多的点满足入度=出度

E. One-Way Reform time limit per test 2 seconds memory limit per test 256 megabytes ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codeforces Round #290 (Div. 2) E. Fox And Dinner 网络流 最大流
举报原因:
原因补充:

(最多只允许输入30个字)