# include<stdio.h>
# include<string.h>
# include<list>
# include<vector>
using namespace std;
vector<int> s[505];
int vis[505], link[505];
bool get_num(int i)
{
for(int j = 0; j < s[i].size(); j++) //指向第一个元素和最后一个元素
{
int t = s[i][j];
if(vis[t] == 0) //用*取值
{
vis[t] = 1;
if(link[t] == 0 || get_num(link[t]) )
{
link[t] = i;
return true;
}
}
}
return false;
}
int main()
{
int t, n, k;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &k);
int a, b;
for(int i = 1; i <= n; i ++) //用之前清空,以免错误和忘记
{
s[i].clear();
}
for(int i = 0; i < k; i ++ )
{
scanf("%d%d", &a, &b);
s[a].push_back(b); //链表中放元素
}
memset(link, 0, sizeof(link));
int num=0;
for(int i = 1; i <= n; i ++)
{
memset(vis, 0, sizeof(vis)); //每次都必须初始化
if(get_num(i))
num++;
}
printf("%d\n",num);
}
return 0;
}
用vector存图 + 二分匹配
最新推荐文章于 2024-01-11 08:30:00 发布