[POJ 2239] Selecting Courses

[题目链接]

          http://poj.org/problem?id=2239

[算法]

        将课程作为左部节点,时间作为右部节点,用匈牙利算法求二分图最大匹配即可

[代码]

        

#include <algorithm>  
#include <bitset>  
#include <cctype>  
#include <cerrno>  
#include <clocale>  
#include <cmath>  
#include <complex>  
#include <cstdio>  
#include <cstdlib>  
#include <cstring>  
#include <ctime>  
#include <deque>  
#include <exception>  
#include <fstream>  
#include <functional>  
#include <limits>  
#include <list>  
#include <map>  
#include <iomanip>  
#include <ios>  
#include <iosfwd>  
#include <iostream>  
#include <istream>  
#include <ostream>  
#include <queue>  
#include <set>  
#include <sstream>  
#include <stdexcept>  
#include <streambuf>  
#include <string>  
#include <utility>  
#include <vector>  
#include <cwchar>  
#include <cwctype>  
#include <stack>  
#include <limits.h>
using namespace std;
#define MAXN 350
const int T = 84;

struct edge
{
        int to,nxt;
} e[MAXN * T];

int i,n,m,x,y,ans,tot;
bool visited[MAXN + T];
int match[MAXN + T],head[MAXN + T];

inline void addedge(int u,int v)
{
        tot++;
        e[tot] = (edge){v,head[u]};
        head[u] = tot;
}
inline bool hungary(int u)
{
        int i,v;
        visited[u] = true;
        for (i = head[u]; i; i = e[i].nxt)    
        {
                v = e[i].to;
                if (!visited[v])
                {
                        visited[v] = true;
                        if (!match[v] || hungary(match[v]))
                        {
                                match[v] = u;
                                return true;        
                        }    
                }    
        }    
        return false;
}

int main() 
{
        
        while (scanf("%d",&n) != EOF)
        {
                tot = 0;
                memset(match,0,sizeof(match));
                for (i = T + 1; i <= T + n + 1; i++) head[i] = 0;
                for (i = 1; i <= n; i++)
                {
                        scanf("%d",&m);
                        while (m--)
                        {
                                scanf("%d%d",&x,&y);
                                addedge(i + T,12 * (x - 1) + y);
                        }
                }
                ans = 0;
                for (i = T + 1; i <= T + n + 1; i++)
                {
                        memset(visited,false,sizeof(visited));
                        if (hungary(i)) ans++;
                }
                printf("%d\n",ans);
        }
        
        return 0;
    
}

 

转载于:https://www.cnblogs.com/evenbao/p/9406485.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值