Skip to content

Instantly share code, notes, and snippets.

@lmatt-bit
Created November 23, 2010 14:15
Show Gist options
  • Select an option

  • Save lmatt-bit/711819 to your computer and use it in GitHub Desktop.

Select an option

Save lmatt-bit/711819 to your computer and use it in GitHub Desktop.
poj 1469
#include <cstdio>
#include <cstring>
#define NUM 501
int grid[NUM][NUM];
int used[NUM];
int path[NUM];
bool dfs(int x, int n)
{
for(int i = 1; i <= n; i++)
{
if(used[i] == 0 && grid[x][i])
{
used[i] = 1;
if(path[i] == 0 || dfs(path[i], n))//path[i] == 0 保证了第一条边和最后一条边为未匹配边
{ //dfs(path[i], n)实现了交替路径
path[i] = x;
return true;
}
}
}
return false;
}
int match(int p, int s)
{
memset(path, 0, sizeof(path));
int sum = 0;
for(int i = 1; i <= p; i++)
{
memset(used, 0, sizeof(used));
if(dfs(i, s)) sum++;
}
return sum;
}
int main()
{
int n;
int p, s, sc;
int st;
scanf("%d", &n);
while(n--)
{
scanf("%d%d", &p, &s);
memset(grid, 0, sizeof(grid));
for(int i = 1; i <= p; i++)
{
scanf("%d", &sc);
for(int j = 1; j <= sc; j++)
{
scanf("%d", &st);
grid[i][st] = 1;
}
}
if(match(p, s) == p) printf("YES\n");
else printf("NO\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment