[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部的点: 每个任务就 ...
随机推荐
- Cracking the coding interview--Q1.7
原文 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are s ...
- 【HDOJ】1732 Push Box
BFS.使用当前结点位置以及三个箱子的位置作为状态. #include <iostream> #include <cstdio> #include <cstring> ...
- 2015第24周五Spring的AOP
AOP(面向方面编程:Aspect Oriented Programing)和IoC一样是Spring容器的内核,声明式事务的功能在此基础上开花结果.但AOP的应用场合是受限的,它一般只适合于那些具有 ...
- This manual page is part of Xcode Tools version 5.0
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/xcodebuild.1.ht ...
- (转)Linux下apache限速和限制同一IP连接数的实现
单位有一台DELL的服务器,4核双CPU,4G内存,1TB的存储空间,闲来无事,申请了域名http://www.zxzy123.cn,做了个网站,本以为用这样的配置做个下载站是绰绰有余了,没想到上线没 ...
- [置顶] Android访问控制系统测试与评估
5.1实验方案 通过以上章节,本文阐述了目前Android平台上的恶意软件以“隐私窃取”和“恶意扣费”类为主,本研究课题访问控制的目标也正是阻止恶意软件“隐私窃取”和“恶意扣费”的行为,因此,本实验方 ...
- HDU2841 Visible Trees (容斥原理)
主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2841 题意: 一个人在(0,0)点,然后前面有一个m*n的格子 ,每一个格子的节点上有一棵树.问这个人 ...
- Duanxx的技术问题:word界面显示模糊
今天打开word时,出现了word打开失败的现象,并且word的界面显示特别的模糊,找了好半天,才解决,问题见下图: 解决方式: 在word的文件->选项,这里面找到显示,然后勾选:禁用硬件图形 ...
- CAsyncSocket
本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 它是一个异步非阻塞Socket封装类,CAsyncSocket::Create()有一个参数指明了你想要处理哪些Socket事 ...
- (原创)android4.4沉浸式标题栏
趁着清明节的闲工夫,把我的百年不升级一次系统的红米note手机升级到了miuiv6的系统,早就听说android4.4的系统有沉浸式标题栏,一直没有体验过.这次终于有机会了.看了几个手机上常用的应用都 ...