[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连边,那本题就转化为求二分图的最小点覆盖。因为二分图最小点覆盖=最大匹配数,所以求一下最大匹配就可以了。
#include<cstdio>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<stdbool.h>
#include<time.h>
#include<stdlib.h>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
#define clr(x,y) memset(x,y,sizeof(x))
#define sqr(x) ((x)*(x))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define LL long long
#define INF 0x3f3f3f3f
#define A first
#define B second
#define PI 3.14159265358979323
const int N=+;
int n,k,f[N],g[N][N],link[N];
char a[],b[]; void init()
{
clr(f,);
clr(g,);
clr(link,-);
} bool find(int x)
{
for(int i=;i<;i++) {
if(!f[i] && g[x][i]) {
f[i]=;
if(link[i]==- || find(link[i])) {
link[i]=x;
return true;
}
}
} return false;
} int hungary()
{
int ans=;
for(int i=;i<;i++) {
clr(f,);
if(find(i)) ans++;
}
return ans;
} int main()
{
int u,v; while(~scanf("%d",&n)) {
if(!n) break;
init();
getchar();
while(n--) {
scanf("%s%s",a,b);
g[a[]-'A'][b[]-'A']=;
}
printf("%d\n",hungary());
} return ;
}
[UVALive] 6492 Welcome Party(最小点覆盖)的更多相关文章
- ACM/ICPC 之 机器调度-匈牙利算法解最小点覆盖集(DFS)(POJ1325)
//匈牙利算法-DFS //求最小点覆盖集 == 求最大匹配 //Time:0Ms Memory:208K #include<iostream> #include<cstring&g ...
- 【POJ 3041】Asteroids (最小点覆盖)
每次选择清除一行或者一列上的小行星.最少选择几次. 将行和列抽象成点,第i行为节点i+n,第j列为节点j,每个行星则是一条边,连接了所在的行列. 于是问题转化成最小点覆盖.二分图的最小点覆盖==最大匹 ...
- POJ 2226 最小点覆盖(经典建图)
Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8881 Accepted: 3300 Desc ...
- nyoj 237 游戏高手的烦恼 二分匹配--最小点覆盖
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=237 二分匹配--最小点覆盖模板题 Tips:用邻接矩阵超时,用数组模拟邻接表WA,暂时只 ...
- [USACO2005][POJ2226]Muddy Fields(二分图最小点覆盖)
题目:http://poj.org/problem?id=2226 题意:给你一个字符矩阵,每个位置只能有"*"或者“.",连续的横着或者竖的“*"可以用一块木 ...
- POJ3041Asteroids(最小点覆盖+有点小抽象)
Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18289 Accepted: 9968 Descri ...
- hdu 1054 最小点覆盖
Sample Input 4 0:(1) 1 1:(2) 2 3 2:(0) 3:(0) 5 3:(3) 1 4 2 1:(1) 0 2:(0) 0:(0) 4:(0) Sample Output ...
- POJ2226 Muddy Fields(二分图最小点覆盖集)
题目给张R×C的地图,地图上*表示泥地..表示草地,问最少要几块宽1长任意木板才能盖住所有泥地,木板可以重合但不能盖住草地. 把所有行和列连续的泥地(可以放一块木板铺满的)看作点且行和列连续泥地分别作 ...
- POJ1325 Machine Schedule(二分图最小点覆盖集)
最小点覆盖集就是在一个有向图中选出最少的点集,使其覆盖所有的边. 二分图最小点覆盖集=二分图最大匹配(二分图最大边独立集) 这题A机器的n种模式作为X部的点,B机器的m种模式作为Y部的点: 每个任务就 ...
随机推荐
- 高性能的EMI滤波器及其小型化设计技术
1 EMI滤波器的常见问题及发展趋势首先介绍了影响EMI滤波器性能/体积的因素及EMI滤波器的常见问题:低频传导发射高.高频传导/辐射发射高.体积大,从而分析出EMI滤波器的发展趋势为高性能和小体积, ...
- BZOJ3401: [Usaco2009 Mar]Look Up 仰望
3401: [Usaco2009 Mar]Look Up 仰望 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 87 Solved: 58[Submit ...
- 带’*’号字符串的匹配
目标: 判断源字符串中是否含有指定子串,子串可能会有*号通配符. 初步测试没问题.记录下来.后面要是有问题再来纠正. #include <string> using namespace s ...
- java中文乱码解决之道(六)—–javaWeb中的编码解码
在上篇博客中LZ介绍了前面两种场景(IO.内存)中的java编码解码操作,其实在这两种场景中我们只需要在编码解码过程中设置正确的编码解码方式一般而言是不会出现乱码的.对于我们从事java开发的人而言, ...
- .Net词汇表中常见缩略语汇总
.Net中存在大量的专业词汇(详细列表,请参考:Visual Studio 和 .NET Framework 词汇表),其中很多词汇常常采用缩略语的形式被大量使用. 在阅读.Net书籍或网络资料时,便 ...
- hdu 4277 USACO ORZ(dfs+剪枝)
Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pasture ...
- windows下配置caffe(环境:win7+vs2013+opencv3.0)
说明:大部分转载于initialneil的大作Caffe + vs2013 + OpenCV in Windows Tutorial (I) – Setup 准备工作: 1.下载CUDA7.5: ht ...
- Spark的日志配置
在測试spark计算时.将作业提交到yarn(模式–master yarn-cluster)上,想查看print到控制台这是imposible的.由于作业是提交到yarn的集群上,so 去yarn集群 ...
- iOS中block实现的探究
[0. Brief introduction of block] Block是iOS4.0+ 和Mac OS X 10.6+ 引进的对C语言的扩展,用来实现匿名函数的特性. 用维基百科的话来说,Blo ...
- MySQL数据库的环境及简单操作
***********************************************声明*************************************************** ...