题意:题目就是求二分图最大匹配。
思路:直接使用匈牙利算法,主要还是用来熟悉一下模版。
代码如下:
1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao/ 4 * Last modified : 2014-02-20 22:44 5 * Filename : poj_1274.cpp 6 * Description : 7 * ************************************************/ 8 9 #include <iostream> 10 #include <cstdio> 11 #include <cstring> 12 #include <cstdlib> 13 #include <algorithm> 14 #include <queue> 15 #include <stack> 16 #include <vector> 17 #include <set> 18 #include <map> 19 #define MP(a, b) make_pair(a, b) 20 #define PB(a) push_back(a) 21 22 using namespace std; 23 typedef long long ll; 24 typedef pair<int, int> pii; 25 typedef pair<unsigned int,unsigned int> puu; 26 typedef pair<int, double> pid; 27 typedef pair<ll, int> pli; 28 typedef pair<int, ll> pil; 29 30 const int INF = 0x3f3f3f3f; 31 const double eps = 1E-6; 32 const int LEN = 1010; 33 vector<int> Map[LEN]; 34 int xn, yn, n, vis[LEN], match[LEN]; 35 36 bool dfs(int v){ 37 vis[v] = 1; 38 for(int i=0; i<Map[v].size(); i++){ 39 int u = Map[v][i], w = match[u]; 40 if(w < 0 || (!vis[w] && dfs(w))){ 41 match[v] = u; 42 match[u] = v; 43 return true; 44 } 45 } 46 return false; 47 } 48 49 int hungary(){ 50 int ret = 0; 51 memset(match, -1, sizeof match); 52 for(int i=0; i<n; i++){ 53 if(match[i] < 0){ 54 memset(vis, 0, sizeof vis); 55 if(dfs(i)) ret ++; 56 } 57 } 58 return ret; 59 } 60 61 int main() 62 { 63 // freopen("in.txt", "r", stdin); 64 65 while(scanf("%d%d", &xn, &yn)!=EOF){ 66 for(int i=0; i<LEN; i++) Map[i].clear(); 67 for(int i=0; i<xn; i++){ 68 int tn, b; 69 scanf("%d", &tn); 70 for(int j=0; j<tn; j++){ 71 scanf("%d", &b);b--; 72 Map[i].PB(b+xn); 73 Map[b+xn].PB(i); 74 } 75 } 76 n = xn + yn; 77 int ans = hungary(); 78 printf("%d ", ans); 79 } 80 return 0; 81 }