refer:http://blog.csdn.net/hackbuteer1/article/details/7398008
http://poj.org/problem?id=1469
#include <iostream>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <stdio.h>
using namespace std;
bool g[110][310];
bool visit[310];
int match[310];
int p, n;
bool dfs(int u)
{
for(int i = 1; i <= n; i++){
if(g[u][i] && !visit[i]){
visit[i] = true;
if(match[i] == -1 || dfs(match[i])){
match[i] = u;
return true;
}
}
}
return false;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &p, &n);
for(int i = 1; i <= p; i++){
for(int j = 1; j <= n; j++){
g[i][j] = false;
}
}
for(int i = 1; i <= n; i++)
match[i] = -1;
bool flag = false;
for(int i = 1; i <= p; i++) //input
{
int times;
scanf("%d", ×);
if(times == 0) flag = true;
while(times--){
int a;
scanf("%d", &a);
g[i][a] = true;
}
}
if(!flag){
int ans = 0;
for(int i = 1; i <= p; i++){
memset(visit, false, sizeof(visit));
if(dfs(i)) ans++;
}
if(ans == p) puts("YES");
else puts("NO");
}
else
puts("NO");
}
return 0;
}
http://acm.hdu.edu.cn/showproblem.php?pid=2444
#include <iostream>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <stdio.h>
using namespace std;
#define white 1
#define black 0
#define none -1
int n;
bool g[220][220];
int color[220];
bool visite[220];
bool DFS(int u)
{
for(int i = 0; i <= n; i++)
{
if(g[u][i])
{
if(color[i] == none)
{
color[i] = 1 - color[u];
if(DFS(i) == true) return true;
}
else if(color[u] == color[i]) return true;
}
}
return false;
}
bool isbigr()
{
memset(color, -1, sizeof(color));
color[1] = white;
//printf("%d %d", color[1], color[3]);
if(DFS(1)) return true;
return false;
}
bool match(int u)
{
for(int i = 1; i <= n; i++)
{
if(g[u][i] && !visite[i])
{
visite[i] = true;
if(color[i] == -1 || match(color[i]))
{
color[i] = u;
return true;
}
}
}
return false;
}
int main()
{
int m;
while(scanf("%d %d", &n, &m) != -1)
{
for(int i = 0; i <= n; i++)
for(int j = 0; j <= n; j++)
g[i][j] = false;
while(m--)
{
int a, b;
scanf("%d %d", &a, &b);
g[a][b] = true;
}
if(isbigr()) puts("No");
else
{
for(int i = 0; i <= n; i++)
color[i] = -1;
int ans = 0;
for(int i = 1; i<= n; i++)
{
memset(visite, false, sizeof(visite));
if(match(i)) ans++;
}
printf("%d\n", ans);
}
}
}