您的位置:首页 > 编程语言 > C语言/C++

HDU 2063过山车(二分最大匹配之最大匹配)

2014-06-06 20:51 435 查看
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2063

水题。。纯模板题。。不多说。。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
int n, vis[600], head[600], link[600], cnt;
struct node
{
int u, v, next;
} edge[1100];
void add(int u, int v)
{
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
int dfs(int u)
{
int i;
for(i=head[u]; i!=-1; i=edge[i].next)
{
int x=edge[i].v;
if(!vis[x])
{
vis[x]=1;
if(link[x]==-1||dfs(link[x]))
{
link[x]=u;
return 1;
}
}
}
return 0;
}
void hungary()
{
int i, ans=0;
memset(link,-1,sizeof(link));
for(i=1; i<=n; i++)
{
memset(vis,0,sizeof(vis));
if(dfs(i))
ans++;
}
printf("%d\n",ans);
}
int main()
{
int t, a, b, m;
while(scanf("%d",&t)!=EOF&&t)
{
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
cnt=0;
while(t--)
{
scanf("%d%d",&a,&b);
add(a,b);
}
hungary();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息