Fox And Dinner CodeForces - 510E (最大流)
大意: 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 (最大流)的更多相关文章
- 网络流 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 ...
- 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 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 510E. Fox And Dinner 网络流
题目链接 给出n个人, 以及每个人的值, 要求他们坐在一些桌子上面, 每个桌子如果有人坐, 就必须做3个人以上. 并且相邻的两个人的值加起来必须是素数.每个人的值都>=2. 由大于等于2这个条件 ...
- [CF#290 Div.1 C]Fox And Dinner(最大流)
题目:http://codeforces.com/contest/512/problem/C 题目大意:给你若干个数,让你分成k组,每组围成一个圆,使得相邻两个数和均为素数,且每组人数应>=3个 ...
- Codeforces 510 E. Fox And Dinner
题目链接:http://codeforces.com/problemset/problem/510/E 乍一看和那啥魔术球问题有点神似啊/XD 其实是不一样的. 解决这道问题的关键在于发现若是相邻的两 ...
- CodeForces Round #290 Fox And Dinner
而是Div2的最后一题,当时打比赛的时候还不会最大流.自己能够把它写出来然后1A还是很开心的. 题意: 有n个不小于2的整数,现在要把他们分成若干个圈.在每个圈中,数字的个数不少于3个,而且相邻的两个 ...
随机推荐
- sonarqube中new issue的标准
https://docs.sonarqube.org/latest/user-guide/issues/#header-4 Understanding which Issues are "N ...
- Elasticsearch 异常处理
cluster_block_exception https://stackoverflow.com/questions/50609417/elasticsearch-error-cluster-blo ...
- Ubuntu fcitx CPU占用率很高解决方法
在Ubuntu中,有时候电脑的风扇突然狂装,用 pidstat -u 5 1 命令查看后台应用的资源占用情况,发现fcitx的占用率接近百分之百. 原因是搜狗云输入的问题,关闭后,在用kill命令干掉 ...
- (转载)WinformGDI+入门级实例——扫雷游戏(附源码)
本文将作为一个入门级的.结合源码的文章,旨在为刚刚接触GDI+编程或对相关知识感兴趣的读者做一个入门讲解.游戏尚且未完善,但基本功能都有,完整源码在文章结尾的附件中. 整体思路: 扫雷的游戏界面让我从 ...
- React入门实例:组件化+react-redux实现网上商城(1)
项目运行 1.git clone https://github.com/soybeanxiaobi/React_demo_onlineShop 2.cd React_demo_onlineShop(文 ...
- Intellij idea注册码失效
从网上下载idea需要输入激活码,晚上用的激活码大多是同一个,但是上次使用的时候突然弹窗告诉我注册码失效了,在网上找到一个新的方法 在注册界面有几个选项,我们常用的是Activation Code,现 ...
- linux du
命令参数: -a或-all 显示目录中个别文件的大小. -b或-bytes 显示目录或文件大小时,以byte为单位. -c或--total 除了显示个别目录或文件的大小外,同时也显示所有目录或文 ...
- CentOS6.X、7.X下Jenkins的安装及使用
一.相关概念 1.1 Jenkins概念: Jenkins是一个功能强大的应用程序,允许持续集成和持续交付项目,无论用的是什么平台.这是一个免费的源代码,可以处理任何类型的构建或持续集成.集成Jenk ...
- java 之2D过气游戏类的写法
2D游戏中各对象的父类 package cn.littlepage.game; import java.awt.Graphics; import java.awt.Image; import java ...
- 《剑指offer》第六十四题(求1+2+…+n)
// 面试题64:求1+2+…+n // 题目:求1+2+…+n,要求不能使用乘除法.for.while.if.else.switch.case // 等关键字及条件判断语句(A?B:C). #inc ...