题目链接:http://codeforces.com/problemset/problem/510/E


乍一看和那啥魔术球问题有点神似啊/XD

其实是不一样的。

解决这道问题的关键在于发现若是相邻的两个数的和是质数,那么他们必定是一奇一偶!

而且一个奇数旁边一定是两个偶数,一个偶数旁边一定是两个奇数(因为是一个大于$3$的环)

秒啊!这就变成了匹配模型。

源点向所有值为奇数的点连容量为$2$边,值为奇数的点向所有与它的和为质数的且值为偶数点连容量为$1$边,所有值为偶数的点向汇点连容量为$2$的边。

跑一边最大流,我们是不是就求出了最大的合法的匹配方案?

如果最大流为$n$才说明了每一个点都匹配上了。

考虑输出方案,$DFS$在$Dinic$之后留下的图,如果流量发生了变化则沿这条路径走下去(当然不能走到源点或者汇点)


 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
#define maxn 1010
#define inf 0x7fffffff
#define llg int
#define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
llg n,m,ans,c[maxn],cnt1,cnt2,tot[maxn];
llg d[maxn][maxn];
inline int getint()
{
int w=,q=; char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
} struct DINIC
{
llg N;
vector<llg>a[maxn],ba[maxn],val[maxn],stval[maxn];
llg dl[maxn],deep[maxn];
bool bj[maxn]; void link(llg x,llg y,llg z)
{
a[x].push_back(y),val[x].push_back(z); stval[x].push_back(z);
a[y].push_back(x),val[y].push_back(); stval[y].push_back();
ba[x].push_back(a[y].size()-),ba[y].push_back(a[x].size()-);
} llg dfs(llg x,llg low)
{
llg va=,inc=;
if (x==N) return low;
llg w=a[x].size(),v;
for (llg i=;i<w;i++)
{
v=a[x][i];
if (deep[x]+==deep[v] && val[x][i]> && (va=dfs(v,min(low,val[x][i]))))
{
val[x][i]-=va; val[v][ba[x][i]]+=va; inc+=va;
return va;
}
}
if (!inc) deep[x]=-;
return ;
} void fencen()
{
llg x,w,v; deep[]=;
memset(bj,,sizeof(bj));
llg head=,tail=; dl[]=; bj[]=;
do
{
x=dl[++head]; w=a[x].size();
for (llg i=;i<w;i++)
{
v=a[x][i];
if (bj[v] || val[x][i]<=) continue;
dl[++tail]=v;
deep[v]=deep[x]+; bj[v]=;
}
}while (head!=tail);
} llg dinic()
{
llg ans=;
while ()
{
fencen();
if (!bj[N]) break;
ans+=dfs(,inf);
}
return ans;
} void DFS(llg x)
{
bj[x]=;
tot[ans]++;
d[ans][tot[ans]]=x;
llg w=a[x].size(),v;
for (llg i=;i<w;i++)
{
v=a[x][i];
if (v== || v==N || bj[v]) continue;
if (stval[x][i]==val[x][i]) continue;
DFS(v);
}
} bool pd(llg x)
{
llg cs=,w=a[x].size(),v;
for (llg i=;i<w;i++)
{
v=a[x][i];
if (v== || v==N) continue;
if (bj[v] || val[x][i]==stval[x][i]) continue;
cs++;
}
if (cs>) return ;else return ;
} void make_ans()
{
memset(bj,,sizeof(bj));
while ()
{
bool WK=;
for (llg i=;i<=n;i++)
if (!bj[i])
{
if (!pd(i)) continue;
WK=;
ans++;
DFS(i);
}
if (!WK) break;
}
}
}G; bool check(llg v1,llg v2)
{
llg x=v1+v2;
llg up=sqrt(x+);
for (llg i=;i<=up;i++)
if (x%i==) return ;
return ;
} void init()
{
cin>>n;
for (llg i=;i<=n;i++) c[i]=getint();
for (llg i=;i<=n;i++)
{
if (c[i]%) G.link(,i,),cnt1++; else G.link(i,n+,),cnt2++;
if (c[i]%==) continue;
for (llg j=;j<=n;j++)
if (i!=j && check(c[i],c[j]))
G.link(i,j,);
}
G.N=n+;
} int main()
{
yyj("flows");
init();
if (cnt1!=cnt2) {cout<<"Impossible"; return ;}
llg flows=G.dinic();
if (flows!=n) {cout<<"Impossible"; return ;}
G.make_ans();
cout<<ans<<endl;
for (llg i=;i<=ans;i++)
{
cout<<tot[i]<<" ";
for (llg j=;j<=tot[i];j++) cout<<d[i][j]<<" ";
cout<<endl;
}
return ;
}

