题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=239
把男孩的编号都加上个n,然后用vector,神奇就过了。
注:当二分图的点集中的点数量相同时,搜索一半的点就可以了,如果不相同,标记的时候单向标记,则要搜索所有点,结果要除以2.
代码:
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
int n,k;
vector<int> e[1005];
int v[1005];
int match[1005];
bool dfs(int u)
{
for(int i = 0;i < e[u].size();++i)
{
int t = e[u][i];
if(!v[t])
{
v[t] = 1;
if(match[t] == 0 || dfs(match[t]))
{
match[t] = u;
match[u] = t;
return true;
}
}
}
return false;
}
int main()
{
int _;
scanf("%d",&_);
while(_--)
{
memset(e,0,sizeof(e));
scanf("%d%d",&n,&k);
for(int i = 1;i <= k;++i)
{
int a,b;
scanf("%d%d",&a,&b);
e[a].push_back(b + n);
e[b + n].push_back(a);
}
memset(match,0,sizeof(match));
int ans = 0;
for(int i = 1;i <= n;++i)
{
memset(v,0,sizeof(v));
if(dfs(i))
ans++;
}
printf("%d\n",ans);
}
return 0;
}