Description
Fortunately, Bessie has a powerful weapon that can vaporize all the asteroids in any given row or column of the grid with a single shot.This weapon is quite expensive, so she wishes to use it sparingly.Given the location of all the asteroids in the field, find the minimum number of shots Bessie needs to fire to eliminate all of the asteroids.
Input
* Lines 2..K+1: Each line contains two space-separated integers R and C (1 <= R, C <= N) denoting the row and column coordinates of an asteroid, respectively.
Output
Sample Input
3 4 1 1 1 3 2 2 3 2
Sample Output
2
Hint
The following diagram represents the data, where "X" is an asteroid and "." is empty space:
X.X
.X.
.X.
OUTPUT DETAILS:
Bessie may fire across row 1 to destroy the asteroids at (1,1) and (1,3), and then she may fire down column 2 to destroy the asteroids at (2,2) and (3,2).
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
const int inf=(1<<28);
using namespace std;
#define maxn 500000
struct edge
{
int u,v,next,pre;
int f;
}e[2*maxn];
int num,rnum;
int head[maxn],rhead[maxn];
int d[maxn];
int numb[maxn];
int start[maxn];
int n,m;//见图后的点数(1->n)和原图边数
int p[maxn];
int source,sink;
//要初始化source 和 sink,重定义n
void Init()
{
memset(head,-1,sizeof(head));
memset(rhead,-1,sizeof(rhead));
memset(p,-1,sizeof(p));
num=0;
return ;
}
void BFS()
{
int i,j;
for(i=1;i<=n;i++)
{
d[i]=n;
numb[i]=0;
}
int Q[maxn],head(0),tail(0);
d[sink]=0;
numb[0]=1;
Q[++tail]=sink;
while(head<tail)
{
i=Q[++head];
for(j=rhead[i];j!=-1;j=e[j].pre)
{
if(e[j].f==0||d[e[j].u]<n)
continue;
d[e[j].u]=d[i]+1;
numb[d[e[j].u]]++;
Q[++tail]=e[j].u;
}
}
return ;
}
int Augment()
{
int i;
int tmp=inf;
for(i=p[sink];i!=-1;i=p[e[i].u])
{
if(tmp>e[i].f)
tmp=e[i].f;
}
for(i=p[sink];i!=-1;i=p[e[i].u])
{
e[i].f-=tmp;
e[i^1].f+=tmp;
}
return tmp;
}
int Retreat(int &i)
{
int tmp,j,mind(n-1);
for(j=head[i];j!=-1;j=e[j].next)
{
if(e[j].f>0&&d[e[j].v]<mind)
mind=d[e[j].v];
}
tmp=d[i];
d[i]=mind+1;
numb[tmp]--;
numb[d[i]]++;
if(i!=source)
i=e[p[i]].u;
return numb[tmp];
}
int maxflow()
{
int flow(0);
int i,j;
BFS();
for(i=1;i<=n;i++)
start[i]=head[i];
i=source;
while(d[source]<n)
{
for(j=start[i];j!=-1;j=e[j].next)
if(e[j].f>0&&d[i]==d[e[j].v]+1)
break;
if(j!=-1)
{
start[i]=j;
p[e[j].v]=j;
i=e[j].v;
if(i==sink)
{
flow+=Augment();
i=source;
}
}
else
{
start[i]=head[i];
if(Retreat(i)==0)
break;
}
}
return flow;
}
//a->b=c;
void addedge(int a,int b,int c)
{
e[num].next=head[a];
head[a]=num;
e[num].pre=rhead[b];
rhead[b]=num;
e[num].f=c;
e[num].u=a;
e[num++].v=b;
e[num].next=head[b];
head[b]=num;
e[num].pre=rhead[a];
rhead[a]=num;
e[num].u=b;
e[num].v=a;
e[num++].f=0;
return ;
}
int main()
{
int tn,tm,tp;
while(scanf("%d%d",&tn,&tp)==2)
{
Init();
tm=tn;
source=1,sink=tn+tm+2;
n=tn+tm+2;
for(int i=1;i<=tn;i++)
{
addedge(source,i+1,1);
}
for(int i=1;i<=tm;i++)
{
addedge(i+1+tn,sink,1);
}
for(int i=0;i<tp;i++)
{
int u,v;scanf("%d%d",&u,&v);
addedge(u+1,v+tn+1,inf);
}
int cnt=maxflow();
printf("%d\n",cnt);
}
return 0;
}