码迷,mamicode.com
首页 > 其他好文 > 详细

nyoj 239 月老的难题 二分图最大匹配

时间:2014-10-14 02:15:17      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   for   sp   div   

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=239

 

思路:二分图最大匹配~~~ 邻接表A过~~

 

#include <iostream>
#include <cstring>
using namespace std;

int num;
int n,k;
int s;
struct node
{
    int u,v,w;
    int next;
}map[10010];
int head[510];
int match[510];
int used[510];

bool solve(int s)
{
    for(int i = head[s]; i != -1; i = map[i].next)
    {
        if(used[map[i].v] == 0)
        {
            used[map[i].v] = 1;
            if(match[map[i].v] == -1 || solve(match[map[i].v]) == true)
            {
                match[map[i].v] = s;
                return true;
            }
        }
    }
    return false;
}

void add_edge(int u, int v, int w)
{
    map[s].u = u;
    map[s].v = v;
    map[s].w = 1;
    map[s].next = head[u];
    head[u] = s++;
} 


int main()
{
    cin>>num;
    
    int a,b;
    while(num--)
    {
        s = 0;
        cin>>n>>k;
        
        memset(head, -1, sizeof(head));
        memset(map, 0, sizeof(map));
        memset(match,-1,sizeof(match));
        for(int i=1; i<=k; i++)
        {
            cin>>a>>b;
            add_edge(a,b,1); 
        }
        
        int res = 0;
        for(int i=1; i<=n; i++)
        {
            memset(used, 0, sizeof(used));
            if(solve(i))
                res++;
        }
        cout<<res<<endl;
    }
    return 0;
} 

 

nyoj 239 月老的难题 二分图最大匹配

标签:style   blog   http   color   io   os   for   sp   div   

原文地址:http://www.cnblogs.com/ltwy/p/4023362.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!