[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部的点: 每个任务就 ...
随机推荐
- Linux 安装字体
把XP下的字体C:\WINDOWS\FONTS\simsun.ttc(也就是宋体,大小为10M),把他重命名为 simsun.ttf 拷贝simsun.ttf 字体到 /usr/share/fonts ...
- 笔记:java并发实践2
public interface Executor { void execute(Runnable command); } 虽然Executor是一个简单的接口,但它为灵活且强大的异步任务框架提供了基 ...
- HDU_2042——递归反推
Problem Description 你活的不容易,我活的不容易,他活的也不容易.不过,如果你看了下面的故事,就会知道,有位老汉比你还不容易.重庆市郊黄泥板村的徐老汉(大号徐东海,简称XDH)这两年 ...
- 关于java中是引用传递还是值传递的问题!!!经常在笔试中遇到,今天终于弄明白了!
关于JAVA中参数传递问题有两种,一种是按值传递(如果是基本类型),另一种是按引用传递(如果是對象).首先以两个例子开始:1)public class Test2 { public static vo ...
- es6新特性:
http://es6katas.org/ es6+一些新特性,截图如下 对应方法,函数显示相关的数据,如图: 对应方法,函数的例子,如下
- 实现Android 动态载入APK(Fragment or Activity实现)
尊重原创:http://blog.csdn.net/yuanzeyao/article/details/38565345 近期由于项目太大了.导致编译通只是(Android对一个应用中的方法个数貌似有 ...
- Openstack 二次开发之:在windows 环境下编译Openstack-java-sdk
在windows环境下使用maven对openstack-java-sdk进行编译 编译源文件 下载源代码 git clonehttps://github.com/woorea/openstack-j ...
- JAVA学习篇--javaweb之Filter具体解释
在DRP项目中,多次提到了Filter,它攻克了字符集的统一设置以及统一控制简单WebCache,从中我们能够体会到.它给我们带来的优点不不过降低代码量这么简单,它的出现避免了我们每一个页面反复的编写 ...
- Web安全测试之XSS(跨站脚本攻击)
XSS 全称(Cross Site Scripting) 跨站脚本攻击, 是Web程序中最常见的漏洞.指攻击者在网页中嵌入客户端脚本(例如JavaScript), 当用户浏览此网页时,脚本就会在用户的 ...
- 【IOS】IOS高速入门之OC语法
Objective-C 是 C 语言的超集 您还能够訪问标准 C 库例程,比如在stdlib.h和stdio.h中声明的那些例程. Objective-C 还是一种很动态的程序设计语言,并且这样的动态 ...