Asteroids
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16509 | Accepted: 9004 |
Description
Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at the lattice points of the grid.
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.
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
* Line 1: Two integers N and K, separated by a single space.
* 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.
* 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
* Line 1: The integer representing the minimum number of times Bessie must shoot.
Sample Input
3 4 1 1 1 3 2 2 3 2
Sample Output
2
Hint
INPUT DETAILS:
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).
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).
两个做法 二分图最大匹配或网络流
匈牙利:
/** Author: ☆·aosaki(*’(OO)’*) niconiconi★ **/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//#include<bits/stdc++.h>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <functional>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <list>
#include <stack>
//#include <tuple>
#define ALL(v) (v).begin(),(v).end()
#define foreach(i,v) for (__typeof((v).begin())i=(v).begin();i!=(v).end();i++)
#define SIZE(v) ((int)(v).size())
#define mem(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define lp(k,a) for(int k=1;k<=a;k++)
#define lp0(k,a) for(int k=0;k<a;k++)
#define lpn(k,n,a) for(int k=n;k<=a;k++)
#define lpd(k,n,a) for(int k=n;k>=a;k--)
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d %d",&a,&b)
#define lowbit(x) (x&(-x))
#define ll long long
#define pi pair<int,int>
#define vi vector<int>
#define PI acos(-1.0)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define TT cout<<"*****"<<endl;
#define TTT cout<<"********"<<endl;
inline int gcd(int a,int b)
{
return a==0?b:gcd(b%a,a);
}
#define INF 1e9
#define eps 1e-8
#define mod 10007
#define MAX 10010
using namespace std;
const int MAXN=550;
int uN,vN;
int g[MAXN][MAXN];
int linker[MAXN];
bool used[MAXN];
int n,m;
bool dfs(int u)
{
int v;
for(v=0;v<vN;v++)
if(g[u][v]&&!used[v])
{
used[v]=true;
if(linker[v]==-1||dfs(linker[v]))
{
linker[v]=u;
return true;
}
}
return false;
}
int hungary()
{
int re=0;
int u;
memset(linker,-1,sizeof(linker));
for(u=0;u<uN;u++)
{
memset(used,0,sizeof(used));
if(dfs(u)) re++;
}
return re;
}
int main()
{
//freopen("in.txt","r",stdin);
int xx,yy;
while(~sc2(n,m))
{
mem(g);
uN=n;
vN=n;
while(m--)
{
sc2(xx,yy);
g[xx-1][yy-1]=1;
}
printf("%d\n",hungary());
}
return 0;
}
dinic:
/** Author: ☆·aosaki(*’(OO)’*) niconiconi★ **/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//#include<bits/stdc++.h>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <functional>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <list>
#include <stack>
//#include <tuple>
#define ALL(v) (v).begin(),(v).end()
#define foreach(i,v) for (__typeof((v).begin())i=(v).begin();i!=(v).end();i++)
#define SIZE(v) ((int)(v).size())
#define mem(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define lp(k,a) for(int k=1;k<=a;k++)
#define lp0(k,a) for(int k=0;k<a;k++)
#define lpn(k,n,a) for(int k=n;k<=a;k++)
#define lpd(k,n,a) for(int k=n;k>=a;k--)
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d %d",&a,&b)
#define lowbit(x) (x&(-x))
#define ll long long
#define pi pair<int,int>
#define vi vector<int>
#define PI acos(-1.0)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define TT cout<<"*****"<<endl;
#define TTT cout<<"********"<<endl;
inline int gcd(int a,int b)
{
return a==0?b:gcd(b%a,a);
}
#define inf 1e9
#define eps 1e-8
#define mod 10007
#define maxn 550*550
using namespace std;
int n,m,tot,S,T,f2,d2;
int pre[maxn],cpyh[maxn],q[maxn],d[maxn];
struct edge
{
int from,to,next,val;
} ji[2*maxn];
void add(int x,int y,int w)
{
ji[tot].from=x;
ji[tot].to=y;
ji[tot].val=w;
ji[tot].next=pre[x];
pre[x]=tot++;
ji[tot].from=y;
ji[tot].to=x;
ji[tot].val=0;
ji[tot].next=pre[y];
pre[y]=tot++;
}
int dinic()
{
int re=0;
while(1)
{
mem1(d);
int head=0,tail=0;
q[tail++]=S;
d[S]=0;
while(head<tail)
{
int t=q[head++];
for(int k=pre[t];k;k=ji[k].next)
{
int toit=ji[k].to;
int val=ji[k].val;
if(val&&d[toit]==-1)
{
d[toit]=d[t]+1;
q[tail++]=toit;
if(toit==T)
{
head=tail;
break;
}
}
}
}
if(d[T]==-1) break;
memcpy(cpyh,pre,(T+1)*sizeof(int));
for(int i=S,top=0;;)
{
if(i==T)
{
int flow=inf,st;
lp0(k,top)
{
if(ji[q[k]].val<flow)
{
st=k;
flow=ji[q[st]].val;
}
}
lp0(k,top)
{
ji[q[k]].val-=flow;
ji[q[k]^1].val+=flow;
}
re+=flow;
top=st;
i=ji[q[top]].from;
}
for(int j=cpyh[i];j;j=cpyh[i]=ji[j].next)
if(ji[j].val>0&&d[i]+1==d[ji[j].to])
break;
if(cpyh[i])
{
q[top++]=cpyh[i];
i=ji[cpyh[i]].to;
}
else
{
if(top==0) break;
d[i]=-1;
i=ji[q[--top]].from;
}
}
}
return re;
}
void init()
{
S=0;
tot=2;
T=2*n+1;
mem(pre);
}
int main()
{
//freopen("in.txt","r",stdin);
int xx,yy;
while(~sc2(n,m))
{
init();
lp(i,m)
{
sc2(xx,yy);
add(xx,yy+n,1);
}
lp(i,n)
{
add(0,i,1);
add(i+n,T,1);
}
printf("%d\n",dinic());
}
return 0;
}