Asteroids 二分图

来源:互联网 发布:2016做淘宝客 编辑:程序博客网 时间:2024/06/02 04:01
Asteroids
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 21082 Accepted: 11443

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.

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.

Output

* Line 1: The integer representing the minimum number of times Bessie must shoot.

Sample Input

3 41 11 32 23 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).




匈牙利算法介绍:匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名。匈牙利算法是基于Hall定理中充分性证明的思想,它是部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最大匹配的算法。---------------------------------*/#include <iostream>#include <cstdio>#include <cstring>#define INF 0x3f3f3f3fusing namespace std;int n,k,r,c;int g[550][550];bool vis[10010];int link[10010];bool find(int v){for(int i=1; i<=n; i++){if(g[v][i] && !vis[i]){vis[i] = true;if(link[i] == 0 || find(link[i])){link[i] = v;return true;}}}return false;}int main(){//freopen("input.txt","r",stdin);int ans;while(scanf("%d%d",&n,&k) != EOF){memset(g,0,sizeof(g));memset(link,0,sizeof(link));for(int i = 0; i < k; i++){scanf("%d%d",&r,&c);g[r][c] = 1;}ans = 0;for(int i=1; i<=n; i++){memset(vis,0,sizeof(vis));//清空上次搜索时的标记if(find(i))//从节点i尝试扩展ans++;}printf("%d\n",ans);}return 0;}
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int Maxn=555;
int Map[Maxn][Maxn],lie[Maxn],hang[Maxn];//匹配的列//标记访问过的行
int n,k;
int bfs(int i)
{
   for(int j=1;j<=n;j++)
   {
     if(Map[i][j]&&lie[j]==0)
     {
      lie[j]=1;
      if(!hang[j]||bfs(hang[j]))
      {
        hang[j]=i;
        return 1;
      }
     }
   }
   return 0;
}
int dfs(int i)
{
   for(int j=1;j<=n;j++)
   {
     if(Map[i][j]&&hang[j]==0)
     {
       hang[j]=1;
       if(!lie[j]||dfs(lie[j]))
       {
         lie[j]=i;
         return 1;
       }
     }
   }
   return 0;
}


int main()
{
   int a,b;
   while(cin>>n>>k)
   {
     memset(Map,0,sizeof(Map));
     for(int i=0;i<k;i++)
     {
       scanf("%d%d",&a,&b);
         Map[a][b]=1;
     }
     memset(lie,0,sizeof(lie));
     int ans=0;
     for(int i=1;i<=n;i++)
     {
       memset(hang,0,sizeof(hang));
       ans+=dfs(i);
     }
     printf("%d\n",ans);
   }
}
用最少的顶点覆盖所有的边;
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
const int MAXN=550;
using namespace std;
int n,k;
vector<int> g[MAXN];
int from[MAXN],tot;
bool use[MAXN];
bool match(int x){
    for(int i=0;i<g[x].size();++i){
        if(!use[g[x][i]]){
            use[g[x][i]]=true;
            if(from[g[x][i]]==-1||match(from[g[x][i]])){
                from[g[x][i]]=x;
                return true;
            }
        }
    }
    return false;
}
int hungary(){
    tot=0;
    memset(from,0xff,sizeof(from));
    for(int i=1;i<=n;++i){
        memset(use,0,sizeof(use));
        if(match(i))
            tot++;
    }
    return tot;
}
int main()
{
    int u,v;
    int u,v;
    while(scanf("%d%d",&n,&k)!=EOF){
        for(int i=1;i<=n;++i) g[i].clear();
        for(int i=0;i<k;++i){
            scanf("%d%d",&u,&v);
            g[u].push_back(v);
        }
        printf("%d\n",hungary());
    }
    return 0;
}



#include<cstring>
#include<iostream>
#include<vector>
#include<cstdio>
using namespace std;
const int maxn=555;
int x[maxn],y[maxn];
int used[maxn],match[maxn];
int n,k;
vector<int>G[maxn];
void add_edge(int u,int v)
{
  G[v].push_back(u);
  G[u].push_back(v);
}
int dfs(int v)
{
   used[v]=1;
   for(int i=0;i<G[v].size();i++)
   {
      int u=G[v][i],w=match[u];
      if(w<0||!used[w]&&dfs(w))
      {
        match[v]=u;match[u]=v;
        return 1;
      }
   }
   return 0;
}
int bipartite_matching()
{


   int res=0;
   memset(match,-1,sizeof(match));
   for(int i=1;i<=2*n;i++)
   {
      if(match[i]<0)
      {
        memset(used,0,sizeof(used));
        if(dfs(i))
        res++;
      }
   }
   return res;
}
int main()
{
    scanf("%d%d",&n,&k);
    memset(match, -1, sizeof(match));
    for(int i = 0; i < k; i++){
        scanf("%d%d",&x[i],&y[i]);
        add_edge(x[i], y[i] + n);
    }
    printf("%d\n",  bipartite_matching());
}

































0 0