Poj--2239(二分图最大匹配)

2014-11-04 17:41:40

思路:以课程编号为X集合,时间为Y集合,进行匹配即可。比较裸的匈牙利。

 1 /*************************************************************************
 2     > File Name: 2239.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com 
 5     > Created Time: Sun 02 Nov 2014 05:36:32 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <vector>
13 #include <map>
14 #include <set>
15 #include <stack>
16 #include <queue>
17 #include <iostream>
18 #include <algorithm>
19 using namespace std;
20 #define lp (p << 1)
21 #define rp (p << 1|1)
22 #define getmid(l,r) (l + (r - l) / 2)
23 #define MP(a,b) make_pair(a,b)
24 typedef long long ll;
25 const int INF = 1 << 30;
26 const int maxn = 310;
27 
28 int n;
29 int first[maxn],next[maxn * 100],ver[maxn * 100],ecnt;
30 int used[maxn],mat[maxn];
31 
32 void Init(){
33     memset(first,-1,sizeof(first));
34     ecnt = 0;
35 }
36 
37 void Add_edge(int u,int v){
38     next[++ecnt] = first[u];
39     ver[ecnt] = v;
40     first[u] = ecnt;
41 }
42 
43 bool find(int p){
44     for(int i = first[p]; i != -1; i = next[i]){
45         int v = ver[i];
46         if(used[v] == 0){
47             used[v] = 1;
48             if(mat[v] == 0 || find(mat[v])){
49                 mat[v] = p;
50                 return true;
51             }
52         }
53     }
54     return false;
55 }
56 
57 int Hungary(){
58     int ans = 0;
59     memset(mat,0,sizeof(mat));
60     for(int i = 1; i <= n; ++i){
61         memset(used,0,sizeof(used));
62         if(find(i)) ++ans;
63     }
64     return ans;
65 }
66 
67 int main(){
68     int a,b,c;
69     while(scanf("%d",&n) != EOF){
70         Init();
71         for(int i = 1; i <= n; ++i){
72             scanf("%d",&a);
73             while(a--){
74                 scanf("%d%d",&b,&c);
75                 Add_edge(i,(b - 1) * 12 + c);
76             }
77         }
78         printf("%d\n",Hungary());
79     }
80     return 0;
81 }

 

posted @ 2014-11-04 17:42  Naturain  阅读(147)  评论(0编辑  收藏  举报