[UVALive] 6492 Welcome Party(最小点覆盖)

                  6492 Welcome Party


  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

 

题解:因为first name 或last name的第一个字母相同的可以划分为一组,求最少划分的小组数。所以可以把所有first name的第一个字母划分为X集合,last name的第一个字母划分为Y集合,每个人的xi向yi连边,那本题就转化为求二分图的最小点覆盖。因为二分图最小点覆盖=最大匹配数,所以求一下最大匹配就可以了。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<string.h>
 4 #include<algorithm>
 5 #include<math.h>
 6 #include<stdbool.h>
 7 #include<time.h>
 8 #include<stdlib.h>
 9 #include<set>
10 #include<map>
11 #include<stack>
12 #include<queue>
13 #include<vector>
14 using namespace std;
15 #define clr(x,y)    memset(x,y,sizeof(x))
16 #define sqr(x)      ((x)*(x))
17 #define rep(i,a,b)  for(int i=(a);i<=(b);i++)
18 #define LL          long long
19 #define INF         0x3f3f3f3f
20 #define A           first
21 #define B           second
22 #define PI          3.14159265358979323
23 const int N=300+131;
24 int n,k,f[N],g[N][N],link[N];
25 char a[100],b[100];
26 
27 void init()
28 {
29     clr(f,0);
30     clr(g,0);
31     clr(link,-1);
32 }
33 
34 bool find(int x)
35 {
36     for(int i=0;i<26;i++) {
37         if(!f[i] && g[x][i]) {
38             f[i]=1;
39             if(link[i]==-1 || find(link[i])) {
40                 link[i]=x;
41                 return true;
42             }
43         }
44     }
45     
46     return false;
47 }
48 
49 int hungary()
50 {
51     int ans=0;
52     for(int i=0;i<26;i++) {
53         clr(f,0);
54         if(find(i)) ans++;
55     }
56     return ans;
57 }
58 
59 int main()
60 {
61     int u,v;
62     
63     while(~scanf("%d",&n)) {
64         if(!n) break;
65         init();
66         getchar();
67         while(n--) {
68             scanf("%s%s",a,b);
69             g[a[0]-'A'][b[0]-'A']=1;
70         }
71         printf("%d\n",hungary());
72     }
73     
74     
75     return 0;
76 }

 

posted @ 2015-03-30 20:55  SXISZERO  阅读(517)  评论(0编辑  收藏  举报