[Hungarian algorithm] Binary Graph template poj 1274

Source: Internet
Author: User

#include <iostream>#include <cstdio>#include <memory.h>using namespace std;int n,m,num,temp,sum;int re[201][201],link[201];//牛与牛栏的对应关系bool tag[201];//增益路径bool DFS(int a){    for(int i=1;i<=m;i++)    {        if(re[a][i]!=0 && !tag[i])//如果节点i与a相邻并且未被查找过        {            tag[i]=true;//标记i为已查找过            if(link[i]==-1||DFS(link[i]))//i在匹配M中,但是从与i相邻的节点出发可以有增广路            {                link[i]=a;//记录查找成功记录//                cout << "sucess i=" << i << "   link[i]= "<<link[i] <<endl;                return true;//返回查找成功            }        }    }    return false;}int main(){    //freopen("in.txt","r",stdin);    while(cin>>n>>m) // cow stall    {        int i,j,sum=0;        memset(re,0,sizeof(re));        memset(link,-1,sizeof(link));        for(i=1;i<=n;i++)        {            cin>>num;            for(j=1;j<=num;j++)            {                cin>>temp;                re[i][temp]=1;            }        }//初始化        for(i=1;i<=n;i++)   //cow        {            for(j=1;j<=m;j++)   //stall            {                tag[j]=false;            }            if(DFS(i))//从节点i尝试扩展            sum++;        }        cout << sum << endl;    }    return 0;}

 

[Hungarian algorithm] Binary Graph template poj 1274

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.