[codevs1027]姓名与ID

试题描述

有N个人,各自有一个姓名和ID(别名)。每个人的姓名和ID都没有重复。这些人依次进入一间房间,然后可能会离开。过程中可以得到一些信息,告知在房间里的某个人的ID。你的任务是准确地确定每个人的ID。

输入

第一行是整数N,表示N个人,N<=20。

接下来的一行是N个人的ID,用一个空格分隔。

接下来的若干行是过程的记录:一个字母和一个字符串。字母是E、L或M中的一个。E表示进入房间,后面跟的字符串表示进来的人的姓名;L表示离开房间,后面跟的字符串表示离开的人的姓名;M表示回答询问,后面跟的字符串表示:当前用这个ID人在房间里面。

最后一行Q表示结束。

所有的姓名和ID都由不超过20个的小写字母组成。所有姓名都会在记录中出现。

一开始时,房间时空的。

输出

共N行,每行形如:“姓名:ID”,如果ID不能确定,输出???。

按照姓名的字典顺序输出。

输入示例

  1. bigman mangler sinbad fatman bigcheese frenchie capodicapo
  2. E mugsy
  3. E knuckles
  4. M bigman
  5. M mangler
  6. L mugsy
  7. E clyde
  8. E bonnie
  9. M bigman
  10. M fatman
  11. M frenchie
  12. L clyde
  13. M fatman
  14. E ugati
  15. M sinbad
  16. E moriarty
  17. E booth
  18. Q

输出示例

  1. bonnie:fatman
  2. booth:???
  3. clyde:frenchie
  4. knuckles:bigman
  5. moriarty:???
  6. mugsy:mangler
  7. ugati:sinbad

数据规模及约定

见“输入”,还有操作数可以达到 10^5 左右。

题解

首先可以想到姓名和 ID 进行二分图匹配,现在难点在于判断唯一性。

