1. 程式人生 > >[POJ 2239] Selecting Courses

[POJ 2239] Selecting Courses

mem streambuf gary oca ios als ctime ani def

[題目鏈接]

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