Test Instructions:Link
Method:Maximum Flow
parsing:The question always feels like I've seen it. And then actually this is the maximum matching problem of a binary graph. Establish the source point to each row with a flow of 1 edges. For a bad point, there is a 1 edge between the line where the bad point resides and the column where the bad point is located. Each column has an edge of 1 flow to the meeting point. You can use the maximum flow to solve the match.
Code:
#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 510#define M 20100#define INF 0x3f3f3f3fusing namespace STD;inthead[n<<1];intdep[n<<1];structnode{intFrom,to,val,next;} EDGE[M];intCnt,s,t,n,k;voidInit () {memset(head,-1,sizeof(head));}voidEdgeadd (intFromintTo,intval) {edge[cnt].from=from,edge[cnt].to=to; Edge[cnt].val=val; Edge[cnt].next=head[from]; head[from]=cnt++;}intBFsintSintE) {memset(DEP,0,sizeof(DEP)); Queue<int>Q Q.push (s); dep[s]=1; while(!q.empty ()) {intU=q.front (); Q.pop ();if(u==e)return 1; for(inti=head[u];i!=-1; i=edge[i].next) {intto=edge[i].to;if(dep[to]==0&&edge[i].val!=0) {dep[to]=dep[u]+1; Q.push (to); } } }return 0;}intDfsintSintMax_vale) {intret=0, TMP;if(s==n*2+1)returnMax_vale; for(inti=head[s];i!=-1; i=edge[i].next) {intto=edge[i].to;if(dep[to]!=dep[s]+1|| edge[i].val==0)Continue; Tmp=dfs (To,min (max_vale-ret,edge[i].val)); edge[i].val-=tmp; edge[i^1].val+=tmp; ret+=tmp;if(Ret==max_vale)returnMax_vale; }returnRET;}intDinic () {intret=0; while(BFS (s,t)) { while(intT=dfs (S,inf)) {ret+=t; } }returnRET;}intMain () {init ();scanf("%d%d", &n,&k); s=0, t=n*2+1; for(intI=1; i<=k;i++) {intx, y;scanf("%d%d", &x,&y); Edgeadd (X,y+n,1); Edgeadd (Y+n,x,0); } for(intI=1; i<=n;i++) {Edgeadd (s,i,1); Edgeadd (I,s,0); Edgeadd (I+n,t,1); Edgeadd (T,i+n,0); }printf("%d\n", Dinic ());}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Bzoj 1693 [Usaco2007 demo]asteroids Max Stream