Codeforces 510 E. Fox And Dinner的更多相关文章

  1. Codeforces 510 A.Fox and Snake

    题目链接:http://codeforces.com/contest/510/problem/A A. Fox And Snake time limit per test2 seconds memor ...

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

  3. codeforces 510 C Fox And Names【拓扑排序】

    题意:给出n串名字,表示字典序从小到大,求符合这样的字符串排列的字典序 先挨个地遍历字符串,遇到不相同的时候,加边,记录相应的入度 然后就是bfs的过程,如果某一点没有被访问过,且入度为0,则把它加入 ...

  4. CodeForces Round #290 Fox And Dinner

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

  5. 【Codeforces】512C Fox and Dinner

    [解析]欧拉筛法,奇偶分析.建二分图,网络流 [Analysis] http://blog.csdn.net/qq574857122/article/details/43453087. 所谓的连通块就 ...

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

  7. CF510E. Fox And Dinner

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

  8. (CodeForces 510C) Fox And Names 拓扑排序

    题目链接:http://codeforces.com/problemset/problem/510/C Fox Ciel is going to publish a paper on FOCS (Fo ...

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

随机推荐

  1. Qt读取TXT文件时,GBK与UTF-8编码判断

    读取txt文件时,很多时候无法获取文件的编码格式.如果直接进行使用,则有可能出现乱码.需要在使用前将其转为Unicode(Qt的默认编码格式). 虽然实际的编码格式种类非常多,但平常主要使用的有GBK ...

  2. bzoj2287 [POJ Challenge]消失之物

    题目链接 少打个else 调半天QAQ 重点在47行,比较妙 #include<algorithm> #include<iostream> #include<cstdli ...

  3. php yii2 使用命令行模式开启脚本 报错 :Error while sending QUERY packet. PID=xxx

    背景:使用Yii2命令行模式开启脚本监控rabbitmq队列(或使用nohup &命令后台监控接口),当队列有订单信息,执行查询,更新操作(相当于PHP文件写个查询,更新,使用命令行启动) 问 ...

  4. mysql union 和 left join 结合查询用法

    union 和 left join 结合查询用法 SELECT u.nickname,z.group_comming_type,z.id,z.user_id,z.title,z.create_time ...

  5. python类的组合

    类的组合,即在类实例化时,将另一个类的实例作为参数传入,这样可以将两个实例关联起来. 当类之间有显著不同,并且较小的类是较大的类所需要的组件时,用组合比较好. 例如,描述一个机器人类,这个大类是由很多 ...

  6. c++ linux下输出中文

    同样,使用的是VS FOR LINUX进行测试. converting to execution character set: Invalid or incomplete multibyte or w ...

  7. 猜字母游戏(Java)

    我的代码: package day20181025; import java.util.Arrays; import java.util.Scanner; /** * 猜字母 * @author Ad ...

  8. Kali linux 2018安装后全屏乱码解决

    安装的时候选择了中文, 后来安装成功后成了全部乱码的. 原因是,系统没有中文字体显示安装包, 下载一个 sudo apt-get install ttf-wqy-zenhei 重启解决!

  9. 浅谈 DDoS 攻击与防御

    浅谈 DDoS 攻击与防御 原创: iMike 运维之美  什么是 DDoS DDoS 是英文 Distributed Denial of Service 的缩写,中文译作分布式拒绝服务.那什么又是拒 ...

  10. ldap集成zabbix

    zabbix版本:3.0.7 ldap认证配置: zabbix管理员登录-->管理-->认证,选择ldap方式 参照以上格式填写,需注意配置完成后需在zabbix上创建与ldap同名账户才 ...