大意: n只狐狸, 要求分成若干个环, 每个环的狐狸不少于三只, 相邻狐狸年龄和为素数.

狐狸年龄都>=2, 那么素数一定为奇数, 相邻必须是一奇一偶, 也就是一个二分图, 源点向奇数点连容量为2的边, 偶数点向汇点连容量为2的边, 和为偶数的奇数点向偶数点连容量为1的边, 看能否满流即可.

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head const int N = 1e5+10;
int n, m, S, T, cnt;
struct _ {
int from,to,w;
void pr() {
printf("fro=%d,to=%d,w=%d\n",from,to,w);
}
};
vector<_> E;
vector<int> g[N];
int a[N], pre[N], vis[N], d[N]; void add(int x, int y, int w) {
g[x].pb(E.size());
E.pb({x,y,w});
g[y].pb(E.size());
E.pb({y,x,0});
} void seive(int n) {
int mx = sqrt(n+0.5);
vis[1] = 1;
REP(i,2,mx) if (!vis[i]) {
for (int j=i*i; j<=n; j+=i) vis[j]=1;
}
}
void build() {
S = n+1, T = n+2;
REP(i,1,n) {
if (a[i]&1) add(S,i,2);
else add(i,T,2);
}
REP(i,1,n) if (a[i]&1) {
REP(j,1,n) if (!vis[a[i]+a[j]]) add(i,j,1);
}
} int Maxflow(int s, int t) {
int ans = 0;
for (; ; ) {
REP(i,1,n+2) d[i] = 0;
queue<int> q;
q.push(s);
d[s] = INF;
while (!q.empty()) {
int x = q.front();q.pop();
for (auto &t:g[x]) {
auto &e = E[t];
if (!d[e.to]&&e.w>0) {
pre[e.to] = t;
d[e.to] = min(d[x], e.w);
q.push(e.to);
}
}
if (d[t]) break;
}
if (!d[t]) break;
for (int u=t; u!=s; u=E[pre[u]].from) {
E[pre[u]].w -= d[t];
E[pre[u]^1].w += d[t];
}
ans += d[t];
}
return ans;
} vector<int> gg[N];
vector<int> ans[N]; void dfs(int x) {
if (vis[x]) return;
vis[x] = 1;
ans[cnt].pb(x);
for (int y:gg[x]) dfs(y);
} int main() {
scanf("%d", &n);
REP(i,1,n) scanf("%d", a+i);
seive(20000);
build();
if (Maxflow(S,T)!=n) return puts("Impossible"),0;
REP(i,1,n) if (a[i]&1^1) {
for (auto &&j:g[i]) if (E[j].w==1) {
gg[E[j].from].pb(E[j].to);
gg[E[j].to].pb(E[j].from);
}
}
memset(vis,0,sizeof vis);
REP(i,1,n) if (!vis[i]) ++cnt,dfs(i);
printf("%d\n", cnt);
REP(i,1,cnt) {
printf("%d ",int(ans[i].size()));
for (auto &&t:ans[i]) printf("%d ",t);hr;
}
}

Fox And Dinner CodeForces - 510E (最大流)的更多相关文章

  1. 网络流 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 ...

  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 ...

  3. CF510E. Fox And Dinner

    CF510E. Fox And Dinner https://codeforces.com/contest/510 分析: 由于\(a_i>2\), 相邻两个数一定一奇一偶,按奇偶建立二分图. ...

  4. 网络流(最大流)CodeForces 512C:Fox And Dinner

    Fox Ciel is participating in a party in Prime Kingdom. There are n foxes there (include Fox Ciel). T ...

  5. CodeForces 510E Fox And Dinner

    网络流. 原点到偶数连边,容量为2, 奇数到汇点连边,容量为2, 偶数到与之能凑成素数的奇数连边,容量为1 如果奇数个数不等于偶数个数,输出不可能 如果原点到偶数的边不满流,输出不可能 剩下的情况有解 ...

  6. codeforces 510E. Fox And Dinner 网络流

    题目链接 给出n个人, 以及每个人的值, 要求他们坐在一些桌子上面, 每个桌子如果有人坐, 就必须做3个人以上. 并且相邻的两个人的值加起来必须是素数.每个人的值都>=2. 由大于等于2这个条件 ...

  7. [CF#290 Div.1 C]Fox And Dinner(最大流)

    题目:http://codeforces.com/contest/512/problem/C 题目大意:给你若干个数,让你分成k组,每组围成一个圆,使得相邻两个数和均为素数,且每组人数应>=3个 ...

  8. Codeforces 510 E. Fox And Dinner

    题目链接:http://codeforces.com/problemset/problem/510/E 乍一看和那啥魔术球问题有点神似啊/XD 其实是不一样的. 解决这道问题的关键在于发现若是相邻的两 ...

  9. CodeForces Round #290 Fox And Dinner

    而是Div2的最后一题,当时打比赛的时候还不会最大流.自己能够把它写出来然后1A还是很开心的. 题意: 有n个不小于2的整数,现在要把他们分成若干个圈.在每个圈中,数字的个数不少于3个,而且相邻的两个 ...

随机推荐

  1. 【做题】CF388D. Fox and Perfect Sets——线性基&数位dp

    原文链接https://www.cnblogs.com/cly-none/p/9711279.html 题意:求有多少个非空集合\(S \subset N\)满足,\(\forall a,b \in ...

  2. sql -- 移除数据中的换行符和回车符

    https://blog.csdn.net/jcx5083761/article/details/40185795 --移除回车符 update master_location SET street_ ...

  3. %lld 和 %I64d的区别

    参考一个博客的链接:https://blog.csdn.net/thunders01/article/details/38879553

  4. Vim的一些使用

    Vim的三种模式 normal(普通模式) insert(插入模式) command(命令模式) Vim的工作方式不同于常规的编辑器,在常规编辑器下对应到Vim中就是一直使用insert模式进行操作, ...

  5. Js批量下载花瓣网及堆糖网专辑图片

    插件作者:SaintIC 文章地址:https://blog.saintic.com/blog/256.html 一.安装 1. 安装Tampermonkey扩展,不同浏览器的支持,参见官网:http ...

  6. Sublime Text3 插件:DocBlockr与javascript注释规范

    原:http://www.ithao123.cn/content-719950.html 1.引子 在写代码的时候,尤其是写脚本,最需要注释了.目前脚本.样式的注释格式都有一个已经成文的约定规范(这些 ...

  7. SVN的常用功能使用教程

    (一)导入项目到版本库中 1. 在SVN服务器的仓库中新建项目名称文件夹 2. 选择安装Visual SVN的本地计算机中的一个文件夹,右键选择导入,将本地项目导入到SVN服务中央仓库中 3. 输入在 ...

  8. _itemmod_creation_enchant

    该表控制物品产生时自动获得随机附魔效果 comment  备注  entry  物品entry  slot  附魔位置1-5,这些位置都可以用来产生自带附魔效果  groupId  附魔组Id 对就_ ...

  9. NYOJ 1277Decimal integer conversion (第九届河南省省赛)

    XiaoMing likes mathematics, and heis just learning how to convert numbers between different bases , ...

  10. python web.py操作mysql数据库,实现对数据库的增删改查操作

    使用web.py框架,实现对mysql数据库的增删改查操作: 该示例代码中连接的是本地数据库testdb,user表,表结构比较简单,只有两个字段:mobile和passwd,类型均为字符型 实际应用 ...