二分匹配_HDU_2063

来源:互联网 发布:Ubuntu 自动获取ip 编辑:程序博客网 时间:2024/05/16 05:40

这是哦入门的男女搭配啊,有点意思

#include<iostream>#include<cstdio>#include<cstring>const int maxn = 1005;using namespace std;int n,m,k;struct node{    int v,next;};node e[maxn];int head[maxn], tot;int link[maxn],vis[maxn];void init(){    tot = 0;    memset(head, -1, sizeof(head));}void add(int u, int v){    e[tot].v = v;    e[tot].next = head[u];    head[u] = tot++;}int dfs(int x){    for(int i = head[x]; i != -1; i=e[i].next)    {        int v = e[i].v;        if(!vis[v])        {            vis[v] = 1;            if(link[v] == -1 || dfs(link[v]))            {                link[v] = x;                return 1;            }        }    }    return 0;}int math(){    int cnt = 0;    memset(link,-1,sizeof(link));    for(int i = 1; i <= n; i++)    {        memset(vis,0,sizeof(vis));        if(dfs(i))            cnt++;    }    return cnt;}int main(){    int u,v;    while(scanf("%d",&k) != EOF)    {        if(k == 0)break;        init();        scanf("%d%d",&n,&m);        while(k--)        {            scanf("%d%d",&u,&v);            add(u,v);        }        printf("%d\n",math());    }    return 0;}
0 0