Click to open link
Test instructions: Given the difference between the value of each row and the value of each column and the value of each element in 0~9, ask how many conditions are met, multiple, one, and impossible to output three different cases respectively
Thinking: Just read the question there is no idea, read the net to know with the network flow, that is good to do, to build a source point, with each line to build a traffic for the line and the edge, each column and the meeting point to build a traffic for columns, each row and each column to build a flow of 9 of the edge, run the maximum flow after the judge whether full flow on the line, But how can you tell if there are many groups of solutions? When there is a loop in the residual network, we can adjust the ring to meet the conditions, add an edge 1, then the other side can be subtracted 1, so to determine whether there is no more than 2 in the residual network ring, and will not ..., see most of the puzzle is the DFS backtracking when the deletion or deletion, and will not And then my approach is to pre-preprocess the feasible side first, then very violent Dfs, accidentally 400ms too ....
#include <queue> #include <vector> #include <stdio.h> #include <string.h> #include <stdlib.h > #include <iostream> #include <algorithm> #include <functional>using namespace Std;typedef long Long ll;const int inf=0x3f3f3f3f;const int maxn=1010;struct edge{int to,cap,rev; Edge (int a,int b,int c) {to=a;cap=b;rev=c;}}; Vector<edge>g[maxn];vector<int>g1[maxn];int level[maxn],iter[maxn],vis[maxn],n,m;void Addedge (int From,int to,int cap) {G[from].push_back (Edge (To,cap,g[to].size ())); G[to].push_back (Edge (From,0,g[from].size ()-1));} void Add_edge (int from,int to) {g1[from].push_back (to);} void BFs (int s) {memset (level,-1,sizeof (level)); queue<int>que;level[s]=0; Que.push (s); while (!que.empty ()) {int V=que.front (); Que.pop (); for (unsigned int i=0;i<g[v].size (); i++) {Edge &e=G[v][i]; if (e.cap>0&&level[e.to]<0) {level[e.to]=level[v]+1; Que.push (e.to); }}}}int dfs (int v,int t,int f) {if (v==t) return F; for (int &i=iter[v];i<g[v].size (); i++) {Edge &e=G[v][i]; if (e.cap>0&&level[v]<level[e.to]) {int D=dfs (e.to,t,min (F,e.cap)); if (d>0) {e.cap-=d; G[e.to][e.rev].cap+=d; return D; }}} return 0;} int max_flow (int s,int t) {int flow=0; while (1) {BFS (s); if (level[t]<0) return flow; memset (Iter,0,sizeof (ITER)); int F; while ((F=dfs (S,t,inf)) >0) flow+=f; }}bool Judge_dfs (int x,int val) {vis[x]=1; for (unsigned int i=0;i<g1[x].size (); i++) {int t=g1[x][i]; if (t==val) continue; if (Vis[t]) return 1; if (Judge_dfs (t,x)) return 1; } vis[x]=0; return 0;} BOOL Judge () {memset (vis,0,sizeof (VIS)); for (int i=0;i<=n+m+1;i++) {for (unsigned int j=0;j<g[i].size (); j + +) { Edge &e=G[i][j]; if (e.cap>0) Add_edge (i,e.to); }} for (int i=0;i<=n;i++) {if (Judge_dfs (i,-1)) return 1; } return 0;} int main () {int t,a,b,t=1; scanf ("%d", &t); while (t--) {for (int i=0;i<maxn;i++) {g[i].clear (); G1[i].clear (); } scanf ("%d%d", &n,&m); int sum=0,sum1=0,sum2=0; for (int i=1;i<=n;i++) {scanf ("%d", &a); sum+=a;sum1+=a; Addedge (0,i,a); } for (int j=1;j<=m;j++) {scanf ("%d", &b); sum2+=b; Addedge (J+N,N+M+1,B); } if (sum1!=sum2) {printf ("Case #%d:so naive!\n", t++); Continue } for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) Addedge (i,j+n,9); } int Ans=max_flow (0,n+m+1); if (ans!=sum) printf ("Case #%d:so naive!\n", t++); else{if (judge ()) printf ("Case #%d:so young!\ n ", t++); else printf ("Case #%d:so simple!\n", t++); }} return 0;}
HDU 4975 maximum flow + judgment ring