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个,而且相邻的两个 ...
随机推荐
- 从0开始安装fedora23的笔记-- 以及使用fedora的常规问题
关于安装过程中的笔记本 触摸板 和鼠标的问题? 在安装过程中, 系统只认从usb接口插入的鼠标和触摸板上的 "左键和右键"按钮. 触摸板上的鼠标只支持"移动"的 ...
- 题解——P1133 教主的花园DP
直接设二维状态具有后效性,会爆零 然后需要加一维 然后70 看了题解之后发现没有考虑1和n的关系 考虑之后,四十 然后懵逼 突然发现自己的ans更新写错了,导致每次ans都是第一个取30的情况的解 然 ...
- Keepalived笔记
Keepalived高可用服务对之间的故障切换转移,是通过 VRRP (Virtual Router Redundancy Protocol ,虚拟路由器冗余协议)来实现的. VRRP的出现是为了解决 ...
- 《算法竞赛入门经典》刘汝佳 C语言部分(前四章)“注解与习题” 之思索 -<1>
此书我购于去年的十一月份,也是经前人推荐购买的一本比较有用的书籍,在寒假自学此书,其简洁清晰高效的示例代码令我印象深刻,于是我打算把这本书的前四章后面的注解与习题(未给出标准解答)认真的去思索和研究, ...
- RN酷炫组件圆形加载
地址:https://js.coach/react-native/react-native-circular-progress?search=react-native 别谢我 点个赞就行 ## Use ...
- python redis 操作
1.String 操作 redis中的String在在内存中按照一个name对应一个value来存储 set() #在Redis中设置值,默认不存在则创建,存在则修改 r.set('name', 'z ...
- eclipse配置tomcat后出现:java virtual machine launcher Error:Could not ……,Program will exit.
原贴在stack overflow上:https://stackoverflow.com/questions/50085750/apache-tomcat-9-x-not-working-with-e ...
- nodejs + ts 配置
参考:https://github.com/nestjs/typescript-starter 和 How to get auto restart and breakpoint support wit ...
- json文件不能有注释
之前一直忽视了这个问题,直接导致taiga-front的部署的时候不能通过其他IP访问. 如图: 首先是提示 app-loader.js:1 Your conf.json file is not a ...
- TypeError: atlas.getSpriteFrame is not a function
1.资源结构如下: 2.在使用cc.loader.loadRes动态异步加载cc.SpriteAtlas资源时出现这个错误,代码如下: var self = this; var url = " ...