[POJ 2239] Selecting Courses
Posted evenbao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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 ",ans); } return 0; }
以上是关于[POJ 2239] Selecting Courses的主要内容,如果未能解决你的问题,请参考以下文章
POJ 2239 Selecting Course 二分图匹配(水)
POJ2239-Selecting Courses-(匈牙利算法)