hdu 1083 簡單的二分匹配 Courses

來源:互聯網
上載者:User

題目 http://acm.hdu.edu.cn/showproblem.php?pid=1083

這是一個簡單的求最大匹配的問題

有二種方法  

ac1

#include<iostream>#include<cstring>#include<cstdio>using namespace std;bool map[101][301];bool mark[301];int cx[101],cy[301]; //cx表示有多少門課  cy表示學生哦int p,n;int findpath(int u) //找出增廣路徑哦{    int i;    for(i=1;i<=n;i++)    {        if(map[u][i]&&!mark[i]) //注意u的位置哦        {            mark[i]=1;                       if(cy[i]==-1||findpath(cy[i]))            {                cy[i]=u;                cx[u]=i;                return 1;            }        }    }   return 0;}void maxmatch()  //找出最大匹配哦{    int i,j;    int res=0;    for(i=1;i<=p;i++)     cx[i]=-1;    for(j=1;j<=n;j++)     cy[j]=-1;    for(i=1;i<=p;i++)    {        if(cx[i]==-1)        {            for(j=1;j<=n;j++)            mark[j]=0;            res+=findpath(i);        }    }    if(res>=p) {cout<<"YES"<<endl; return ;}    else cout<<"NO"<<endl;}int main(){    int i,j,k,m;    cin>>k;    while(k--)    {        memset(map,false,sizeof(map));        cin>>p>>n;        for(i=1;i<=p;i++)        {            cin>>j;            for(int h=1;h<=j;h++)            {                cin>>m;                map[i][m]=1;            }        }        maxmatch();    }    return 0;}

   ac2

#include<iostream>#include<cstring>#include<cstdio>using namespace std;bool map[301][301];bool mark[301];int link[301]; //cx表示有多少門課  cy表示學生哦int p,n;int findpath(int u){    int i;    for(i=1;i<=n;i++)    {        if(map[u][i]&&!mark[i])        {            mark[i]=1;            //這裡到底是cx還是cy呢?            if(!link[i]||findpath(link[i]))            {                link[i]=u;                return 1;            }        }    }   return 0;}void maxmatch(){    int i,j;    int res=0;    for(j=1;j<=n;j++)     link[j]=0;    for(i=1;i<=p;i++)    {        for(j=1;j<=n;j++)        mark[j]=0;        res+=findpath(i);    }    if(res>=p) {cout<<"YES"<<endl; return ;}    else cout<<"NO"<<endl;}int main(){    int i,j,k,m;    cin>>k;    while(k--)    {        memset(map,false,sizeof(map));        memset(link,false,sizeof(link));        cin>>p>>n;        for(i=1;i<=p;i++)        {            cin>>j;            for(int h=1;h<=j;h++)            {                cin>>m;                map[i][m]=1;            }        }        maxmatch();       // cout<<"ddddddddddddd1"<<endl;    }    return 0;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.