http://poj.org/problem?id=3281
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
const int MAX=0x7fffffff;
const int N=510;
int path[N][N];
int l[N];
int n,f,d;
int Bfs()
{
memset(l,-1,sizeof(l));
queue< int >str;
str.push(0);
int k;
l[0]=0;
while(!str.empty())
{
k=str.front();
str.pop();
for(int i=n;i>=0;--i)
{
if(path[k][i]>0&&l[i]==-1)
{
l[i]=l[k]+1;
str.push(i);
}
}
}
//for(int i=0;i<=n;++i)
//printf("( %d %d ) ",i,l[i]);
//cout<<endl;
if(l[n]!=-1)
return true;
return false;
}
int Dfs(int x,int Max)
{
int sum=Max;
if(x==n)
{return Max;}
for(int i=0;i<=n;++i)
{
if(sum==0)
break;
if(l[i]==l[x]+1&&path[x][i]>0)
{
int w=Dfs(i,min(sum,path[x][i]));
path[x][i]-=w;
path[i][x]+=w;
sum-=w;
}
}
return (Max-sum);
}
int main()
{
//freopen("data.txt","r",stdin);
//while(1)
{
scanf("%d %d %d",&n,&f,&d);
int k1,k2;
int w;
memset(path,0,sizeof(path));
for(int i=1;i<=n;++i)
{
path[i][i+n]=1;
scanf("%d %d",&k1,&k2);//cout<<i<<" "<<i+n<<endl;
while(k1--)
{
scanf("%d",&w);
path[w+2*n][i]=1;//cout<<w+2*n<<" "<<i<<endl;
path[0][w+2*n]=1;
}
while(k2--)
{
scanf("%d",&w);
path[i+n][w+2*n+f]=1;//cout<<i+n<<" "<<w+2*n+f<<endl;
path[w+2*n+f][2*n+f+d+1]=1;
}
}
n=2*n+f+d+1;//cout<<"n=== "<<n<<endl;
int ans=0;
while(Bfs())
{
while(1)
{
w=Dfs(0,MAX);
if(w==0)
break;
ans+=w;
}
}
printf("%d\n",ans);
}
return 0;
}