ZYB's Prime

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5594

Description

After getting 600 scores in NOIP,ZYB(ZJ−267) creates a problem:you are given N numbers,now you are asked to divide them into K groups(K≥1),the
number of each group must be no less than 3,and put all the numbers in a group into a ring,the sum of every two adjacent numbers must be a prime.ZYB want to ask
you whether the N numbers can be divided or not?

Input

In the first line there is the testcase T.

For each teatcase:

In the first line there is one number N.
In the next line there are N numbers Ai.

1≤T≤50,1≤N≤200,1≤Ai≤200,for 60% cases N≤20.

Output

For each testcase,print the YES or NO.

Sample Input

2
7
3 4 8 9 1 1 1
3
1 2 3

Sample Output

YES
NO

HINT

题意

题解:

代码:

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<vector>
  4. #include<cstring>
  5. using namespace std;
  6.  
  7. namespace NetFlow
  8. {
  9. const int MAXN=,MAXM=,inf=1e9;
  10. struct Edge
  11. {
  12. int v,c,f,nx;
  13. Edge() {}
  14. Edge(int v,int c,int f,int nx):v(v),c(c),f(f),nx(nx) {}
  15. } E[MAXM];
  16. int G[MAXN],cur[MAXN],pre[MAXN],dis[MAXN],gap[MAXN],N,sz;
  17. void init(int _n)
  18. {
  19. N=_n,sz=; memset(G,-,sizeof(G[])*N);
  20. }
  21. void link(int u,int v,int c)
  22. {
  23. E[sz]=Edge(v,c,,G[u]); G[u]=sz++;
  24. E[sz]=Edge(u,,,G[v]); G[v]=sz++;
  25. }
  26. int ISAP(int S,int T)
  27. {//S -> T
  28. int maxflow=,aug=inf,flag=false,u,v;
  29. for (int i=;i<N;++i)cur[i]=G[i],gap[i]=dis[i]=;
  30. for (gap[S]=N,u=pre[S]=S;dis[S]<N;flag=false)
  31. {
  32. for (int &it=cur[u];~it;it=E[it].nx)
  33. {
  34. if (E[it].c>E[it].f&&dis[u]==dis[v=E[it].v]+)
  35. {
  36. if (aug>E[it].c-E[it].f) aug=E[it].c-E[it].f;
  37. pre[v]=u,u=v; flag=true;
  38. if (u==T)
  39. {
  40. for (maxflow+=aug;u!=S;)
  41. {
  42. E[cur[u=pre[u]]].f+=aug;
  43. E[cur[u]^].f-=aug;
  44. }
  45. aug=inf;
  46. }
  47. break;
  48. }
  49. }
  50. if (flag) continue;
  51. int mx=N;
  52. for (int it=G[u];~it;it=E[it].nx)
  53. {
  54. if (E[it].c>E[it].f&&dis[E[it].v]<mx)
  55. {
  56. mx=dis[E[it].v]; cur[u]=it;
  57. }
  58. }
  59. if ((--gap[dis[u]])==) break;
  60. ++gap[dis[u]=mx+]; u=pre[u];
  61. }
  62. return maxflow;
  63. }
  64. bool bfs(int S,int T)
  65. {
  66. static int Q[MAXN]; memset(dis,-,sizeof(dis[])*N);
  67. dis[S]=; Q[]=S;
  68. for (int h=,t=,u,v,it;h<t;++h)
  69. {
  70. for (u=Q[h],it=G[u];~it;it=E[it].nx)
  71. {
  72. if (dis[v=E[it].v]==-&&E[it].c>E[it].f)
  73. {
  74. dis[v]=dis[u]+; Q[t++]=v;
  75. }
  76. }
  77. }
  78. return dis[T]!=-;
  79. }
  80. int dfs(int u,int T,int low)
  81. {
  82. if (u==T) return low;
  83. int ret=,tmp,v;
  84. for (int &it=cur[u];~it&&ret<low;it=E[it].nx)
  85. {
  86. if (dis[v=E[it].v]==dis[u]+&&E[it].c>E[it].f)
  87. {
  88. if (tmp=dfs(v,T,min(low-ret,E[it].c-E[it].f)))
  89. {
  90. ret+=tmp; E[it].f+=tmp; E[it^].f-=tmp;
  91. }
  92. }
  93. }
  94. if (!ret) dis[u]=-; return ret;
  95. }
  96. int dinic(int S,int T)
  97. {
  98. int maxflow=,tmp;
  99. while (bfs(S,T))
  100. {
  101. memcpy(cur,G,sizeof(G[])*N);
  102. while (tmp=dfs(S,T,inf)) maxflow+=tmp;
  103. }
  104. return maxflow;
  105. }
  106. }
  107. using namespace NetFlow;
  108.  
  109. int One;
  110. vector<int> Even,Odd;
  111. const int MAXN_ = ;
  112. bool _flag[MAXN_];
  113. int _primes[MAXN_], _pi;
  114. void GetPrime_1()
  115. {
  116. int i, j;
  117. _pi = ;
  118. memset(_flag, false, sizeof(_flag));
  119. for (i = ; i < MAXN_; i++)
  120. if (!_flag[i])
  121. {
  122. _primes[i] = ;//素数标识为1
  123. for (j = i; j < MAXN_; j += i)
  124. _flag[j] = true;
  125. }
  126. }
  127. void Clear()
  128. {
  129. Even.clear();
  130. Odd.clear();
  131. init();
  132. One=;
  133. }
  134. bool work()
  135. {
  136. int n;scanf("%d",&n);
  137. int flag = ;
  138. for(int i=;i<=n;i++)
  139. {
  140. int x;scanf("%d",&x);
  141. if(x==)One++;
  142. else if(x%==)Odd.push_back(x);
  143. else Even.push_back(x);
  144. }
  145. if(Odd.size()>Even.size())return ;
  146. if(Odd.size()+One<Even.size())return ;
  147. int In = ;
  148. while(Odd.size()<Even.size())
  149. {
  150. flag = ;
  151. Odd.push_back();
  152. One--;
  153. In++;
  154. }
  155. if(flag==&&(One==||One==))return ;
  156. int tmp = ;
  157. for(int i=;i<Odd.size();i++)
  158. link(,+i,);
  159. for(int i=;i<Even.size();i++)
  160. link(n++i,,);
  161. for(int i=;i<Odd.size();i++)
  162. {
  163. for(int j=;j<Even.size();j++)
  164. {
  165. if(_primes[Odd[i]+Even[j]])
  166. {
  167. if(Odd[i]==&&One>&&In>)
  168. link(+i,n++j,);
  169. else
  170. link(+i,n++j,);
  171. }
  172. }
  173. }
  174. if(Even.size()*==dinic(,))return ;
  175. return ;
  176. }
  177. int main()
  178. {
  179. GetPrime_1();
  180. int t;
  181. scanf("%d",&t);
  182. for(int cas=;cas<=t;cas++)
  183. {
  184. Clear();
  185. printf("%s\n",work()?"YES":"NO");
  186. }
  187. }

