码迷,mamicode.com
首页 > 其他好文 > 详细

[POJ 2239] Selecting Courses

时间:2018-08-02 12:53:46      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:ace   false   print   std   namespace   ||   list   ack   tin   

[题目链接]

          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;
    
}

 

[POJ 2239] Selecting Courses

标签:ace   false   print   std   namespace   ||   list   ack   tin   

原文地址:https://www.cnblogs.com/evenbao/p/9406485.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!