HDU 1083 二分图匹配

来源:互联网 发布:大数据和云计算的关系 编辑:程序博客网 时间:2024/05/19 04:27

建立二分图,x集合是课程,y集合是学生。我们求最大匹配数,如果最大匹配数等于|x|那么每个课程都可以找到一个代表,反之找不到

#pragma warning(disable:4996);#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <cmath>#include <queue>#include <new>#include <set>#include <map>using namespace std;typedef long long int LL;const double pi = (acos(-1));const int maxn = 1000;int g[maxn][maxn], used[maxn], link[maxn];int nx, ny;bool dfs (int u) {for (int v = 1; v <= ny; v++) {if (g[u][v] && !used[v]) {used[v] = 1;if (link[v] == -1 || dfs (link[v])) {link[v] = u;return true;}}}return false;}int maxmatch () {int res = 0;memset (link, -1, sizeof (link));for (int i = 1; i <= nx; i++) {memset (used, 0, sizeof (used));if (dfs (i)) res++;}return res;}int main(){//freopen("D:\\Test_in.txt", "r", stdin);//freopen("D:\\Test_out.txt", "w", stdout);int T;scanf ("%d", &T);while (T--) {scanf ("%d%d", &nx, &ny);memset (g, 0, sizeof (g));for (int i = 1; i <= nx; i++) {int n;scanf ("%d", &n);for (int j = 0; j < n; j++) {int a;scanf ("%d", &a);g[i][a] = 1;}}int ans = maxmatch ();if (ans == nx) printf ("YES\n");else printf ("NO\n");}return 0;}


0 0
原创粉丝点击