最最基础的二分匹配
//
// main.cpp
// jixun
//
// Created by zhou yi on 14-4-8.
// Copyright (c) 2014年 edward. All rights reserved.
//
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
const int N = 501;
vector<int>map[N];
int cy[N],cx[N];
bool visited[N];
int n;
bool findPath(int u)
{
for (int v = 0; v < map[u].size(); ++ v)
{
if (!visited[map[u][v]])
{
visited[map[u][v]] = 1;
if (cy[map[u][v]] == -1 || findPath(cy[map[u][v]]))
{
cy[map[u][v]] = u;
cx[u] = map[u][v];
return 1;
}
}
}
return 0;
}
int getAns()
{
int ans = 0;
memset(cy, -1, sizeof(cy));
memset(cx, -1, sizeof(cx));
for (int i = 1; i <= n; ++ i)
{
if (cx[i] == -1)
{
memset(visited, 0, sizeof(visited));
ans += findPath(i);
}
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
int m;
scanf("%d %d",&n,&m);
for (int i = 1; i <= n; ++ i)
{
map[i].clear();
}
for (int i = 0; i < m; ++ i)
{
int a,b;
scanf("%d %d",&a,&b);
map[a].push_back(b);
}
printf("%d\n",getAns());
}
return 0;
}