跑完二分图匹配后,我们逐个判断,对于一个姓名 i,我们强制不让它匹配它当前的匹配对象,在此基础上再跑一遍二分图,若还是完全匹配,则说明姓名 i 不能被唯一确定,标记成“???”。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <cctype>
  6. #include <algorithm>
  7. #include <string>
  8. #include <map>
  9. using namespace std;
  10.  
  11. int read() {
  12. int x = 0, f = 1; char c = getchar();
  13. while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
  14. while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
  15. return x * f;
  16. }
  17.  
  18. #define maxn 45
  19. #define maxm 3210
  20. #define oo 2147483647
  21. struct Edge {
  22. int from, to, flow;
  23. Edge() {}
  24. Edge(int _1, int _2, int _3): from(_1), to(_2), flow(_3) {}
  25. } ;
  26. struct Dinic {
  27. int n, m, s, t, head[maxn], next[maxm];
  28. Edge es[maxm];
  29. int hd, tl, Q[maxn], vis[maxn];
  30. int cur[maxn];
  31. void cpy(Dinic& T) {
  32. n = T.n; m = T.m; s = T.s; t = T.t;
  33. memcpy(head, T.head, sizeof(head));
  34. memcpy(next, T.next, sizeof(next));
  35. for(int i = 0; i < m; i++) es[i] = T.es[i];
  36. return ;
  37. }
  38. void init(int nn) {
  39. n = nn; m = 0;
  40. memset(head, -1, sizeof(head));
  41. return ;
  42. }
  43. void AddEdge(int a, int b, int c) {
  44. es[m] = Edge(a, b, c); next[m] = head[a]; head[a] = m++;
  45. es[m] = Edge(b, a, 0); next[m] = head[b]; head[b] = m++;
  46. return ;
  47. }
  48. bool BFS() {
  49. memset(vis, 0, sizeof(vis)); vis[s] = 1;
  50. hd = tl = 0; Q[++tl] = s;
  51. while(hd < tl) {
  52. int u = Q[++hd];
  53. for(int i = head[u]; i != -1; i = next[i]) {
  54. Edge& e = es[i];
  55. if(!vis[e.to] && e.flow) {
  56. vis[e.to] = vis[u] + 1;
  57. Q[++tl] = e.to;
  58. }
  59. }
  60. }
  61. return vis[t] > 1;
  62. }
  63. int DFS(int u, int a) {
  64. if(u == t || !a) return a;
  65. int flow = 0, f;
  66. for(int& i = cur[u]; i != -1; i = next[i]) {
  67. Edge& e = es[i];
  68. if(vis[e.to] == vis[u] + 1 && (f = DFS(e.to, min(a, e.flow)))) {
  69. flow += f; a -= f;
  70. e.flow -= f; es[i^1].flow += f;
  71. if(!a) return flow;
  72. }
  73. }
  74. return flow;
  75. }
  76. int MaxFlow(int ss, int tt) {
  77. s = ss; t = tt;
  78. int flow = 0;
  79. while(BFS()) {
  80. for(int i = 1; i <= n; i++) cur[i] = head[i];
  81. flow += DFS(s, oo);
  82. }
  83. return flow;
  84. }
  85. } sol, sol2, sol3;
  86.  
  87. #define maxp 25
  88. string ID[maxp], name[maxp];
  89. map <string, int> M, M2;
  90. bool S[maxp];
  91. int en[maxn][maxn];
  92. struct Info {
  93. string name, ID;
  94. Info() {}
  95. Info(string _, string __): name(_), ID(__) {}
  96. bool operator < (const Info& t) const { return name < t.name; }
  97. } is[maxp];
  98.  
  99. int main() {
  100. int n = read();
  101. sol.init((n << 1) + 2); int s = (n << 1) + 1, t = s + 1;
  102. for(int i = 1; i <= n; i++) cin >> ID[i];
  103. sort(ID + 1, ID + n + 1);
  104. for(int i = 1; i <= n; i++) M[ID[i]] = i;
  105. int cp = 0;
  106. for(int i = 1; i <= n; i++)
  107. for(int j = 1; j <= n; j++) en[i][j+n] = en[j+n][i] = 1;
  108. while(1) {
  109. char tc[2]; scanf("%s", tc);
  110. if(tc[0] == 'Q') break;
  111. string t; cin >> t;
  112. if(tc[0] == 'E') {
  113. if(!M2.count(t)) M2[t] = ++cp, name[cp] = t;
  114. S[M2[t]] = 1;
  115. }
  116. if(tc[0] == 'L') S[M2[t]] = 0;
  117. if(tc[0] == 'M') {
  118. int p = M[t];
  119. for(int i = 1; i <= n; i++) if(!S[i]) en[i][p+n] = en[p+n][i] = -1;
  120. }
  121. }
  122. int ce = 0;
  123. for(int i = 1; i <= cp; i++)
  124. for(int j = n + 1; j <= (n << 1); j++) if(en[i][j] > 0)
  125. sol.AddEdge(i, j, 1), en[i][j] = ce, ce += 2;
  126. for(int i = 1; i <= cp; i++) sol.AddEdge(s, i, 1);
  127. for(int i = 1; i <= n; i++) sol.AddEdge(i + n, t, 1);
  128. sol3.cpy(sol); sol2.cpy(sol);
  129. int ans = sol.MaxFlow(s, t);
  130. for(int i = 1; i <= cp; i++) {
  131. for(int j = n + 1; j <= (n << 1); j++) if(en[i][j] >= 0 && !sol.es[en[i][j]].flow) {
  132. sol2.cpy(sol3);
  133. sol2.es[en[i][j]].flow = 0;
  134. int tmp = sol2.MaxFlow(s, t);
  135. // printf("tmp: %d\n", tmp);
  136. if(tmp == ans) is[i] = Info(name[i], string("???"));
  137. else is[i] = Info(name[i], ID[j-n]);
  138. break;
  139. }
  140. }
  141.  
  142. sort(is + 1, is + cp + 1);
  143. for(int i = 1; i <= cp; i++) cout << is[i].name << ':' << is[i].ID << endl;
  144.  
  145. return 0;
  146. }

