大意: 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. (转)mblog解读(二)

    (二期)12.开源博客项目mblog解读(二) [课程12]freema...模板.xmind77.9KB [课程12]hibernat...arch.xmind0.1MB freemarker模板技 ...

  2. dajie项目的坑

    1.首先IDEA巨坑无比的地方是引入时,只要哪怕一个依赖下载不到,就会长期阻塞,删除.重新引入都没用!! 2.注释掉项目及其子项目中所有pom.xml中引用的spring仓库,否则即使maven配置阿 ...

  3. Why database migrations?

    https://flywaydb.org/getstarted/why First, let's start from the beginning and assume we have a proje ...

  4. oracle 之 伪列 rownum 和 rowid的用法与区别

    rownum的用法 select  rownum,empno,ename,job from emp where rownum<6 可以得到小于6的值数据 select rownum,empno, ...

  5. [JavaScript] - replaceAll,将字符串中的字母或数字等全部替换掉的方式

    原题 function DNAtoRNA(dna) { // create a function which returns an RNA sequence from the given DNA se ...

  6. 《操作系统_FCFS和SJF》

    先来先服务FCFS和短作业优先SJF进程调度 转自:https://blog.csdn.net/qq_34374664/article/details/73231072 一.概念介绍和案例解析 FCF ...

  7. facebook api call——error

    Error Codes send-api error-codes whatsapp api errors marketing-api error-reference graph-api/using-g ...

  8. GTK 菜单的创建详解

    1 定义 1.1 菜单由菜单条和菜单项组成,它们的定义如下所示: 菜单项(GtkMenuItem):添加到菜单条或下拉菜单中构件 顶层菜单项:添加到菜单条上的菜单项称为顶层菜单项 下拉菜单(GtkMe ...

  9. 1、My Scripts

    1.写一个包含命令.变量和流程控制的语句来清除/var/log的messages日志文件的shell脚本.(P26)(11-21) 2.利用$0和(dirname.basename)取出当前路径的目录 ...

  10. RN中关于IOS和Android的相关权限的问题

    在日常的开发中,时常需要去获取应用的一权限 比如查看通讯录/打开摄像机等 1:ios  iOS 的权限管理在info.plist里设置 info.plist主要是管理了app 的一些信息文件,比如版本 ...