首页 > 代码库 > nyoj 239 月老的难题 二分图最大匹配

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

题目链接: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 月老的难题 二分图最大匹配