A simple Gaussian elimination problem.
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1528 Accepted Submission(s): 479
Problem Description
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?
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.
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!
Author
BJTU
Source
Recommend
选择这个是个错误QAQ
题意:给定一个n*m的棋盘,给出每行的和以及每列的和,问是否可以确定出该棋盘(唯一,多解or无解)
很容易想到建边的方式,然后兴致冲冲的写了,没有想到的是居然还要判唯一!这时候需要理解一下模板的flow[]流量这个玩意==,如果有那么一圈的flow[]都是大于0的,说明流量可以在这一圈上流动,我们需要写一个dfs做判断,依次用1-n调用Dfs(i,-1)判断以某点开头的路径是否存在一个边数大于0的正环。由于这个算法是把题中所有给出的所有的边都在遍历每个点时最坏需要遍历一遍,我们需要想办法将复杂度降低,方法:如果边为0或者走了这条边没有发现环,就将这条边删去省的下次遍历,如果出现了一个环,返回真,算法结束。biu这货是每次没找到环就赋成当前边序号的,if else写法不同只是是否是开头的区别(链式前向星没理解好orz)
其实更好的是王太虚的这篇,他是重新建边的,也很好理解,跑完最大流依次判断每条边,为残余网络建边,不计流量的跑一圈下来,看是否有环==
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int mm=1000000;
const int mn=505*505*3;
const int oo=1000000000;
int node,src,dest,edge;
int reach[mm],flow[mm],nxt[mm];
int head[mn],work[mn],dis[mn],q[mn];
inline int min(int a,int b)
{
return a<b?a:b;
}
inline void prepare(int _node,int _src,int _dest)
{
node=_node,src=_src,dest=_dest;
for(int i=0;i<node;++i)head[i]=-1;
edge=0;
}
inline void addedge(int u,int v,int c1,int c2)
{
reach[edge]=v,flow[edge]=c1,nxt[edge]=head[u],head[u]=edge++;
reach[edge]=u,flow[edge]=c2,nxt[edge]=head[v],head[v]=edge++;
}
bool Dinic_bfs()
{
int i,u,v,l,r=0;
for(i=0;i<node;++i)dis[i]=-1;
dis[q[r++]=src]=0;
for(l=0;l<r;++l)
for(i=head[u=q[l]];i>=0;i=nxt[i])
if(flow[i]&&dis[v=reach[i]]<0)
{
dis[q[r++]=v]=dis[u]+1;
if(v==dest)return 1;
}
return 0;
}
int Dinic_dfs(int u,int exp)
{
if(u==dest)return exp;
for(int &i=work[u],v,tmp;i>=0;i=nxt[i])
if(flow[i]&&dis[v=reach[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
{
flow[i]-=tmp;
flow[i^1]+=tmp;
return tmp;
}dis[u]--;
return 0;
}
int Dinic_flow()
{
int i,ret=0,delta;
while(Dinic_bfs())
{
for(i=0;i<node;++i)work[i]=head[i];
while(delta=Dinic_dfs(src,oo))ret+=delta;
}
return ret;
}
///====================
int mark[mn];
bool dfs(int u,int pre)
{
int biu=-1;
mark[u]=true;
for(int i=head[u];i!=-1;i=nxt[i])
{
int v=reach[i];
if(pre==v)continue;
if(flow[i]>0)
{
if(mark[v])return true;
if(dfs(v,u))return true;
}
if(biu==-1) head[u]=nxt[i];
else nxt[biu]=nxt[i];
biu=i;
}
mark[u]=false;
return false;
}
bool judge()
{
memset(mark,false,sizeof(mark));
for(int i=1;i<=node;i++)
if(dfs(i,-1))return true;
return false;
}
///====================
int main()
{
// freopen("cin.txt","r",stdin);
int n,m,t,cas=1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
prepare(n+m+2,0,n+m+1);
int sum=0;
for(int i=1;i<=n;i++)
{
int a;
scanf("%d",&a);
sum+=a;
addedge(0,i,a,0);
}
for(int i=1+n;i<=n+m;i++)
{
int a;
scanf("%d",&a);
addedge(i,n+m+1,a,0);
}
for(int i=1;i<=n;i++)
for(int j=n+1;j<=n+m;j++)
addedge(i,j,9,0);
int total=Dinic_flow();
if(sum!=total)
printf("Case #%d: So naive!\n",cas++);
else if(judge())
printf("Case #%d: So young!\n",cas++);
else printf("Case #%d: So simple!\n",cas++);
}
return 0;
}