Poj2239 Selecting Courses --- maximum matching of a Bipartite Graph

Source: Internet
Author: User

Algorithm template question in Hungary


There are n courses, and each course may have different time periods. different time periods are equivalent.

Ask the maximum number of courses that can be selected without conflict.

Create a bipartite graph. One vertex indicates different courses, and the other vertex indicates the course time (hash ).


#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <map>#define inf 0x3f3f3f3f#define eps 1e-6#define ll __int64using namespace std;int mp[310][310],used[310],link[310],n,m;bool dfs(int x){    for(int i=1;i<=m;i++)    {        if(!used[i]&&mp[x][i])        {            used[i]=1;            if(link[i]==-1||dfs(link[i]))            {                link[i]=x;                return 1;            }        }    }    return 0;}int hungry(){    int res=0;    memset(link,-1,sizeof link);    for(int i=1;i<=n;i++)    {        memset(used,0,sizeof used);        if(dfs(i))            res++;    }    return res;}int main(){    int i,a,b;    while(~scanf("%d",&n))    {        memset(mp,0,sizeof mp);        for(i=1;i<=n;i++)        {            scanf("%d",&m);            while(m--)            {                scanf("%d%d",&a,&b);                mp[i][(a-1)*12+b]=1;            }        }        m=7*12;        printf("%d\n",hungry());    }    return 0;}


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.