Google Code Jam 2016 Round 1B Problem C. Technobabble
题目链接:https://code.google.com/codejam/contest/11254486/dashboard#s=p2
大意是教授的学生每个人在纸条上写一个自己的topic,每个topic由两个单词组成,那么纸上留下了若干个topic。topic分为 "faked" 或者 "un-faked",所谓faked意思就是其实这个topic的第一个和第二个单词都是随便由之前纸上已经有的topic来构成的,当然,不能是由原来的同一个topic组成,这样就重复了。现在给出这些topic,原来的顺序不知道,试问,最多可能有多少个topic属于faked。
小数据是最多18个topic,大数据是最多1000个。
首先肯定不能暴力枚举顺序,这样复杂度太高了。小数据的做法是可以二进制标记,哪些topic属于faked,那么不属于faked的那些把它们的两个单词都存进map,检查枚举的faked的topic是否两个单词都在map中存在,更新答案。
大数据的做法其实是规约到二分图最小边覆盖的模型上。如果我们把每个topic的第一个和第二个单词分开,就构成了一个二分图,每个topic其实对应了这张二分图上的一条边。现在的问题就是寻找最少的边集(也就是un-faked topic集)使得所有的点都被边集中的至少一条边覆盖到。
这就意味着所有的单词都会被选出来的topic覆盖到,也就做到了题目中的要求,此时只需要再将topic个数n减去求出来的最小边覆盖(un-faked topic数)就得到了最大的faked topic个数了。
最小边覆盖的计算方式是二分图的点数(左部+右部)减去最大匹配数。
代码如下:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#include <cassert>
#include <set>
using namespace std; const int N=; bool g[N][N],vis[N];
int nx,ny;
int cx[N],cy[N];
bool dfs(int u){
for (int i=;i<=ny;i++){
if (g[u][i]&&!vis[i]){
vis[i]=true;
if (cy[i]==-||dfs(cy[i])){
cy[i]=u;
cx[u]=i;
return true;
}
}
}
return false;
}
int maxMatch(){
int ret=;
memset(cx,-,sizeof(cx));
memset(cy,-,sizeof(cy));
for (int i=;i<=nx;i++){
if (cx[i]==-){
memset(vis,,sizeof(vis));
ret+=dfs(i);
}
}
return ret;
} string a[N],b[N];
int main () {
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
int T;
cin>>T;
while (T--) {
int n;
cin>>n;
nx=;ny=;
map<string,int>ma,mb;
for (int i=;i<=n;i++) {
cin>>a[i]>>b[i];
if (ma[a[i]]==)
ma[a[i]]=++nx;
if (mb[b[i]]==)
mb[b[i]]=++ny;
}
memset(g,,sizeof g);
for (int i=;i<=n;i++) {
int l=ma[a[i]];
int r=mb[b[i]];
g[l][r]=true;
}
int match=maxMatch();
int minEdgeCover=nx+ny-match;
int ret=n-minEdgeCover;
static int cas=;
cout<<"Case #"<<cas++<<": "<<ret<<endl;
}
return ;
}
Google Code Jam 2016 Round 1B Problem C. Technobabble的更多相关文章
- Google Code Jam 2010 Round 1B Problem B. Picking Up Chicks
https://code.google.com/codejam/contest/635101/dashboard#s=p1 Problem A flock of chickens are runn ...
- Google Code Jam 2010 Round 1B Problem A. File Fix-it
https://code.google.com/codejam/contest/635101/dashboard#s=p0 Problem On Unix computers, data is s ...
- Google Code Jam 2016 Round 1B B
题意:给出两个数字位数相同,分别中间有若干位不知道,用问号表示.现在要求补全这两个数字,使得差值的绝对值最小,多解则取第一个数字的值最小的,再多解就取第二个数字最小的. 分析: 类似数位dp,但是很多 ...
- Google Code Jam 2014 Round 1B Problem B
二进制数位DP,涉及到数字的按位与操作. 查看官方解题报告 #include <cstdio> #include <cstdlib> #include <cstring& ...
- Google Code Jam 2010 Round 1C Problem A. Rope Intranet
Google Code Jam 2010 Round 1C Problem A. Rope Intranet https://code.google.com/codejam/contest/61910 ...
- Google Code Jam 2010 Round 1C Problem B. Load Testing
https://code.google.com/codejam/contest/619102/dashboard#s=p1&a=1 Problem Now that you have won ...
- dp - Google Code jam Qualification Round 2015 --- Problem B. Infinite House of Pancakes
Problem B. Infinite House of Pancakes Problem's Link: https://code.google.com/codejam/contest/6224 ...
- Google Code jam Qualification Round 2015 --- Problem A. Standing Ovation
Problem A. Standing Ovation Problem's Link: https://code.google.com/codejam/contest/6224486/dashbo ...
- Google Code Jam 2010 Round 1A Problem A. Rotate
https://code.google.com/codejam/contest/544101/dashboard#s=p0 Problem In the exciting game of Jo ...
随机推荐
- Modbus软件开发实战指南 之 开发自己的Modbus Poll工具 - 1
在开发Modbus程序的过程中,也可以发现经常需要使用诸如Modbus Poll和Modbus Slave等辅助调试工具, 用于验证MODBUS通讯消息是否正确.但是,Modbus Poll和Modb ...
- 推荐三款日期选择插件(My97DatePicker+jquery.datepicker+Mobiscroll)
1.My97DatePicker 纯原生JS,专注于PC端,支持IE6+:页面上只需要引入WdatePicker.js文件,但是My97DatePicker整个目录是一个整体,最好不要破坏里面的目录结 ...
- Octave Tutorial(《Machine Learning》)之第五课《控制语句和方程及向量化》
第五课 控制语句和方程 For,while,if statements and functions (1)For loop v=zeros(10,1) %initial vectors for i=1 ...
- C#三层构架
三层构架:表示层(即界面层UI)->业务逻辑层(Business logic level)->数据访问层(Database access level) 由于层是一种弱耦合结构,层与层之间的 ...
- 实例PK(Vue服务端渲染 VS Vue浏览器端渲染)
Vue 2.0 开始支持服务端渲染的功能,所以本文章也是基于vue 2.0以上版本.网上对于服务端渲染的资料还是比较少,最经典的莫过于Vue作者尤雨溪大神的 vue-hacker-news.本人在公司 ...
- C—动态内存分配之malloc与realloc的区别
在程序的执行期间分配内存时,内存区域中的这个空间称为堆(heap).还有另一个内存区域,称为堆栈(stack),其中的空间分配给函数的参数和本地变量.在执行完该函数后,存储参数和本地变量的内存空间就会 ...
- css gray,grayscale,css变灰兼容大部分浏览器
css gray,grayscale,css变灰兼容大部分浏览器 html { filter: url("data:image/svg+xml;utf8,<svg xmlns=\'ht ...
- 兔子生娃问题---函数递归应用--c语言实现
事情是这样的:在很久很久以前....有一对兔子,从出生后第 3 个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 兔子的规律为数列:1, 1 ...
- MyBatis快速入门(1):搭建环境和单表映射
一.MyBatis简介 一说起对象关系映射框架,大家第一时间想到的肯定是Hibernate.Hibernate作为一个著名的框架,功能十分强大.我们只需要配置好实体类和数据表之间的关系,Hibe ...
- unity3D插件开发——前篇
Unity3D(以下简称Unity)是今年来非常流行的游戏开发引擎.他不仅有足够和unreal(虚幻)引擎媲美的渲染效果,足够多的平台输出,更为突出的就是Unity本身的编辑器.unity本身除了使用 ...