A simple Gaussian elimination problem. Time Limit:1000msmemory limit:65536kbthis problem'll be judged onHDU. 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 would be useless after he got the sums, and he destroyed the table after that.
However Dragon ' s mom came back and found what he had do. She would give Dragon a feast if Dragon could reconstruct the table, otherwise keep dragon hungry. Dragon is, and so, and so, the original numbers in the table is one-digit number (e.g. 0-9).
Could Dragon to do?InputThe first line of input contains is only one integer, T (<=30), the number of test cases. Following T blocks, each block describes one test case.
There is three lines for each block. The first line contains 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.OutputEach of the output should occupy one line. Each line should start with a "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, should output one line contains only: ' So young! ', otherwise (only one-to-reconstruct the table) y OU should output: "So simple!".Sample Input
31 1552 20 100 102 22 22 2
Sample Output
Case #1: So simple! Case #2: So naive! Case #3: So young!
SourceMulti-university Training Contest 10 problem solving: With hdu 48,881 like Ah ... The key is to optimize the process of the ring. Mark the points that have been visited, and if these points are not in the loop at the time of the last visit, the visit will still not ring.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <queue>5 using namespacestd;6 Const intMAXN =1010;7 Const intINF =0x3f3f3f3f;8 structarc{9 intTo,flow,next;TenArcintx =0,inty =0,intz =-1){ Oneto =x; AFlow =y; -Next =Z; - } the}e[1000000]; - inthead[maxn],d[maxn],cur[maxn],tot,s,t; - BOOLVIS[MAXN],HV[MAXN]; - voidAddintUintVintflow) { +E[tot] =arc (V,flow,head[u]); -Head[u] = tot++; +E[tot] = arc (U,0, Head[v]); AHEAD[V] = tot++; at } - BOOLBFs () { -queue<int>Q; -memset (d,-1,sizeofd); -D[s] =1; - Q.push (S); in while(!Q.empty ()) { - intU =Q.front (); to Q.pop (); + for(inti = Head[u]; ~i; i =E[i].next) { - if(E[i].flow && d[e[i].to] = =-1){ theD[e[i].to] = D[u] +1; * Q.push (e[i].to); $ }Panax Notoginseng } - } the returnD[t] >-1; + } A intDfsintUintLow ) { the if(U = = T)returnLow ; + intTMP =0, A; - for(int&i = Cur[u]; ~i; i =E[i].next) { $ if(E[i].flow && d[e[i].to] = = d[u]+1&& (a=Dfs (E[i].to,min (low,e[i].flow)))) { $E[i].flow-=A; -e[i^1].flow + =A; -Low-=A; theTMP + =A; - if(!low) Break;Wuyi } the } - if(!tmp) D[u] =-1; Wu returntmp; - } About intDinic () { $ intRET =0; - while(BFS ()) { -memcpy (Cur,head,sizeofhead); -RET + =DFS (s,inf); A } + returnret; the } - BOOLDFS2 (intUintFA) { $ if(Vis[u])return true; theVis[u] =true; the for(inti = Head[u]; ~i; i =e[i].next) the if(!hv[e[i].to] && e[i].flow && e[i].to! = FA && dfs2 (e[i].to,u))return true; theHv[u] =true; - returnVis[u] =false; in } the intMain () { the intTs,n,m,tmp,sum,sum2,cs =1; Aboutscanf"%d",&Ts); the while(ts--){ thescanf"%d%d",&n,&m); thememset (head,-1,sizeofhead); +memset (HV,false,sizeofHV); -sum2 = sum = S = tot =0; theT = n + M +1;Bayi for(inti =1; I <= N; ++i) { thescanf"%d",&tmp); the Add (s,i,tmp); -Sum + =tmp; - for(intj =1; J <= M; ++j) theAdd (I,j+n,9); the } the for(inti =1; I <= m; ++i) { thescanf"%d",&tmp); -Add (i+n,t,tmp); theSum2 + =tmp; the } the if(Sum = =sum2) {94 if(Sum = =Dinic ()) { the BOOLFlag =false; thememset (Vis,false,sizeofvis); the for(inti =1; I <= N; ++i)98 if(flag = DFS2 (i,-1)) Break; About if(flag) printf ("Case #%d:so young!\n", cs++); - Elseprintf"Case #%d:so simple!\n", cs++);101}Elseprintf"Case #%d:so naive!\n", cs++);102}Elseprintf"Case #%d:so naive!\n", cs++);103 }104 return 0; the}View Code
HDU 4975 A Simple Gaussian elimination problem.