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

3
1 1
5
5
2 2
0 10
0 10
2 2
2 2
2 2

Sample Output

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

Source

 
解题:跟hdu 4888一样啊。。。关键是是优化判环的过程。。标记已经访问过的点,如果这些点上次访问的时候没有成环,这次再去访问还是不会成环
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
struct arc{
int to,flow,next;
arc(int x = ,int y = ,int z = -){
to = x;
flow = y;
next = z;
}
}e[];
int head[maxn],d[maxn],cur[maxn],tot,S,T;
bool vis[maxn],hv[maxn];
void add(int u,int v,int flow){
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
bool bfs(){
queue<int>q;
memset(d,-,sizeof d);
d[S] = ;
q.push(S);
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].flow && d[e[i].to] == -){
d[e[i].to] = d[u] + ;
q.push(e[i].to);
}
}
}
return d[T] > -;
}
int dfs(int u,int low){
if(u == T) return low;
int tmp = ,a;
for(int &i = cur[u]; ~i; i = e[i].next){
if(e[i].flow && d[e[i].to] == d[u]+&&(a=dfs(e[i].to,min(low,e[i].flow)))){
e[i].flow -= a;
e[i^].flow += a;
low -= a;
tmp += a;
if(!low) break;
}
}
if(!tmp) d[u] = -;
return tmp;
}
int dinic(){
int ret = ;
while(bfs()){
memcpy(cur,head,sizeof head);
ret += dfs(S,INF);
}
return ret;
}
bool dfs2(int u,int fa){
if(vis[u]) return true;
vis[u] = true;
for(int i = head[u]; ~i; i = e[i].next)
if(!hv[e[i].to] && e[i].flow && e[i].to != fa && dfs2(e[i].to,u)) return true;
hv[u] = true;
return vis[u] = false;
}
int main(){
int Ts,n,m,tmp,sum,sum2,cs = ;
scanf("%d",&Ts);
while(Ts--){
scanf("%d %d",&n,&m);
memset(head,-,sizeof head);
memset(hv,false,sizeof hv);
sum2 = sum = S = tot = ;
T = n + m + ;
for(int i = ; i <= n; ++i){
scanf("%d",&tmp);
add(S,i,tmp);
sum += tmp;
for(int j = ; j <= m; ++j)
add(i,j+n,);
}
for(int i = ; i <= m; ++i){
scanf("%d",&tmp);
add(i+n,T,tmp);
sum2 += tmp;
}
if(sum == sum2){
if(sum == dinic()){
bool flag = false;
memset(vis,false,sizeof vis);
for(int i = ; i <= n; ++i)
if(flag = dfs2(i,-)) break;
if(flag) printf("Case #%d: So young!\n",cs++);
else printf("Case #%d: So simple!\n",cs++);
}else printf("Case #%d: So naive!\n",cs++);
}else printf("Case #%d: So naive!\n",cs++);
}
return ;
}

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. Mysql写出高质量的sql语句的几点建议

    CleverCode在实际的工作也写过一些低效率的sql语句.这些语句会给数据库带来非常大的压力.最基本的表现就是sql语句执行慢,后来逐渐的去优化和尝试. 总结了一些高质量的sql语句的写法.这里C ...

  2. spfile

    1 让ORACLE自己主动从spfile启动  SQL> create spfile='/dev/vx/rdsk/vgora/lv_spfile' from pfile;  SQL> sh ...

  3. Android5.0以上系统的移动网络开关

    笔者近期遇到一个非常有意思的bug,贴出来和大家分享下. 那是一个温暖的早晨,阳光晒得人非常舒服.一封bug邮件像一片叶子飘到我的邮箱. 一番交流.笔者确认负责的Widget开关在Android5.0 ...

  4. caffe 训练測试自己的数据集

    简单记录一下自己使用caffe的过程和遇到的一些问题. 下载caffe以及安装不具体叙述了. 可參照 http://caffe.berkeleyvision.org/installation.html ...

  5. zzulioj--1746--三角形面积(几何水题)

    1746: 三角形面积 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 100  Solved: 31 SubmitStatusWeb Board De ...

  6. WebAPI的自动化监控和预警

    Metrics.net + influxdb + grafana 构建WebAPI的自动化监控和预警 前言 这次主要分享通过Metrics.net + influxdb + grafana 构建Web ...

  7. CMake入门之创建一个基于PCL的最小工程

    最近在学习PCL,借助Cmake可省去繁琐的添加包含目录和依赖库操作. 一个典型的CMakeLists.txt内容通常为: cmake_minimum_required(VERSION 2.6 FAT ...

  8. MongoDB 的replicattion 复制集练习

              replicattion 相当于 mysql 的主从复制的读写分离,共同维护相同的数据,提高服务器的可用性[假如主(PRIMARY)不能用时,mongo会迅速自动切到从(SECON ...

  9. C#中使用Dictionary实现Map数据结构——VC编程网

    转载自: http://blog.51cto.com/psnx168 在VC中使用过CMap以及在Java中使用过Map的朋友应该很熟悉,使用Map可以方便实现基于键值对数据的处理,在C#中,你就需要 ...

  10. HDU-2050 折线分割平面 找规律&递推

    题目链接:https://cn.vjudge.net/problem/HDU-2050 题意 算了吧,中文题不解释了 我们看到过很多直线分割平面的题目,今天的这个题目稍微有些变化,我们要求的是n条折线 ...