题目地址: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; }