题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=237
此题大意为:有个n行n列的阵列,然后里面有些格子里有怪物,某个人一次能消灭掉某一行或某一列全部的怪物,问最少需要多少次才能消灭完;
//////////////////////////////////////////////////////////////
此题为简单的二分图最大匹配题, 最小顶点覆盖=二分图最大匹配
需要注意的是存图的时候不要用邻接矩阵,否则会超时,我用的是邻接表,顺便练下链表的操作;
////////////////////////////
#include <stdio.h>
#include <string.h>
#include <malloc.h>
int flag[1000];
int res[1000];
int n,v;
int num;
struct node
{
int n;
node *next;
}a[1000];
void add(int u,int b)//向邻接表中增加边
{
node *p;
p=&a[u];
while(p->next)
p=p->next;
p->next=(node *)malloc(sizeof(node));
p=p->next;
p->n=b;
p->next=NULL;
}
int fin(int u) //匈牙利算法主函数
{
node *p=&a[u];
p=p->next;
while(p)
{
if(flag[p->n]==0)
{
flag[p->n]=1;
if(res[p->n]==0||fin(res[p->n]))
{
res[p->n]=u;
return 1;
}
}
p=p->next;
}
return 0;
}
void execute()
{
scanf("%d%d",&n,&v);
int u,b;
int i;
memset(a,0,sizeof(a));
node *p;
for(i=0;i<v;i++)
{
scanf("%d%d",&u,&b);
add(u,b);
}
int ans=0;
memset(res,0,sizeof(res));
for(i=1;i<=n;i++)
{
memset(flag,0,sizeof(flag));
if(fin(i))
ans++;
}
printf("%d\n",ans);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
execute();
return 0;
}