网络流 I - Fox And Dinner CodeForces - 510E
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:
- Each fox is sitting at some table.
- Each table has at least 3 foxes sitting around it.
- 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
4
3 4 8 9
1
4 1 2 4 3
5
2 2 2 2 2
Impossible
12
2 3 4 5 6 7 8 9 10 11 12 13
1
12 1 2 3 6 5 12 9 8 7 10 11 4
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
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.
奇数和偶数如果加和得到一个质数,那么奇数和这个偶数相连,容量为1.
为什么要这么建图呢?因为如果奇数和源点的容量应该是2,如果满流就代表着这个奇数连到了两个偶数而且奇数偶数之和为一个素数,
这个样子就找到了这个奇数左右两边相邻的数,这个样子同时满足了第一第二两个条件。
显而易见如果要满足上面的建图条件,则奇数和偶数一定要相同,因为奇数和源点容量是2,偶数和汇点容量也是2(因为一个偶数两边应该也要两个奇数,奇偶加和为一个素数)
所以这个最大流就应该==n。如果不满足就输出impossible
最后就是路径的输出了,这个用用数组来存,把连了的边存到一个数组里面,用vis进行标记来确定不会连重复的边。
然后就是一个递归来找这个桌子的数量,如果一条边它的容量为1则说明这个被经过,就可以加上这条边。
值得注意的是,这些奇数偶数肯定会形成一个环,所以不用担心可能会导致存在两个相邻的数不满足条件,
这个不确定的话可以自己模拟一下。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 1e5 + ;
const int INF = 0x3f3f3f3f;
struct edge
{
int u, v, c, f;
edge(int u, int v, int c, int f) :u(u), v(v), c(c), f(f) {}
};
vector<edge>e;
vector<int>G[maxn];
int level[maxn];//BFS分层,表示每个点的层数
int iter[maxn];//当前弧优化
int m;
void init(int n)
{
for (int i = ; i <= n; i++)G[i].clear();
e.clear();
}
void add(int u, int v, int c)
{
e.push_back(edge(u, v, c, ));
e.push_back(edge(v, u, , ));
m = e.size();
G[u].push_back(m - );
G[v].push_back(m - );
}
void BFS(int s)//预处理出level数组
//直接BFS到每个点
{
memset(level, -, sizeof(level));
queue<int>q;
level[s] = ;
q.push(s);
while (!q.empty())
{
int u = q.front();
q.pop();
for (int v = ; v < G[u].size(); v++)
{
edge& now = e[G[u][v]];
if (now.c > now.f && level[now.v] < )
{
level[now.v] = level[u] + ;
q.push(now.v);
}
}
}
}
int dfs(int u, int t, int f)//DFS寻找增广路
{
if (u == t)return f;//已经到达源点,返回流量f
for (int &v = iter[u]; v < G[u].size(); v++)
//这里用iter数组表示每个点目前的弧,这是为了防止在一次寻找增广路的时候,对一些边多次遍历
//在每次找增广路的时候,数组要清空
{
edge &now = e[G[u][v]];
if (now.c - now.f > && level[u] < level[now.v])
//now.c - now.f > 0表示这条路还未满
//level[u] < level[now.v]表示这条路是最短路,一定到达下一层,这就是Dinic算法的思想
{
int d = dfs(now.v, t, min(f, now.c - now.f));
if (d > )
{
now.f += d;//正向边流量加d
e[G[u][v] ^ ].f -= d;
//反向边减d,此处在存储边的时候两条反向边可以通过^操作直接找到
return d;
}
}
}
return ;
}
int Maxflow(int s, int t)
{
int flow = ;
for (;;)
{
BFS(s);
if (level[t] < )return flow;//残余网络中到达不了t,增广路不存在
memset(iter, , sizeof(iter));//清空当前弧数组
int f;//记录增广路的可增加的流量
while ((f = dfs(s, t, INF)) > )
{
flow += f;
}
}
return flow;
}
int p[maxn];
void init()
{
memset(p, , sizeof(p));
for (int i = ; i < maxn; i++) p[i] = ;
for(ll i=;i*i<maxn;i++)
{
if(p[i])
{
for(ll j=i*i;j<maxn;j+=i)
{
p[j] = ;
}
}
}
}
int a[maxn], cnt, out[maxn];
vector<int>vec[maxn];
bool vis[maxn]; void solve(int u)
{
out[++cnt] = u;
for(int i=;i<vec[u].size();i++)
{
int v = vec[u][i];
if (vis[v]) continue;
vis[v] = ;
solve(v);
}
} int main()
{
init();
int n;
scanf("%d", &n);
int s = , t = n + ;
for(int i=;i<=n;i++)
{
scanf("%d", &a[i]);
if (a[i] & ) add(s, i, );
else add(i, t, );
}
for(int i=;i<=n;i++)
{
if(a[i]&)
{
for(int j=;j<=n;j++)
{
if (i == j) continue;
if (p[a[i] + a[j]]) add(i, j, );
}
}
}
int ans = Maxflow(s, t);
if(ans!=n)
{
printf("Impossible\n");
return ;
}
for(int i=;i<=n;i++)
{
if(a[i]&)
{
for(int j=;j<G[i].size();j++)
{
edge now = e[G[i][j]];
if(now.v<t&&now.v>s&&now.u<t&&now.u>s&&now.f==)
{
vec[now.u].push_back(now.v);
vec[now.v].push_back(now.u);
}
}
}
}
int count = ;
memset(vis, , sizeof(vis));
for(int i=;i<=n;i++)
{
cnt = ;
if(!vis[i])
{
count++;
vis[i] = ;
solve(i);
}
}
printf("%d\n", count);
memset(vis, , sizeof(vis));
for(int i=;i<=n;i++)
{
cnt = ;
if (vis[i]) continue;
vis[i] = ;
solve(i); printf("%d ", cnt);
for (int j = ; j < cnt; j++) printf("%d ", out[j]);
printf("%d\n", out[cnt]);
}
return ;
}
网络流 I - Fox And Dinner CodeForces - 510E的更多相关文章
- Fox And Dinner CodeForces - 510E (最大流)
大意: n只狐狸, 要求分成若干个环, 每个环的狐狸不少于三只, 相邻狐狸年龄和为素数. 狐狸年龄都>=2, 那么素数一定为奇数, 相邻必须是一奇一偶, 也就是一个二分图, 源点向奇数点连容量为 ...
- 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 ...
- CF510E. Fox And Dinner
CF510E. Fox And Dinner https://codeforces.com/contest/510 分析: 由于\(a_i>2\), 相邻两个数一定一奇一偶,按奇偶建立二分图. ...
- codeforces 510E. Fox And Dinner 网络流
题目链接 给出n个人, 以及每个人的值, 要求他们坐在一些桌子上面, 每个桌子如果有人坐, 就必须做3个人以上. 并且相邻的两个人的值加起来必须是素数.每个人的值都>=2. 由大于等于2这个条件 ...
- 网络流(最大流)CodeForces 512C:Fox And Dinner
Fox Ciel is participating in a party in Prime Kingdom. There are n foxes there (include Fox Ciel). T ...
- CodeForces 510E Fox And Dinner
网络流. 原点到偶数连边,容量为2, 奇数到汇点连边,容量为2, 偶数到与之能凑成素数的奇数连边,容量为1 如果奇数个数不等于偶数个数,输出不可能 如果原点到偶数的边不满流,输出不可能 剩下的情况有解 ...
- Codeforces 510 E. Fox And Dinner
题目链接:http://codeforces.com/problemset/problem/510/E 乍一看和那啥魔术球问题有点神似啊/XD 其实是不一样的. 解决这道问题的关键在于发现若是相邻的两 ...
- 【Codeforces】512C Fox and Dinner
[解析]欧拉筛法,奇偶分析.建二分图,网络流 [Analysis] http://blog.csdn.net/qq574857122/article/details/43453087. 所谓的连通块就 ...
- CodeForces Round #290 Fox And Dinner
而是Div2的最后一题,当时打比赛的时候还不会最大流.自己能够把它写出来然后1A还是很开心的. 题意: 有n个不小于2的整数,现在要把他们分成若干个圈.在每个圈中,数字的个数不少于3个,而且相邻的两个 ...
随机推荐
- 选择IT行业的自我心得,希望能帮助到各位!(四)
俗话说,只有尝过人生的苦,吃过人生的亏,你才能吃一见长一智,人生教会了我们该如何去吃亏,该如何做人,该如何和人打交道,生活会让我们低下无数的头,就看你怎么去面对这些曲折该如何告诉自己不能就被这样打到, ...
- python这门语言为什么要起这个名字
我只是一只可爱的小虫 前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:Liz喵 PS:如有需要Python学习资料的小 ...
- stand up meeting 1/7/2016
part 组员 今日工作 工作耗时/h 明日计划 工作耗时/h UI 冯晓云 调研下滑条的存在问题,尝试替换方案 6 全面实行替换 ...
- MDC是什么鬼?用法、源码一锅端
近期用到阿里的一款开源的数据同步工具 Canal,不经意之中看到了 MDC 的用法,而且平时项目中也多次用到 MDC,趁机科普一把. 通过今天的分享,能让你轻松 get 如下几点,绝对收获满满. a) ...
- Jmeter系列(3)- Jmeter安装目录介绍
如果你想从头学习Jmeter,可以看看这个系列的文章哦 https://www.cnblogs.com/poloyy/category/1746599.html Jmeter安装目录说明 bin:包含 ...
- 最通俗易懂的Redis发布订阅及代码实战
发布订阅简介 除了使用List实现简单的消息队列功能以外,Redis还提供了发布订阅的消息机制.在这种机制下,消息发布者向指定频道(channel)发布消息,消息订阅者可以收到指定频道的消息,同一个频 ...
- 【已解决】error setting certificate verify locations报错
目录 1.问题描述 2.问题分析 3.解决方法 1.问题描述 在公司的电脑上从Github上clone项目的时候git黑窗口报错"error setting certificate veri ...
- nginx history路由模式时,页面返回404重定向index.html
1.路由默认是带#的,有时我们感觉不美观,就使其变为history模式,也就没有#字符 2.# 如果找不到当前页面(404),就返回index.html,重新分配路由 location ^~/prod ...
- BUAA_OO 第二单元总结
作业分析 第一次作业 本次作业是单次可捎带电梯的设计,主要是初步了解多线程的设计实现和测试,本身算法设计非常简单.这次作业整体来说不是很难,是多线程的入门,主要目的就是让我们认识,了解一下什么是多线程 ...
- Unity 芯片拼图算法
很多游戏的养成系统中会有利用芯片或者碎片来合成特定道具的功能,或者来给玩家以额外的属性提升等,先截个图以便更好说明: 如上图,我们有各种各样形状迥异的碎片,上面只不过列举了其中一部分,现在,我们需要利 ...