[codevs1027]姓名与ID的更多相关文章

  1. 1027 姓名与ID[二分图匹配(匈牙利)]

    1027 姓名与ID  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解  查看运行结果       题目描述 Description 有N个人,各自有一 ...

  2. 姓名与ID(codevs 1027 未结题)

    题目描述 Description 有N个人,各自有一个姓名和ID(别名).每个人的姓名和ID都没有重复.这些人依次进入一间房间,然后可能会离开.过程中可以得到一些信息,告知在房间里的某个人的ID.你的 ...

  3. codevs 1027 姓名与ID

    /* 二分图匹配 建图稍麻烦点 不过 有STL大法带我上天 说正经的 先假设都有关系 然后把确定的没有关系的删掉 这样跑出来的一定是完美匹配 至于确定的匹配嘛 删掉这一条 不再是完美匹配 然后记下排序 ...

  4. Java连接excel实现:通过姓名查找id和通过id查找姓名

    注意每个方法结束都要关闭workbook: 还有getIdbyname()方法中字符串flag与name的比较,一定要用equals()方法!!!: 剩下的不多解释,注释都在代码中: import j ...

  5. 表id关联数据获取至页面,制作下拉框多选进行数据多项获取(字段处理)

     这周完成了一张表单,重点碰到以下问题: 1.freemaker获取年份的type值取year,类型直接为Long,传至后台和获取数据不需要转换: 2.freemaker获取日期type值为date, ...

  6. 《转载》PAT 习题

    博客出处:http://blog.csdn.net/zhoufenqin/article/details/50497791 题目出处:https://www.patest.cn/contests/pa ...

  7. 编写高质量代码:改善Java程序的151个建议(第3章:类、对象及方法___建议47~51)

    建议47:在equals中使用getClass进行类型判断 本节我们继续讨论覆写equals的问题,这次我们编写一个员工Employee类继承Person类,这很正常,员工也是人嘛,而且在JavaBe ...

  8. ASP.NET MVC的客户端验证:jQuery验证在Model验证中的实现

    在简单了解了Unobtrusive JavaScript形式的验证在jQuery中的编程方式之后,我们来介绍ASP.NET MVC是如何利用它实现客户端验证的.服务端验证最终实现在相应的ModelVa ...

  9. 完整mybatis应用

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-/ ...

随机推荐

  1. Java排序算法——插入排序

    import java.util.Arrays; //================================================= // File Name : Select_S ...

  2. ectouch笔记

    1.取消配送方式验证mobile\include\apps\default\controller\FlowController.class.php 第1109行 if (isset($is_real_ ...

  3. Visual Studio无法查找或打开 PDB 文件解决办法

    Visual Studio无法查找或打开 PDB 文件解决办法 用VS调试程序时,有时会在VS底部的“输出”框中提示“无法查找或打开 PDB 文件”.这该怎么解决呢? 下面,我们以VS2013为例,来 ...

  4. Enabling and Mounting NFS on CoreOS

    http://blog.scottlowe.org/2015/02/20/config-mount-nfs-coreos/ #cloud-config write-files: - path: /et ...

  5. docker管理shipyard中文版v3.0.2更新

    shipyard中文版v3.0.2更新  https://console.dockerclub.net/ https://dockerclub.net/docs/intro/getting_start ...

  6. OC- @property @synthesize

    @property 1,在@interface中 2,自动生成setter和getter的声明 #import <Foundation/Foundation.h> @interface P ...

  7. VFP笔记

    1.计算圆的面积的计算器 650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/12/43/wKiom1MAsFzxm ...

  8. mysql SELECT FOR UPDATE语句使用示例

    以MySQL 的InnoDB 为例,预设的Tansaction isolation level 为REPEATABLE READ,在SELECT 的读取锁定主要分为两种方式:SELECT ... LO ...

  9. 论Linux运维的一些基础安全知识和简单办法

    不知不觉本人来北京也已经第三个年头了,从一个Linux小小鸟,开始,2012年我参加了第一份工作,其实现在想想其实我是幸运的,本来求学的时候,就没好好的学Linux,我认为有Cisco知识从上wind ...

  10. WebService 之 WSDL文件 讲解

    原文地址:http://blog.csdn.net/tropica/archive/2008/11/02/3203892.aspx 恩,我想说的是,是不是经常有人在开发的时候,特别是和第三方有接口的时 ...