![]() | ||||||||||
| ||||||||||
A simple Gaussian elimination problem.Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1020 Accepted Submission(s): 332
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?
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
Sample Output
|
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=1010;
const int INF=1000000000;
int sumx[maxn/2],sumy[maxn/2];
int n,m,k,nn,st,en;
int vis[maxn],pre[maxn],cnt;
int dis[maxn],gap[maxn],cur[maxn],head[maxn];
struct node
{
int v,f,next;
}edge[510*500*2];
void add_edge(int x,int y,int f)
{
edge[cnt].v=y;
edge[cnt].f=f;
edge[cnt].next=head[x];
head[x]=cnt++;
edge[cnt].v=x;
edge[cnt].f=0;
edge[cnt].next=head[y];
head[y]=cnt++;
}
int SAP(int st,int en)
{
for(int i=0;i<=nn;i++)
{
cur[i]=head[i];
dis[i]=gap[i]=0;
}
int u=0;
int flow=0,aug=INF;
gap[st]=nn;
u=pre[st]=st;
bool flag;
while(dis[st]<nn)
{
flag=0;
for(int &j=cur[u];j!=-1;j=edge[j].next)
{
int v=edge[j].v;
if(edge[j].f>0&&dis[u]==dis[v]+1)
{
flag=1;
if(edge[j].f<aug)aug=edge[j].f;
pre[v]=u;
u=v;
if(u==en)
{
flow+=aug;
while(u!=st)
{
u=pre[u];
edge[cur[u]].f-=aug;
edge[cur[u]^1].f+=aug;
}
aug=INF;
}
break;
}
}
if(flag)continue;
int mindis=nn;
for(int j=head[u];j!=-1;j=edge[j].next)
{
int v=edge[j].v;
if(dis[v]<mindis&&edge[j].f>0)
{
mindis=dis[v];
cur[u]=j;
}
}
if((--gap[dis[u]])==0)break;
gap[dis[u]=mindis+1]++;
u=pre[u];
}
return flow;
}
bool dfs(int u,int fa)
{
int flag=-1;
vis[u]=1;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(v==fa)continue;
if(edge[i].f>0)
{
if(vis[v]||dfs(v,u))return true;
}
if(flag==-1)head[u]=edge[i].next;//删边,如果走这条边没有换或者是容量为0,则删除,以后就不用走了
else edge[flag].next=edge[i].next;
flag=i;
}
vis[u]=0;
return false;
}
bool findcircle(int fn)
{
memset(vis,0,sizeof(vis));
for(int i=1;i<=fn;i++)
{
if(!vis[i])
{
vis[i]=1;
if(dfs(i,i))
return true;
vis[i]=0;
}
}
return false;
}
int main()
{freopen("in.txt","r",stdin);
int T,cas=1;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
k=9;
memset(sumx,0,sizeof(sumx));
memset(sumy,0,sizeof(sumy));
for(int i=1;i<=n;i++)
{
scanf("%d",&sumx[i]);
sumx[0]+=sumx[i];
}
for(int i=1;i<=m;i++)
{
scanf("%d",&sumy[i]);
sumy[0]+=sumy[i];
}
printf("Case #%d: ",cas++);
if(sumx[0]!=sumy[0])
{
printf("So naive!\n");
continue;
}
cnt=0;
memset(head,-1,sizeof(head));
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
add_edge(i,n+j,k);
int num=cnt;
for(int i=1;i<=n;i++)
add_edge(0,i,sumx[i]);
for(int j=1;j<=m;j++)
add_edge(n+j,n+m+1,sumy[j]);
nn=n+m+2;
st=0,en=n+m+1;//cout<<1;
int ans=SAP(st,en);//cout<<2;
if(ans!=sumx[0])
{
printf("So naive!\n");
continue;
}
if(findcircle(n))
{
printf("So young!\n");
continue;
}
printf("So simple!\n");
}
return 0;
}