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

二分图匹配

时间:2015-05-14 23:17:04      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

http://poj.org/problem?id=1469

匈牙利算法

邻接矩阵版

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <vector>
 4 using namespace std;
 5 
 6 const int maxNodes = 405;
 7 
 8 int G[110][310];
 9 
10 int num_left;
11 int num_right;
12 
13 int checked[310];
14 int matching[310];
15 
16 bool dfs(int u)
17 {
18     for(int v = 0; v < num_right; v++)
19     {
20         if(G[u][v] == 1 && !checked[v])
21         {
22             checked[v] = true;
23             if(matching[v] == -1 || dfs(matching[v]))
24             {
25                 matching[v] = u;
26                 return true;
27             }
28         }
29     }
30     return false;
31 }
32 
33 int hungarian()
34 {
35     memset(matching, -1, sizeof(matching));
36     int ans = 0;
37 
38     for(int u = 0; u < num_left; u++)
39     {
40         memset(checked, 0, sizeof(checked));
41         if(dfs(u))
42         {
43             ans++;
44         }
45     }
46     return ans;
47 }
48 
49 int main()
50 {
51     int n;
52     scanf_s("%d", &n);
53     while(n--)
54     {
55         memset(G, 0, sizeof(G));
56 
57         scanf_s("%d%d", &num_left, &num_right);
58         bool flag = true;
59         for(int u = 0; u < num_left; u++)
60         {
61             int t;
62             scanf_s("%d", &t);
63             if(t == 0)
64             {
65                 flag = false;
66             }
67             for(int j = 0; j < t; j++)
68             {
69                 int v;
70                 scanf_s("%d", &v);
71                 G[u][v-1] = 1;
72             }
73         }
74 
75         if(!flag)
76         {
77             cout<<"NO"<<endl;
78         }
79         else
80         {
81             int res = hungarian();
82             if(num_left == res)
83             {
84                 cout<<"YES"<<endl;
85             }
86             else
87             {
88                 cout<<"NO"<<endl;
89             }
90         }    
91     }
92 }

邻接表版

技术分享
  1 #include <iostream>
  2 #include <vector>
  3 #include <cstdio>
  4 using namespace std;
  5 
  6 const int maxNodes = 405;
  7 
  8 struct Edge
  9 {
 10     int from;
 11     int to;
 12     int weight;
 13 
 14     Edge(int f, int t, int w):from(f), to(t), weight(w){}
 15 };
 16 
 17 vector<int> G[maxNodes];
 18 vector<Edge> edges;
 19 
 20 int num_nodes;
 21 int num_left;
 22 int num_right;
 23 int num_edges;
 24 
 25 int checked[maxNodes];
 26 int matching[maxNodes];
 27 
 28 bool dfs(int u)
 29 {
 30     vector<int>::iterator it;
 31     for(it = G[u].begin(); it != G[u].end(); it++)
 32     {
 33         int v = *it;
 34         if(!checked[v])
 35         {
 36             checked[v] = true;
 37             if(matching[v] == -1 || dfs(matching[v]))
 38             {
 39                 matching[v] = u;
 40                 return true;
 41             }
 42         }
 43     }
 44     return false;
 45 }
 46 
 47 int hungarian()
 48 {
 49     memset(matching, -1, sizeof(matching));
 50     int ans = 0;
 51 
 52     for(int u = 0; u < num_left; u++)
 53     {
 54         memset(checked, 0, sizeof(checked));
 55         if(dfs(u))
 56         {
 57             ans++;
 58         }
 59     }
 60     return ans;
 61 }
 62 
 63 int main()
 64 {
 65     int n;
 66     scanf_s("%d", &n);
 67     while(n--)
 68     {
 69         edges.clear();
 70         for(int i = 0; i < maxNodes; i++)
 71         {
 72             G[i].clear();
 73         }
 74         bool flag = true;
 75         scanf_s("%d%d", &num_left, &num_right);
 76         for(int u = 0; u < num_left; u++)
 77         {
 78             int t;
 79             scanf_s("%d", &t);
 80             if(t == 0)
 81             {
 82                 flag = false;
 83             }
 84             for(int j = 0; j < t; j++)
 85             {
 86                 int v;
 87                 scanf_s("%d", &v);
 88                 edges.push_back(Edge(u, v-1, 1));
 89                 G[u].push_back(v-1);
 90             }
 91         }
 92         
 93         if(flag)
 94         {
 95             if(num_left == hungarian())
 96             {
 97                 cout<<"YES"<<endl;
 98             }
 99             else
100             {
101                 cout<<"NO"<<endl;
102             }
103         }
104         else
105         {
106             cout<<"NO"<<endl;
107         }
108     }
109 }
View Code

 

二分图匹配

标签:

原文地址:http://www.cnblogs.com/ygw0616/p/4504522.html

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