Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

呵呵!原来就是二分图的匹配问题好简单!

Posted by chenxuan123456789 at 2012-08-10 14:42:43 on Problem 1422
#include <stdio.h>
#include <string.h>
#define size 150
int g[size][size];
int used[size];
int match[size];
int n,m;
int dfs(int x)
{
	int i;
	for(i=1;i<=n;i++)
	{
		if(!used[i]&&g[x][i])
		{
			used[i]=1;
			if(match[i]==-1||dfs(match[i]))
			{
				match[i]=x;
				return 1;
			}
		}
	}
	return 0;
}
int main()
{
	int cases,i,a,b,res;
	scanf("%d",&cases);
	while(cases--)
	{
		scanf("%d",&n);
		scanf("%d",&m);
		memset(g,0,sizeof(g));
		memset(match,-1,sizeof(match));
		for(i=1;i<=m;i++)
		{
			scanf("%d %d",&a,&b);
			g[a][b]=1;
		}
		res=0;
		for(i=1;i<=n;i++)
		{
			memset(used,0,sizeof(used));
			if(dfs(i))
				res++;
		}
		printf("%d\n",n-res);
	}
	return 1;
}


Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator