大意: 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. Elasticsearch-->Get Started--> Exploring Your Data

    Exploring Your Data Sample Dataset Now that we’ve gotten a glimpse of the basics, let’s try to work ...

  2. HIHOcoder 1449 后缀自动机三·重复旋律6

    思路 显然endpos的大小就对应了对应子串的出现次数,所以快速求出endpos的大小,然后用它更新对应子串长度(minlen[i]~maxlen[i])的答案即可 endpos的大小可以拓扑排序求出 ...

  3. (zhuan) Notes on Representation Learning

    this blog from: https://opendatascience.com/blog/notes-on-representation-learning-1/   Notes on Repr ...

  4. (转) AdversarialNetsPapers

      本文转自:https://github.com/zhangqianhui/AdversarialNetsPapers AdversarialNetsPapers The classical Pap ...

  5. js中获取当前浏览器类型

    本文为博主原创,转载请注明出处: 在应用POI进行导出时,先应用POI进行数据封装,将数据封装到Excel中,然后在进行download下载操作,从而完成 POI导出操作.由于在download操作时 ...

  6. Linux安装svn客户端

    Red Hat Linux 1.安装$ yum install subversion 2.常见问题1.执行svn报错:cannot set LC_CTYPE localevi /etc/profile ...

  7. 将 Graphviz .dot 文件转换为其他格式的图像

    参考: Graphviz: How to go from .dot to a graph? 将 Graphviz .dot 文件转换为其他格式的图像 在Linux系统下,使用以下命令: dot -Tp ...

  8. xxx did not match any file(s) known to git

    切换分支的时候,报了标题这么个错误,error: pathspec ''xxx did not match any file(s) known to git. 看见不能切换分支,我首先 git sta ...

  9. js全选 反选

    // 全选 反选 allChoose: function (o) { var obj = $.extend(true, { id: "#id", name: "name& ...

  10. R语言 set.seed()函数

    看了几个帖子,说得不是特别清楚,特重新描述如下: set.seed()函数是为了保证你随机生成的随机数前后一致,看效果 首先,不设置该种子函数. x=rnorm(10) plot(x)绘出的图如下: ...