[codevs1027]姓名与ID
[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不能确定,输出???。
按照姓名的字典顺序输出。
输入示例
bigman mangler sinbad fatman bigcheese frenchie capodicapo
E mugsy
E knuckles
M bigman
M mangler
L mugsy
E clyde
E bonnie
M bigman
M fatman
M frenchie
L clyde
M fatman
E ugati
M sinbad
E moriarty
E booth
Q
输出示例
bonnie:fatman
booth:???
clyde:frenchie
knuckles:bigman
moriarty:???
mugsy:mangler
ugati:sinbad
数据规模及约定
见“输入”,还有操作数可以达到 10^5 左右。
题解
首先可以想到姓名和 ID 进行二分图匹配,现在难点在于判断唯一性。
跑完二分图匹配后,我们逐个判断,对于一个姓名 i,我们强制不让它匹配它当前的匹配对象,在此基础上再跑一遍二分图,若还是完全匹配,则说明姓名 i 不能被唯一确定,标记成“???”。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <string>
#include <map>
using namespace std; int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 45
#define maxm 3210
#define oo 2147483647
struct Edge {
int from, to, flow;
Edge() {}
Edge(int _1, int _2, int _3): from(_1), to(_2), flow(_3) {}
} ;
struct Dinic {
int n, m, s, t, head[maxn], next[maxm];
Edge es[maxm];
int hd, tl, Q[maxn], vis[maxn];
int cur[maxn];
void cpy(Dinic& T) {
n = T.n; m = T.m; s = T.s; t = T.t;
memcpy(head, T.head, sizeof(head));
memcpy(next, T.next, sizeof(next));
for(int i = 0; i < m; i++) es[i] = T.es[i];
return ;
}
void init(int nn) {
n = nn; m = 0;
memset(head, -1, sizeof(head));
return ;
}
void AddEdge(int a, int b, int c) {
es[m] = Edge(a, b, c); next[m] = head[a]; head[a] = m++;
es[m] = Edge(b, a, 0); next[m] = head[b]; head[b] = m++;
return ;
}
bool BFS() {
memset(vis, 0, sizeof(vis)); vis[s] = 1;
hd = tl = 0; Q[++tl] = s;
while(hd < tl) {
int u = Q[++hd];
for(int i = head[u]; i != -1; i = next[i]) {
Edge& e = es[i];
if(!vis[e.to] && e.flow) {
vis[e.to] = vis[u] + 1;
Q[++tl] = e.to;
}
}
}
return vis[t] > 1;
}
int DFS(int u, int a) {
if(u == t || !a) return a;
int flow = 0, f;
for(int& i = cur[u]; i != -1; i = next[i]) {
Edge& e = es[i];
if(vis[e.to] == vis[u] + 1 && (f = DFS(e.to, min(a, e.flow)))) {
flow += f; a -= f;
e.flow -= f; es[i^1].flow += f;
if(!a) return flow;
}
}
return flow;
}
int MaxFlow(int ss, int tt) {
s = ss; t = tt;
int flow = 0;
while(BFS()) {
for(int i = 1; i <= n; i++) cur[i] = head[i];
flow += DFS(s, oo);
}
return flow;
}
} sol, sol2, sol3; #define maxp 25
string ID[maxp], name[maxp];
map <string, int> M, M2;
bool S[maxp];
int en[maxn][maxn];
struct Info {
string name, ID;
Info() {}
Info(string _, string __): name(_), ID(__) {}
bool operator < (const Info& t) const { return name < t.name; }
} is[maxp]; int main() {
int n = read();
sol.init((n << 1) + 2); int s = (n << 1) + 1, t = s + 1;
for(int i = 1; i <= n; i++) cin >> ID[i];
sort(ID + 1, ID + n + 1);
for(int i = 1; i <= n; i++) M[ID[i]] = i;
int cp = 0;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) en[i][j+n] = en[j+n][i] = 1;
while(1) {
char tc[2]; scanf("%s", tc);
if(tc[0] == 'Q') break;
string t; cin >> t;
if(tc[0] == 'E') {
if(!M2.count(t)) M2[t] = ++cp, name[cp] = t;
S[M2[t]] = 1;
}
if(tc[0] == 'L') S[M2[t]] = 0;
if(tc[0] == 'M') {
int p = M[t];
for(int i = 1; i <= n; i++) if(!S[i]) en[i][p+n] = en[p+n][i] = -1;
}
}
int ce = 0;
for(int i = 1; i <= cp; i++)
for(int j = n + 1; j <= (n << 1); j++) if(en[i][j] > 0)
sol.AddEdge(i, j, 1), en[i][j] = ce, ce += 2;
for(int i = 1; i <= cp; i++) sol.AddEdge(s, i, 1);
for(int i = 1; i <= n; i++) sol.AddEdge(i + n, t, 1);
sol3.cpy(sol); sol2.cpy(sol);
int ans = sol.MaxFlow(s, t);
for(int i = 1; i <= cp; i++) {
for(int j = n + 1; j <= (n << 1); j++) if(en[i][j] >= 0 && !sol.es[en[i][j]].flow) {
sol2.cpy(sol3);
sol2.es[en[i][j]].flow = 0;
int tmp = sol2.MaxFlow(s, t);
// printf("tmp: %d\n", tmp);
if(tmp == ans) is[i] = Info(name[i], string("???"));
else is[i] = Info(name[i], ID[j-n]);
break;
}
} sort(is + 1, is + cp + 1);
for(int i = 1; i <= cp; i++) cout << is[i].name << ':' << is[i].ID << endl; return 0;
}
[codevs1027]姓名与ID的更多相关文章
- 1027 姓名与ID[二分图匹配(匈牙利)]
1027 姓名与ID 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 查看运行结果 题目描述 Description 有N个人,各自有一 ...
- 姓名与ID(codevs 1027 未结题)
题目描述 Description 有N个人,各自有一个姓名和ID(别名).每个人的姓名和ID都没有重复.这些人依次进入一间房间,然后可能会离开.过程中可以得到一些信息,告知在房间里的某个人的ID.你的 ...
- codevs 1027 姓名与ID
/* 二分图匹配 建图稍麻烦点 不过 有STL大法带我上天 说正经的 先假设都有关系 然后把确定的没有关系的删掉 这样跑出来的一定是完美匹配 至于确定的匹配嘛 删掉这一条 不再是完美匹配 然后记下排序 ...
- Java连接excel实现:通过姓名查找id和通过id查找姓名
注意每个方法结束都要关闭workbook: 还有getIdbyname()方法中字符串flag与name的比较,一定要用equals()方法!!!: 剩下的不多解释,注释都在代码中: import j ...
- 表id关联数据获取至页面,制作下拉框多选进行数据多项获取(字段处理)
这周完成了一张表单,重点碰到以下问题: 1.freemaker获取年份的type值取year,类型直接为Long,传至后台和获取数据不需要转换: 2.freemaker获取日期type值为date, ...
- 《转载》PAT 习题
博客出处:http://blog.csdn.net/zhoufenqin/article/details/50497791 题目出处:https://www.patest.cn/contests/pa ...
- 编写高质量代码:改善Java程序的151个建议(第3章:类、对象及方法___建议47~51)
建议47:在equals中使用getClass进行类型判断 本节我们继续讨论覆写equals的问题,这次我们编写一个员工Employee类继承Person类,这很正常,员工也是人嘛,而且在JavaBe ...
- ASP.NET MVC的客户端验证:jQuery验证在Model验证中的实现
在简单了解了Unobtrusive JavaScript形式的验证在jQuery中的编程方式之后,我们来介绍ASP.NET MVC是如何利用它实现客户端验证的.服务端验证最终实现在相应的ModelVa ...
- 完整mybatis应用
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-/ ...
随机推荐
- python学习笔记-(二)python入门
1.第一个python程序 1.1 直接打印输出 打开cmd,输入python进入到python交互式环境:(看到>>>是在Python交互式环境下:) 在python交互环境下输入 ...
- kail2 linux 安装vmware tools
kali进去后,安装vmtools有点蛋疼,中途会问你要编译内核模块所需要的内核头文件,但是没有默认安装的.安装headers时又因为没有源下载不了,所以我们要做一些准备工作. 首先打开shell,我 ...
- re正则表达式7_{}
curly brackets {} instead of one number, you can specify a range by writing a minimum,a comma,and a ...
- centos linux安装telnet 过程及问题(源于内部tomcat网站,外部无法访问)
首先本地没有telnet客户端及服务器 root权限下安装 yum install telnet yum install telnet-server vi /etc/xinetd.d/telnet 这 ...
- NumberFormat类
NumberFormat表示数字的格式化类,即可以按照本地的风格习惯进行数字的显示. NumberFormat是一个抽象类,和MessageFormat类一样,都是Format类的子类,本类在使用时可 ...
- WEB开发基本知识
参考文献:http://www.cnblogs.com/xdp-gacl/p/3729033.html 一.基本概念 1.1.WEB开发的相关知识 WEB,在英语中web即表示网页的意思,它用于表示I ...
- 60行JS实现俄罗斯方块
参考文献:http://www.cnblogs.com/jimaojin/p/5413857.html 原版: <!doctype html><html><head> ...
- ActionScript学习笔记
ActionScript学习笔记 ActionScript中预定义的数据类型:Boolean.int.Number.String.uint 其中,int.Number.uint是处理数字的.int用来 ...
- .Net Core 之 图形验证码
本文介绍.Net Core下用第三方ZKWeb.System.Drawing实现验证码功能. 通过测试的系统: Windows 8.1 64bit Ubuntu Server 16.04 LTS 64 ...
- 字符编码-UNICODE,GBK,UTF-8区别【转转】
字符编码介绍及不同编码区别 今天看到这篇关于字符编码的文章,抑制不住喜悦(总结的好详细)所以转到这里来.转自:祥龙之子http://www.cnblogs.com/cy163/archive/2007 ...