匈牙利算法...与NYOJ239一模一样的板子题
/*
qq:1239198605
ctgu_yyf
*/
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
int n,m;//行数 组数
vector<int>boy[520];
bool use[520];
int father[520];
int match(int cur)
{
for(int i=0;i<boy[cur].size();i++)
{
if(!use[boy[cur].at(i)])
{
use[boy[cur].at(i)]=1;
if(father[boy[cur].at(i)]==-1||match(father[boy[cur].at(i)]))
{
father[boy[cur].at(i)]=cur;
return 1;
}
}
}
return 0;
}
int hungary()
{
int count=0;
memset(father,-1,sizeof(father));
for(int i=1;i<=n;i++)
{
memset(use,0,sizeof(use));
count=count+match(i);
}
return count;
}
int main()
{
ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--)
{
cin>>n>>m;
for(int i=1;i<=n;i++)
boy[i].clear();
int start,end;
for(int i=0;i<m;i++)
{
cin>>start>>end;
boy[start].push_back(end);
}
int ans=hungary();
cout<<ans<<endl;
}
return 0;
}