hdu 5594 ZYB's Prime 最大流的更多相关文章

  1. HDU 3549 Flow Problem(最大流)

    HDU 3549 Flow Problem(最大流) Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...

  2. hdu 6214 Smallest Minimum Cut[最大流]

    hdu 6214 Smallest Minimum Cut[最大流] 题意:求最小割中最少的边数. 题解:对边权乘个比边大点的数比如300,再加1 ,最后,最大流对300取余就是边数啦.. #incl ...

  3. HDU(2485),最小割最大流

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2485 Destroying the bus stations Time Limit: 40 ...

  4. hdu 2686 Matrix 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 Yifenfei very like play a number game in the n*n ...

  5. hdu 5592 ZYB's Game 树状数组

    ZYB's Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=55 ...

  6. hdu 5591 ZYB's Game 博弈论

    ZYB's Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=55 ...

  7. HDU 5590 ZYB's Biology 水题

    ZYB's Biology Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid ...

  8. hdu 5268 ZYB loves Score 水题

    ZYB loves Score Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  9. hdu 4494 Teamwork 最小费用最大流

    Teamwork Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4494 ...

随机推荐

  1. Cent OS5.2安装Hyper-V集成光盘

    一.Hyper-V安装windows系统没有问题,windows2000以后系统都可以,一切顺利. 驱动程序:IDE.SCSI.网络.视频和鼠标 要想实现更强的功能,宿主机需要安装Hyper-V集成光 ...

  2. UITableViewCell 自定义绘制 性能高

    // //  FoodListTableViewCellB.m //  BabyFood // //  Created by zhuang chaoxiao on 16/3/7. //  Copyri ...

  3. SpringMVC 3.1集成Spring Security 3.1

    这篇算是一个入门文章,昨天看见有网友提问,spring mvc集成spring security 的时候出错,揣测了一下问题木有解决.我就帮忙给搭建了一个集成框架他说可以,他告诉我这样的文章网上少.今 ...

  4. [python]Python操作MySQL

    [安装] 安装MySQL 安装MySQL不用多说了,下载下来安装就是,没有特别需要注意的地方. 一个下载地址:点击打开链接 [样例] ? 1 2 3 4 5 6 7 8 9 10 11 12 13 1 ...

  5. TopFreeTheme精选免费模板【20130629】

    今天给大家推荐9款最新精选的WordPress主题,它们涵盖了新闻.杂志.博客.房地产方面的主题.有些是商业模板,但现在都可以免费下载. GeoPlaces v4.6.2 – 来自Templatic的 ...

  6. delphi 压缩ZLib

    system.ZLib http://docwiki.embarcadero.com/CodeExamples/Berlin/en/ZLibCompressDecompress_(Delphi) 还不 ...

  7. ubuntu下设置开机自动挂载硬盘

    我们在linux中常常用mount命令把硬盘分区或者光盘挂载到文件系统中./etc/fstab就是在开机引导的时候自动挂载到linux的文件系统. 如果给计算机配了一块新磁盘,已经分区,格式化,挂载, ...

  8. ICON的设计很重要

    ICON的设计很重要 发布者: wuye | 发布时间: 2014-12-19 17:45| 评论数: 0 游戏类型:手游游戏/平板游戏  设计类型:[功能/模块/UI] 作者:小金狮的UI分享 每个 ...

  9. 细说OpenSessionInView问题

    [环境参数] 环境:SSH框架 [问题描述]  NoSession问题 HibernateTemplate对象提供的方法如果使用“延迟加载”,Session对象的管理不受开发者控制,此时如果在表现层获 ...

  10. hdu 5475 An easy problem(暴力 || 线段树区间单点更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...