标签:
题目链接:http://poj.org/problem?id=1469
题意:最大匹配学生与课程数。
解法:ans == 学生数量 YES else NO
代码:
#include <stdio.h>
#include <ctime>
#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
using namespace std;
int n, m, num, k;
int p[310][310];
int book[310];
int match[310];
int t;
bool vis[310];
bool dfs(int u)
{
int i;
for (i = 1; i <= m; i++)
{
if (book[i] == 0 && p[u][i] == 1)
{
book[i] = 1;
if (match[i] == 0 || dfs(match[i]))
{
match[i] = u;
return true;
}
}
}
return false;
}
int main()
{
scanf("%d",&t);
while (t--)
{
memset(p, 0, sizeof(p));
memset(match, 0, sizeof(match));
memset(vis,false,sizeof(vis));
scanf("%d%d",&n,&m);
for (int i = 1; i <= n; i++)
{
scanf("%d",&num);
for (int j = 1; j <= num; j++)
{
scanf("%d",&k);
p[i][k] = 1;
//p[num][m] = 1;
}
}
int ans = 0;
for (int i = 1; i <= n; i++)
{
memset(book, 0, sizeof(book));
if (dfs(i))
ans++;
}
if (ans == n) puts("YES");
else puts("NO");
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/u014427196/article/details/46491871