UVa live6492Welcome Party(二分最大匹配之最小点覆盖)

题目地址:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4503

由于外国网站速度较慢,所以给出题目内容吧。。

For many summers, the Agile Crystal Mining company ran an internship program for students. They
greatly valued interns' ability to self-organize into teams. So as a get-to-know-you activity during
orientation, they asked the interns to form teams such that all members of a given team either have
rst names beginning with the same letter, or last names beginning with the same letter. To make it
interesting, they asked the interns to do this while forming as few teams as possible.
As an example, one year there were six interns: Stephen Cook, Vinton Cerf, Edmund Clarke, Judea
Pearl, Sha Goldwasser, and Silvio Micali. They were able to self-organize into three teams:
Stephen Cook, Vinton Cerf, and Edmund Clarke (whose last names all begin with C)
Sha Goldwasser and Silvio Micali (whose rst names begin with S)
Judea Pearl (not an interesting group, but everyone's rst name in this group starts with J)
As a historical note, the company was eventually shut down due to a rather strange (and illegal)
hiring practice|they refused to hire any interns whose last names began with the letter S, T, U, V, W,
X, Y, or Z. (First names were not subject to such a whim, which was fortunate for our friend Vinton
Cerf.)
Input
Each year's group of interns is considered as a separate trial. A trial begins with a line containing
a single integer N , such that 1 N 300, designating the number of interns that year. Following
that are N lines|one for each intern|with a line having a rst and last name separated by one
space. Names will not have any punctuation, and both the rst name and last name will begin with
an uppercase letter. In the case of last names, that letter will have an additional constraint that it be
in the range from `A' to `R' inclusive. The end of the input is designated by a line containing the value
`0'. There will be at most 20 trials.
Output
For each trial, output a single integer, k, designating the minimum number of teams that were necessary.
Sample Input
6
Stephen Cook
Vinton Cerf
Edmund Clarke
Judea Pearl
Shafi Goldwasser
Silvio Micali
9
Richard Hamming
Marvin Minskey
John McCarthy
Edsger Dijkstra
Donald Knuth
Michael Rabin
John Backus
Robert Floyd
Tony Hoare
0
Sample Output
3
6

这个题当时是在ACdream群赛里碰到的,当时根本不知道二分匹配为何物,以为这题是技巧题。。于是当时想了好长时间也没想出来怎么做。。最近学了二分匹配突然想起来了这题!没错!!就是一道二分匹配最小点覆盖题,而且几乎是纯模板题。代码如下:

#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 mp[30][30], vis[30], link[30], n;
int dfs(int a)
{
    int i;
    for(i=0;i<26;i++)
    {
        if(!vis[i]&&mp[a][i])
        {
            vis[i]=1;
            if(link[i]==-1||dfs(link[i]))
            {
                link[i]=a;
                return 1;
            }
        }
    }
    return 0;
}
void hungary()
{
    int i, ans=0;
    memset(link,-1,sizeof(link));
    for(i=0;i<26;i++)
    {
        memset(vis,0,sizeof(vis));
        if(dfs(i))
            ans++;
    }
    printf("%d\n",ans);
}
int main()
{
    int m;
    char s1[50], s2[50];
    while(scanf("%d",&m)!=EOF&&m)
    {
        memset(mp,0,sizeof(mp));
        while(m--)
        {
            scanf("%s %s",s1,s2);
            mp[s1[0]-'A'][s2[0]-'A']=1;
        }
        hungary();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值