题目链接: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个数了。

最小边覆盖的计算方式是二分图的点数(左部+右部)减去最大匹配数。

代码如下:

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5. #include <cstring>
  6. #include <cstdio>
  7. #include <math.h>
  8. #include <queue>
  9. #include <stack>
  10. #include <map>
  11. #include <cassert>
  12. #include <set>
  13. using namespace std;
  14.  
  15. const int N=;
  16.  
  17. bool g[N][N],vis[N];
  18. int nx,ny;
  19. int cx[N],cy[N];
  20. bool dfs(int u){
  21. for (int i=;i<=ny;i++){
  22. if (g[u][i]&&!vis[i]){
  23. vis[i]=true;
  24. if (cy[i]==-||dfs(cy[i])){
  25. cy[i]=u;
  26. cx[u]=i;
  27. return true;
  28. }
  29. }
  30. }
  31. return false;
  32. }
  33. int maxMatch(){
  34. int ret=;
  35. memset(cx,-,sizeof(cx));
  36. memset(cy,-,sizeof(cy));
  37. for (int i=;i<=nx;i++){
  38. if (cx[i]==-){
  39. memset(vis,,sizeof(vis));
  40. ret+=dfs(i);
  41. }
  42. }
  43. return ret;
  44. }
  45.  
  46. string a[N],b[N];
  47. int main () {
  48. freopen("in.txt","r",stdin);
  49. freopen("out.txt","w",stdout);
  50. int T;
  51. cin>>T;
  52. while (T--) {
  53. int n;
  54. cin>>n;
  55. nx=;ny=;
  56. map<string,int>ma,mb;
  57. for (int i=;i<=n;i++) {
  58. cin>>a[i]>>b[i];
  59. if (ma[a[i]]==)
  60. ma[a[i]]=++nx;
  61. if (mb[b[i]]==)
  62. mb[b[i]]=++ny;
  63. }
  64. memset(g,,sizeof g);
  65. for (int i=;i<=n;i++) {
  66. int l=ma[a[i]];
  67. int r=mb[b[i]];
  68. g[l][r]=true;
  69. }
  70. int match=maxMatch();
  71. int minEdgeCover=nx+ny-match;
  72. int ret=n-minEdgeCover;
  73. static int cas=;
  74. cout<<"Case #"<<cas++<<": "<<ret<<endl;
  75. }
  76. return ;
  77. }

Google Code Jam 2016 Round 1B Problem C. Technobabble的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Google Code Jam 2016 Round 1B B

    题意:给出两个数字位数相同,分别中间有若干位不知道,用问号表示.现在要求补全这两个数字,使得差值的绝对值最小,多解则取第一个数字的值最小的,再多解就取第二个数字最小的. 分析: 类似数位dp,但是很多 ...

  4. Google Code Jam 2014 Round 1B Problem B

    二进制数位DP,涉及到数字的按位与操作. 查看官方解题报告 #include <cstdio> #include <cstdlib> #include <cstring& ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 用GDB调试程序

    转自:http://blog.csdn.net/haoel/article/details/2879 是一篇从基础讲gdb的博文 用GDB调试程序 GDB概述---- GDB是GNU开源组织发布的一个 ...

  2. 200行自定义异步非阻塞Web框架

    Python的Web框架中Tornado以异步非阻塞而闻名.本篇将使用200行代码完成一个微型异步非阻塞Web框架:Snow. 一.源码 本文基于非阻塞的Socket以及IO多路复用从而实现异步非阻塞 ...

  3. Reverse Words in a String leetcode

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  4. Android 启动模式--任务(Task)--桟 的误区

    Android 启动模式--任务(Task)--桟 的误区 写这篇文章是因为前几天的一次面试,面试官说SingleInstance模式会新建一个桟,而SingleTask不会.首先不说这个对不对(非要 ...

  5. Jmeter正则提取器常用的几种方式

    使用jmeter的同学都知道,jmeter提供了各种各样的提取器,如jsonpath.Beanshell.Xpath.正则等!!! 我们就针对正则提取器如何使用进行说明. 举例说明:假设取sessio ...

  6. Mac下tomcat配置ssl

    最近在搞单点登录CAS,第一步就是需要给tomcat配置证书.但是,第一次配置就遇到了个问题排插了一下午.下面来存一份文档,以备以后遇到. 一.首先准备好环境 java环境:配置好环境变量,找到jdk ...

  7. (8)集合之List,ArrayList,LinkedList

    集合的体系结构 Collection 单列集合的接口 |----List 如果实现了List接口的集合类,具备的特点是有序,可重复 |----Set 如果实现了Set接口的集合类,集合特点无序不可重复 ...

  8. 对VC++6.0爱得深沉(一)安装vc++6.0,支持winXP,win7,win8.1,win10

    [欢迎入坑] 从这里起,我称VC++6.0为小c. 为什么我对小c爱得深沉? 虽然饱受非议,但是,我只想说,我太单纯,小c轻轻松松成功运行在各个win平台,对于我来说她:高速.小巧.便捷.听话.可定制 ...

  9. cuda编程学习4——Julia

    书上的例子编译会有错误,修改一下行即可. __device__ cuComplex(float a,float b):r(a),i(b){} /* ========================== ...

  10. IOS推送--之开发模式测试

    参考文章:http://blog.csdn.net/showhilllee/article/details/8631734#comments 第一步.下载你工程的开发证书 第二步.从钥匙串访问中导出秘 ...