[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 }
时间: 2024-10-03 13:09:57

[UVALive] 6492 Welcome Party(最小点覆盖)的相关文章

UVA-11419 SAM I AM (最小点覆盖)

题目大意:在一个n*m的网格中,有k个目标,现在可以任选一行或列消除在其上的所有目标,求出最少选择次数及选法. 题目分析:经典的最小点覆盖问题,并且输出一个最小点覆盖集.在求出最大匹配之后,以未覆盖的x点进行标记,沿着未覆盖->覆盖->未覆盖->覆盖...的路径标记,最后x中未标记的和y中标记的点构成最小点覆盖集. 代码如下: # include<iostream> # include<cstdio> # include<cstring> # incl

hdu 1054 Strategic Game 二分图最小点覆盖

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054 题意: 给出一个无向图,求最小点覆盖. 思路: 用网络流来做设立一个超级源点和一个超级汇点. 每个点拆成i和i'. 从超级源点向点i连一条边,容量为1. 从i’向超级汇点连一条边,容量为1. 从i向i'连一条边,容量为正无穷. 然后求最小割/2.因为拆点拆成了2个. 也可以用二分图匹配来做,也是求出最大匹配然后/2. 1 #include <bits/stdc++.h> 2 using na

POJ2226 Muddy Fields(二分图最小点覆盖集)

题目给张R×C的地图,地图上*表示泥地..表示草地,问最少要几块宽1长任意木板才能盖住所有泥地,木板可以重合但不能盖住草地. 把所有行和列连续的泥地(可以放一块木板铺满的)看作点且行和列连续泥地分别作为XY部,每一块泥地看作边.这样就构造出了一个二分图. 那么,问题就是在这个二分图中就是选出最少的点覆盖所有的边,即二分图最小点覆盖集,而二分图最小点覆盖集=二分图最大匹配. 1 #include<cstdio> 2 #include<cstring> 3 #include<qu

UVa11419 SAM I AM(构造最小点覆盖)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=27475 [思路] 二分图的最小点覆盖以及构造最小覆盖. 可见:http://www.tuicool.com/articles/jmAnEb [代码] #include<cstdio> #include<cstring> #include<vector> #include<iostream> using namespace st

POJ3041Asteroids(最小点覆盖+然而并不是很理解why)

Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18289   Accepted: 9968 Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K as

二分图匹配 + 最小点覆盖 - Vertex Cover

Vertex Cover Problem's Link Mean: 给你一个无向图,让你给图中的结点染色,使得:每条边的两个顶点至少有一个顶点被染色.求最少的染色顶点数. analyse: 裸的最小点覆盖问题,二分图的最大匹配,直接套模版即可. Time complexity: O(N^2) view code

hdu 1151 或 poj 1422 二分图 最小点覆盖集

最小点覆盖集的裸题,只要“拆点建边”然后求出最大匹配,则:最小点覆盖集的大小 = 点数 - 最大匹配 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int N = 121; 7 const int M = 5000; 8 bool visit[N]; 9 int mark[N]; 10 int head[N]; 11 int

POJ3041 Asteroids【二分图最小点覆盖】

题目链接: http://poj.org/problem?id=3041 题目大意: 有一个N*N的矩阵,有些格子上有障碍物(坐标为(x,y) ),在消除这些障碍物的时候,可以一次性消除 该障碍物同一行所有的障碍物,或是一次性消除该障碍物同一列所有的障碍物.只能选择清理该行或是 清理该列.问:最小进行多少次消除,就可以清理所有的障碍物. 思路: 可以将每一行当做一个点,这样总共有N个点,作为二分图的一边.将每一列当做一个点,这样又有N 个点,作为二分图的另一边.将有障碍物的行点和列点连接起来,每

二分图的最小点覆盖和最大独立集

一.二分图的最小点覆盖 无向图的最小点覆盖,就是选取图中最少的点使得每条边至少有一个端点被选中. 而二分图的最小点覆盖 = 最大匹配. 二.二分图的最大独立集 无向图的最大独立集,就是从无向图中选取尽量多的点,这些点两两不邻接. 而二分图的最大独立集 = 点总数 - 最大匹配. 解析见链接:http://hihocoder.com/problemset/problem/1127