hdu 4619 Warm up 2 (二分匹配)

来源:互联网 发布:正版matlab软件价格 编辑:程序博客网 时间:2024/05/21 09:41
/*二分匹配邻接表 (匈牙利算法)*/# include<stdio.h># include<algorithm># include<string.h>using namespace std;# define MAX 13000int cnt;int n,m,map[MAX],vis[MAX],head[MAX];int tot;struct node{   // int u;    int to;    int next;}Edge[MAX*2];void add(int u,int v){//    Edge[tot].u=u;    Edge[tot].to=v;    Edge[tot].next=head[u];    head[u]=tot;    tot++;   // Edge[tot].u=v;    Edge[tot].to=u;    Edge[tot].next=head[v];    head[v]=tot;    tot++;}int bfs(int x){    for(int i=head[x]; i!=-1; i=Edge[i].next)    {       int  v=Edge[i].to;        if(!vis[v])        {            vis[v]=1;            if(!map[v]||bfs(map[v]))            {                map[v]=x;                return 1;            }        }    }    return 0;}int f(){    int a=0;    memset(map,0,sizeof(map));    for(int i=0; i<=12000; i++)//总点数    {        memset(vis,0,sizeof(vis));        if(bfs(i))            a++;    }    return a;}int main(){    int i,j,k,cot1=0,a,b,u,v;    while(~scanf("%d%d",&n,&m),n+m)    {        tot=0;        memset(head,-1,sizeof(head));        for(i=0; i<n; i++)        {            scanf("%d%d",&a,&b);            add((b)*102+(a),(b)*102+a+1);///离散化        }        for(i=0; i<m; i++)        {            scanf("%d%d",&a,&b);            add((b)*102+a,(b+1)*102+a);        }        int ans=f();        printf("%d\n",ans/2);    }    return 0;}

0 0
原创粉丝点击