用临街矩阵些超时,改用邻接表,结果对vector忘清零,错了好几次。
http://acm.nyist.net/JudgeOnline/problem.php?pid=239
#include<vector>
#include<stdio.h>
#include<string.h>
using namespace std;
#define maxn 502
vector<int>v[maxn];
int use[maxn],path[maxn];
bool dfs(int x)
{
for(int i=0;i<v[x].size();i++)
{
int e=v[x][i];
if(!use[e])
{
use[e]=1;
if(!path[e]||dfs(path[e]))
{
path[e]=x;
return true ;
}
}
}
return false ;
}
int match(int n)
{
int sum=0;
memset(path,0,sizeof(path));
for(int i=1;i<=n;i++)
{
memset(use,0,sizeof(use));
if(dfs(i)) sum++;
}
return sum;
}
int main()
{
int i,n,m,ncase,a,b;
scanf("%d",&ncase);
while(ncase--)
{
scanf("%d%d",&n,&m);
memset(v,0,sizeof(v));
for(i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
v[a].push_back(b);
}
printf("%d\n",match(n));
}
return 0;
}