说明:注意cin会TLE,使用scanf读。vs2013使用更安全的scanf_s代替scanf。
源代码:
#include <iostream>
#include <fstream>
using namespace std;
int cases;
int n, p;
int num;
int map[110][310];
int student;
int result,link[310];
bool visit[310];
bool dfs(int x)
{
for (int y = 1; y <= n; y++)
{
if (map[x][y] == 1 && !visit[y])
{
visit[y] = true;
if (link[y] == 0 || dfs(link[y]))
{
link[y] = x;
return true;
}
}
}
return false;
}
void search()
{
result = 0;
memset(link,0,sizeof(link));
for (int x = 1; x <= p; x++)
{
memset(visit,false,sizeof(visit));
if (dfs(x))
result++;
}
}
int main()
{
//ifstream in("input.txt");
scanf_s("%d",&cases);
for (int i = 1; i <= cases; i++)
{
scanf_s("%d%d", &p, &n);
memset(map, 0, sizeof(map));
for (int j = 1; j <= p; j++)//P courses : from 1 to p
{
scanf_s("%d", &num);
for (int k = 1; k <= num; k++)
{
scanf_s("%d", &student);
map[j][student] = 1;
}
}
search();
if (result == p)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}//end cases
//system("pause");
return 0;
}