【Cf #290 C】Fox And Dinner(最大流)

如果要相邻两个数(a[i] >= 2)相加为质数,显然它们的奇偶性不同,也就是说一个圆桌(环)必须是偶环。
也就是答案的若干个环组成了一张二分图,其中以奇偶分色。
考虑每个点的度数一定为2,用最大流解决:
- 让源点向所有的奇数点连流量为2的边。
- 让所有的偶数点向汇点连流量为2的边。
- 当且仅当一组奇数和偶数相加为质数时,连一条流量为1的边。
可以证明,如果最大流小于n,那就不存在解,否则一定存在若干个边数大于2的偶环,使得所有点只出现在一个环里,最后Dfs找出环即可。
$ \bigodot $ 技巧&套路:
- 根据奇偶性或网格图黑白染色,想到建立二分图。
- 最大流的模型。
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm> const int N = , M = ; int n, s, t, tot, a[N], vis[N], lc[N], rc[N], ln, rn;
int ntp[];
std::vector<int> an[N]; void Init() {
ntp[] = ;
for (int i = ; i <= ; ++i) if (!ntp[i]) {
for (int j = i * i; j <= ; j += i) {
ntp[j] = ;
}
}
} namespace GR {
int yun = , cur[N], las[N], to[M << ], pre[M << ], fl[M << ];
int h[N], gap[N];
inline void Add(int a, int b, int c) {
to[++yun] = b; fl[yun] = c; pre[yun] = las[a]; las[a] = yun;
to[++yun] = a; fl[yun] = ; pre[yun] = las[b]; las[b] = yun;
}
int Isap(int x, int flo, int usd = ) {
if (x == t) return flo;
for (int i = cur[x]; i; i = pre[i]) if (fl[i] > && h[to[i]] + == h[x]) {
int f = Isap(to[i], std::min(flo, fl[i]));
usd += f; fl[i] -= f; fl[i ^ ] += f;
if (fl[i] > ) cur[x] = i;
if (usd == flo) return flo;
}
if (gap[h[x]] == ) h[s] = t + ;
--gap[h[x]]; ++gap[++h[x]];
cur[x] = las[x];
return usd;
}
int Max_flow(int re = ) {
memset(h, , sizeof h);
memset(gap, , sizeof gap);
for (; h[s] < t + ; ) re += Isap(s, 1e9);
return re;
}
} void Build() {
s = n + ; t = n + ;
for (int i = ; i <= n; ++i) {
if (a[i] & ) {
lc[++ln] = i; GR::Add(s, i, );
} else {
rc[++rn] = i; GR::Add(i, t, );
}
}
for (int i = ; i <= ln; ++i) {
for (int j = ; j <= rn; ++j) {
if (!ntp[a[lc[i]] + a[rc[j]]]) {
GR::Add(lc[i], rc[j], );
}
}
}
} void Dfs(int gr, int x) {
an[gr].push_back(x);
vis[x] = ;
for (int i = GR::las[x]; i; i = GR::pre[i]) {
int v = GR::to[i];
if (vis[v] || v == s || v == t) continue;
if (((~i & ) && GR::fl[i] == ) || ((i & ) && GR::fl[i] == )) {
Dfs(gr, v);
}
}
} int main() {
Init();
scanf("%d", &n);
for (int i = ; i <= n; ++i) {
scanf("%d", &a[i]);
}
Build(); int ans = GR::Max_flow();
if (ans != n) {
puts("Impossible"); return ;
}
for (int i = ; i <= n; ++i) {
if (!vis[i]) Dfs(++tot, i);
}
printf("%d\n", tot);
for (int i = ; i <= tot; ++i) {
printf("%d ", (int)an[i].size());
for (int j = ; j < (int)an[i].size(); ++j) {
printf("%d ", an[i][j]);
}
putchar('\n');
} return ;
}
【Cf #290 C】Fox And Dinner(最大流)的更多相关文章
- Solution -「CF 510E」Fox And Dinner
\(\mathcal{Description}\) Link. 给定正整数集合 \(\{a_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\), 相邻两个数一定一奇一偶,按奇偶建立二分图. ...
- [CF#290 Div.1 C]Fox And Dinner(最大流)
题目:http://codeforces.com/contest/512/problem/C 题目大意:给你若干个数,让你分成k组,每组围成一个圆,使得相邻两个数和均为素数,且每组人数应>=3个 ...
- 网络流(最大流)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 Round #290 Fox And Dinner
而是Div2的最后一题,当时打比赛的时候还不会最大流.自己能够把它写出来然后1A还是很开心的. 题意: 有n个不小于2的整数,现在要把他们分成若干个圈.在每个圈中,数字的个数不少于3个,而且相邻的两个 ...
- Fox And Dinner CodeForces - 510E (最大流)
大意: n只狐狸, 要求分成若干个环, 每个环的狐狸不少于三只, 相邻狐狸年龄和为素数. 狐狸年龄都>=2, 那么素数一定为奇数, 相邻必须是一奇一偶, 也就是一个二分图, 源点向奇数点连容量为 ...
- 【Cf #290 B】Fox And Jumping(dp,扩展gcd)
根据裴蜀定理,当且仅当选出来的集合的L[i]的gcd等于1时,才能表示任何数. 考虑普通的dp,dp[i][j]表示前i个数gcd为j的最少花费,j比较大,但状态数不多,拿个map转移就好了. $ \ ...
- 网络流 I - Fox And Dinner CodeForces - 510E
Fox Ciel is participating in a party in Prime Kingdom. There are n foxes there (include Fox Ciel). T ...
随机推荐
- alibaba/fescar 阿里巴巴 开源 分布式事务中间件
Fescar 是 阿里巴巴 开源的 分布式事务中间件,以 高效 并且对业务 0 侵入 的方式,解决 微服务 场景下面临的分布式事务问题. 示例:https://github.com/windwant/ ...
- LinuxMint 18.3禁用ipv6
编辑/etc/sysctl.conf文件,添加如下内容 net.ipv6.conf.all.disable_all = 1 保存后执行 sudo sysctl -p 即可生效
- 监控与管理-SpringBoot
在微服务架构中,我们将原本庞大的单体系统拆分成多个提供不同服务的应用. 虽然 各个应用的内部逻辑因分解而得以简化,但是由于部署应用的数量成倍增长,使得系统的 维护复杂度大大提升. 对于运维人员来说,随 ...
- Windows下LimeSDR Mini使用说明
本文内容.开发板及配件仅限用于学校或科研院所开展科研实验! 淘宝店铺名称:开源SDR实验室 LimeSDR链接:https://item.taobao.com/item.htm?spm=a230r.1 ...
- kubernetes高可用设计-CA,etcd
环境准备: master01:192.168.150.128 master02:192.168.150.130 master03:192.168.150.131 node01:192.168.150. ...
- kali获得windows的shell后乱码
输入 chcp 65001
- Trait 是什么东西
PHP官方手册里面写的内容是 自 PHP 5.4.0 起,PHP 实现了一种代码复用的方法,称为 trait. Trait 是为类似 PHP 的单继承语言而准备的一种代码复用机制.Trait 为了减少 ...
- zabbix第一篇:zabbix安装及使用
常用软件安装及使用目录 一:搭建zabbix命令集 cat /etc/redhat-release uname -r getenforce systemctl status firewalld.ser ...
- 使用Python一年多了,总结八个好用的Python爬虫技巧
用python也差不多一年多了,python应用最多的场景还是web快速开发.爬虫.自动化运维:写过简单网站.写过自动发帖脚本.写过收发邮件脚本.写过简单验证码识别脚本. 爬虫在开发过程中也有很多复用 ...
- Shell 字符串处理、获取文件名和后缀名
http://blog.csdn.net/guojin08/article/details/38704823