A simple Gaussian elimination problem.

Time Limit: 1000ms
Memory Limit: 65536KB

This problem will be judged on HDU. Original ID: 4975
64-bit integer IO format: %I64d      Java class name: Main

Dragon is studying math. One day, he drew a table with several rows and columns, randomly wrote numbers on each elements of the table. Then he counted the sum of each row and column. Since he thought the map will be useless after he got the sums, he destroyed the table after that.

However Dragon's mom came back and found what he had done. She would give dragon a feast if Dragon could reconstruct the table, otherwise keep Dragon hungry. Dragon is so young and so simple so that the original numbers in the table are one-digit number (e.g. 0-9).

Could you help Dragon to do that?

 

Input

The first line of input contains only one integer, T(<=30), the number of test cases. Following T blocks, each block describes one test case.

There are three lines for each block. The first line contains two integers N(<=500) and M(<=500), showing the number of rows and columns.

The second line contains N integer show the sum of each row.

The third line contains M integer show the sum of each column.

 

Output

Each output should occupy one line. Each line should start with "Case #i: ", with i implying the case number. For each case, if we cannot get the original table, just output: "So naive!", else if we can reconstruct the table by more than one ways, you should output one line contains only: "So young!", otherwise (only one way to reconstruct the table) you should output: "So simple!".

 

Sample Input

  1. 3
  2. 1 1
  3. 5
  4. 5
  5. 2 2
  6. 0 10
  7. 0 10
  8. 2 2
  9. 2 2
  10. 2 2

Sample Output

  1. Case #1: So simple!
  2. Case #2: So naive!
  3. Case #3: So young!

Source

 
解题:跟hdu 4888一样啊。。。关键是是优化判环的过程。。标记已经访问过的点,如果这些点上次访问的时候没有成环,这次再去访问还是不会成环
 
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. using namespace std;
  6. const int maxn = ;
  7. const int INF = 0x3f3f3f3f;
  8. struct arc{
  9. int to,flow,next;
  10. arc(int x = ,int y = ,int z = -){
  11. to = x;
  12. flow = y;
  13. next = z;
  14. }
  15. }e[];
  16. int head[maxn],d[maxn],cur[maxn],tot,S,T;
  17. bool vis[maxn],hv[maxn];
  18. void add(int u,int v,int flow){
  19. e[tot] = arc(v,flow,head[u]);
  20. head[u] = tot++;
  21. e[tot] = arc(u,,head[v]);
  22. head[v] = tot++;
  23. }
  24. bool bfs(){
  25. queue<int>q;
  26. memset(d,-,sizeof d);
  27. d[S] = ;
  28. q.push(S);
  29. while(!q.empty()){
  30. int u = q.front();
  31. q.pop();
  32. for(int i = head[u]; ~i; i = e[i].next){
  33. if(e[i].flow && d[e[i].to] == -){
  34. d[e[i].to] = d[u] + ;
  35. q.push(e[i].to);
  36. }
  37. }
  38. }
  39. return d[T] > -;
  40. }
  41. int dfs(int u,int low){
  42. if(u == T) return low;
  43. int tmp = ,a;
  44. for(int &i = cur[u]; ~i; i = e[i].next){
  45. if(e[i].flow && d[e[i].to] == d[u]+&&(a=dfs(e[i].to,min(low,e[i].flow)))){
  46. e[i].flow -= a;
  47. e[i^].flow += a;
  48. low -= a;
  49. tmp += a;
  50. if(!low) break;
  51. }
  52. }
  53. if(!tmp) d[u] = -;
  54. return tmp;
  55. }
  56. int dinic(){
  57. int ret = ;
  58. while(bfs()){
  59. memcpy(cur,head,sizeof head);
  60. ret += dfs(S,INF);
  61. }
  62. return ret;
  63. }
  64. bool dfs2(int u,int fa){
  65. if(vis[u]) return true;
  66. vis[u] = true;
  67. for(int i = head[u]; ~i; i = e[i].next)
  68. if(!hv[e[i].to] && e[i].flow && e[i].to != fa && dfs2(e[i].to,u)) return true;
  69. hv[u] = true;
  70. return vis[u] = false;
  71. }
  72. int main(){
  73. int Ts,n,m,tmp,sum,sum2,cs = ;
  74. scanf("%d",&Ts);
  75. while(Ts--){
  76. scanf("%d %d",&n,&m);
  77. memset(head,-,sizeof head);
  78. memset(hv,false,sizeof hv);
  79. sum2 = sum = S = tot = ;
  80. T = n + m + ;
  81. for(int i = ; i <= n; ++i){
  82. scanf("%d",&tmp);
  83. add(S,i,tmp);
  84. sum += tmp;
  85. for(int j = ; j <= m; ++j)
  86. add(i,j+n,);
  87. }
  88. for(int i = ; i <= m; ++i){
  89. scanf("%d",&tmp);
  90. add(i+n,T,tmp);
  91. sum2 += tmp;
  92. }
  93. if(sum == sum2){
  94. if(sum == dinic()){
  95. bool flag = false;
  96. memset(vis,false,sizeof vis);
  97. for(int i = ; i <= n; ++i)
  98. if(flag = dfs2(i,-)) break;
  99. if(flag) printf("Case #%d: So young!\n",cs++);
  100. else printf("Case #%d: So simple!\n",cs++);
  101. }else printf("Case #%d: So naive!\n",cs++);
  102. }else printf("Case #%d: So naive!\n",cs++);
  103. }
  104. return ;
  105. }

