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

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

2014-06-04 20:54 465 查看
题目地址: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息