
A simple Gaussian elimination problem.
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 728 Accepted Submission(s):
241
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?
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.
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!".
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <numeric>
using namespace std;
typedef long long LL;
const int MAXN = ;
const int MAXV = MAXN << ;
const int MAXE = * MAXN * MAXN;
const int INF = 0x3f3f3f3f;
struct ISAP
{
int head[MAXV], cur[MAXV], gap[MAXV], dis[MAXV], pre[MAXV];
int to[MAXE], next[MAXE], flow[MAXE];
int n, ecnt, st, ed;
void init(int n)
{
this->n = n;
memset(head + , -, n * sizeof(int));
ecnt = ;
}
void add_edge(int u, int v, int c)
{
to[ecnt] = v;
flow[ecnt] = c;
next[ecnt] = head[u];
head[u] = ecnt++;
to[ecnt] = u;
flow[ecnt] = ;
next[ecnt] = head[v];
head[v] = ecnt++; }
void bfs()
{
memset(dis + , 0x3f, n * sizeof(int));
queue<int> que;
que.push(ed);
dis[ed] = ;
while(!que.empty())
{
int u = que.front();
que.pop();
gap[dis[u]]++;
for(int p = head[u]; ~p; p = next[p])
{
int v = to[p];
if(flow[p ^ ] && dis[u] + < dis[v])
{
dis[v] = dis[u] + ;
que.push(v);
}
}
}
} int max_flow(int ss, int tt)
{
st = ss, ed = tt;
int ans = , minFlow = INF;
for(int i = ; i <= n; ++i)
{
cur[i] = head[i];
gap[i] = ; }
bfs();
int u = pre[st] = st;
while(dis[st] < n)
{
bool flag = false;
for(int &p = cur[u]; ~p; p = next[p])
{
int v = to[p];
if(flow[p] && dis[u] == dis[v] + )
{
flag = true;
minFlow = min(minFlow, flow[p]);
pre[v] = u;
u = v;
if(u == ed)
{
ans += minFlow;
while(u != st)
{
u = pre[u];
flow[cur[u]] -= minFlow;
flow[cur[u] ^ ] += minFlow; }
minFlow = INF; }
break; } }
if(flag) continue;
int minDis = n - ;
for(int p = head[u]; ~p; p = next[p])
{
int &v = to[p];
if(flow[p] && dis[v] < minDis)
{
minDis = dis[v];
cur[u] = p; }
}
if(--gap[dis[u]] == ) break;
++gap[dis[u] = minDis + ];
u = pre[u]; }
return ans; } int stk[MAXV], top;
bool sccno[MAXV], vis[MAXV];
bool dfs(int u, int f, bool flag)
{
vis[u] = true;
stk[top++] = u;
for(int p = head[u]; ~p; p = next[p]) if(flow[p])
{
int v = to[p];
if(v == f) continue;
if(!vis[v])
{
if(dfs(v, u, flow[p ^ ])) return true; }
else if(!sccno[v]) return true; }
if(!flag)
{
while(true)
{
int x = stk[--top];
sccno[x] = true;
if(x == u) break; } }
return false; }
bool acycle()
{
memset(sccno + , , n * sizeof(bool));
memset(vis + , , n * sizeof(bool));
top = ;
return dfs(ed, , ); }
} G;
int row[MAXN], col[MAXN];
int mat[MAXN][MAXN];
int n, m, k, ss, tt;
void solve()
{
int sumr = accumulate(row + , row + n + , );
int sumc = accumulate(col + , col + m + , );
if(sumr != sumc)
{
puts("So naive!");
return ; }
int res = G.max_flow(ss, tt);
if(res != sumc)
{
puts("So naive!");
return ; }
if(G.acycle())
{
puts("So young!"); }
else
{
puts("So simple!");
} }
int main()
{
int T,Case;
scanf("%d",&T); for(Case=;Case<=T;Case++)
{
scanf("%d%d",&n,&m);
k=;
for(int i = ; i <= n; ++i) scanf("%d", &row[i]);
for(int i = ; i <= m; ++i) scanf("%d", &col[i]);
ss = n + m + , tt = n + m + ;
printf("Case #%d: ",Case);
G.init(tt);
for(int i = ; i <= n; ++i) G.add_edge(ss, i, row[i]);
for(int i = ; i <= m; ++i) G.add_edge(n + i, tt, col[i]);
for(int i = ; i <= n; ++i)
{
for(int j = ; j <= m; ++j)
{
mat[i][j] = G.ecnt ^ ;
G.add_edge(i, n + j, k);
}
}
solve(); }
}