HDU 4975 A simple Gaussian elimination problem.的更多相关文章

  1. hdu 4975 A simple Gaussian elimination problem.(网络流,推断矩阵是否存在)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4975 Problem Description Dragon is studying math. One ...

  2. hdu - 4975 - A simple Gaussian elimination problem.(最大流量)

    意甲冠军:要在N好M行和列以及列的数字矩阵和,每个元件的尺寸不超过9,询问是否有这样的矩阵,是独一无二的N(1 ≤ N ≤ 500) , M(1 ≤ M ≤ 500). 主题链接:http://acm ...

  3. hdu 4975 A simple Gaussian elimination problem 最大流+找环

    原题链接 http://acm.hdu.edu.cn/showproblem.php?pid=4975 这是一道很裸的最大流,将每个点(i,j)看作是从Ri向Cj的一条容量为9的边,从源点除法连接每个 ...

  4. HDOJ 4975 A simple Gaussian elimination problem.

    和HDOJ4888是一样的问题,最大流推断多解 1.把ISAP卡的根本出不来结果,仅仅能把全为0或者全为满流的给特判掉...... 2.在残量网络中找大于2的圈要用一种类似tarjian的方法从汇点開 ...

  5. hdu4975 A simple Gaussian elimination problem.(正确解法 最大流+删边判环)(Updated 2014-10-16)

    这题标程是错的,网上很多题解也是错的. http://acm.hdu.edu.cn/showproblem.php?pid=4975 2014 Multi-University Training Co ...

  6. A simple Gaussian elimination problem.(hdu4975)网络流+最大流

    A simple Gaussian elimination problem. Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65 ...

  7. A simple Gaussian elimination problem.

    hdu4975:http://acm.hdu.edu.cn/showproblem.php?pid=4975 题意:给你一个n*m的矩阵,矩阵中的元素都是0--9,现在给你这个矩阵的每一行和每一列的和 ...

  8. hdu4975 A simple Gaussian elimination problem.(最大流+判环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4975 题意:和hdu4888基本一样( http://www.cnblogs.com/a-clown/ ...

  9. hdu 4972 A simple dynamic programming problem(高效)

    pid=4972" target="_blank" style="">题目链接:hdu 4972 A simple dynamic progra ...

随机推荐

  1. Cocos2dx 小技巧(十五)话说ScrollView的delegate实现过程

    附:本文參加了CSDN博客大赛.亲假设认为这篇文章不错,就大胆的来投上一票吧! !!http://vote.blog.csdn.net/Article/Details? articleid=34140 ...

  2. Codeforces Round #249 (Div. 2) (模拟)

    C. Cardiogram time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  3. Android4.2.2下Stagefright下OMX编解码器组件的控制流

    本文均属自己阅读源代码的点滴总结.转账请注明出处谢谢. 欢迎和大家交流. qq:1037701636 email:gzzaigcn2012@gmail.com Android源代码版本号Version ...

  4. BAPC2014 K&amp;&amp;HUNNU11591:Key to Knowledge(中途相遇法)

    题意: 有N个学生.有M题目 然后相应N行分别有一个二进制和一个整数 二进制代表该同学给出的每道题的答案.整数代表该同学的答案与标准答案相符的个数 要求推断标准答案有几个,假设标准答案仅仅有一种.则输 ...

  5. 解决VMware Pro 14安装Linux镜像时黑屏问题

    软件及版本: VMware-workstation-full-14.0.0-6661328 CentOS-6.8-x86_64-bin-DVD1 系统: win10 问题: 启动虚拟机,配置完cent ...

  6. [HAOI2007]理想的正方形 单调队列 暴力

    Code: #include<cstdio> #include<queue> #include<algorithm> using namespace std; #d ...

  7. 使用 init-runonce脚本创建一个 openstack云项目

    source /etc/kolla/admin-openrc.sh cd /usr/share/kolla-ansible ./init-runonce 报错内容 Traceback (most re ...

  8. python IO编程-序列化

    原文链接:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143192607 ...

  9. 洛谷 P1056 排座椅

    P1056 排座椅 题目描述 上课的时候总会有一些同学和前后左右的人交头接耳,这是令小学班主任十分头疼的一件事情.不过,班主任小雪发现了一些有趣的现象,当同学们的座次确定下来之后,只有有限的D对同学上 ...

  10. HDOJ 2544 最短路(最短路径 dijkstra算法,SPFA邻接表实现,floyd算法)

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...