What's In A Name?
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 2600   Accepted: 933

Description

The FBI is conducting a surveillance of a known criminal hideout which serves as a communication center for a number of men and women of nefarious intent. Using sophisticated decryption software and good old fashion wiretaps, they are able to decode any e-mail messages leaving the site. However, before any arrest warrants can be served, they must match actual names with the user ID's on the messages. While these criminals are evil, they're not stupid, so they use random strings of letters for
their ID's (no dillingerj ID's found here). The FBI knows that each
criminal uses only one ID. The only other information they have which
will help them is a log of names of the people who enter and leave the
hideout. In many cases, this is enough to link the names to the ID's.

Input

Input
consists of one problem instance. The first line contains a single
positive integer n indicating the number of criminals using the hideout.
The maximum value for n will be 20. The next line contains the n user
ID's, separated by single spaces. Next will be the log entries in
chronological order. Each entry in the log has the form type arg , where
type is either E, L or M: E indicates that criminal arg has entered the
hideout; L indicates criminal arg has left the hideout; M indicates a
message was intercepted from user ID arg. A line containing only the
letter Q indicates the end of the log. Note that not all user ID's may
be present in the log but each criminal name will be guaranteed to be in
the log at least once. At the start of the log, the hideout is presumed
to be empty. All names and user ID's consist of only lowercase letters
and have length at most 20. Note: The line containing only the user ID's
may contain more than 80 characters.

Output

Output
consists of n lines, each containing a list of criminal names and their
corresponding user ID's, if known. The list should be sorted in
alphabetical order by the criminal names. Each line has the form
name:userid , where name is the criminal's name and userid is either
their user ID or the string ??? if their user ID could not be determined
from the surveillance log.

Sample Input

7
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

Sample Output

bonnie:fatman
booth:???
clyde:frenchie
knuckles:bigman
moriarty:???
mugsy:mangler
ugati:sinbad
【题意】一个犯罪团伙有N个人,他们分别有一个名字和一个网名 现已知他们会先后进出一个房间发送电报 警方可以知道所有时间下: 进出房间的人的真实名字 同时通过截获该房间发出的电报,获得网名 问最后
能否将所有真实名字和虚拟网名对上
【分析】首先根据题目条件名字和网名是一一对应的,可以大概确定是二分匹配中的完美匹配 然而根据样例很容易看出来,要想根据正确关系来建边是很复杂的 容易的做法是:每次将不可能匹配的名字和网名建边,
最后根据补图进行最大匹配即可初步得出所有匹配关系.但现在得到的最大匹配不一定是完美匹配 要确定某个名字和网名是匹配的 我们可以删除当前已匹配的边,再进行最大匹配 如果结果减小了,则一定是对应的
这样,依次枚举每一条最大匹配中的边.即可得出答案
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <queue>
#include <vector>
#define inf 0x7fffffff
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = ;
const int M = ;
int read() {
int x=,f=;
char c=getchar();
while(c<''||c>'') {
if(c=='-')f=-;
c=getchar();
}
while(c>=''&&c<='') {
x=x*+c-'';
c=getchar();
}
return x*f;
}
struct man {
string a,b;
} s[N];
map<string,int>m1,m2;
string s1[N];
int edg[N][N];
int link[N],vis[N];
int mark[N];
int id,n; void init() {
memset(edg,,sizeof(edg));
memset(mark,,sizeof(mark));
id=;
m1.clear(),m2.clear();
} bool cmp(man x,man y) {
return x.a<y.a;
} bool dfs(int u) {
for(int i=; i<=n; i++) {
if(!vis[i]&&edg[u][i]==) {
vis[i]=;
if(link[i]==-||dfs(link[i])) {
link[i]=u;
return true;
}
}
}
return false;
} int MaxMatch() {
memset(link,-,sizeof(link));
int ans=;
for(int i=; i<=n; i++) {
memset(vis,,sizeof(vis));
if(dfs(i))
ans++;
}
return ans;
}
int main() {
int m,a,b,d;
char ch;
string str;
while(~scanf("%d",&n)) {
init();
for(int i=; i<=n; i++) {
cin>>s1[i];
m1[s1[i]]=i;
}
while(cin>>ch) {
if(ch=='Q')
break;
else {
cin>>str;
if(ch=='E') {
if(!m2[str])m2[str]=id++;
d=m2[str];
mark[d]=;
s[d].a=str;
s[d].b="???";
} else if(ch=='L') {
d=m2[str];
mark[d]=;
} else if(ch=='M') {
d=m1[str];
for(int i=; i<=n; i++)if(!mark[i])edg[d][i]=; //建立反向边
}
}
}
int ans=MaxMatch();
int linkt[N];
for(int i=; i<=n; i++) //原最大匹配中的边
linkt[i]=link[i];
for(int i=; i<=n; i++) {
d=linkt[i];
edg[d][i]=;
if(MaxMatch()!=ans)s[i].b=s1[d]; //最大匹配减少
edg[d][i]=;
}
sort(s+,s++n,cmp);
for(int i=; i<=n; i++)
cout<<s[i].a<<":"<<s[i].b<<endl;
}
return ;
}

POJ 1043 What's In A Name?(唯一的最大匹配方法)的更多相关文章

  1. 关于全局唯一ID生成方法

    引:最近业务开发过程中需要涉及到全局唯一ID生成.之前零零总总的收集过一些相关资料,特此整理以便后用 本博客已经迁移至:http://cenalulu.github.io/ 本篇博文已经迁移,阅读全文 ...

  2. 转:C#生成唯一值的方法汇总

    这篇文章主要介绍了C#生成唯一值的方法汇总,有需要的朋友可以参考一下 生成唯一值的方法很多,下面就不同环境下生成的唯一标识方法一一介绍,作为工作中的一次总结,有兴趣的可以自行测试: 一.在 .NET ...

  3. C#生成唯一值的方法汇总

    生成唯一值的方法很多,下面就不同环境下生成的唯一标识方法一一介绍,作为工作中的一次总结,有兴趣的可以自行测试: https://www.cnblogs.com/xinweichen/p/4287640 ...

  4. POJ 1845-Sumdiv(快速幂取模+整数唯一分解定理+约数和公式+同余模公式)

    Sumdiv Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  5. 生成GUID唯一值的方法汇总(dotnet/javascript/sqlserver)

    一.在 .NET 中生成1.直接用.NET Framework 提供的 Guid() 函数,此种方法使用非常广泛.GUID(全局统一标识符)是指在一台机器上生成的数字,它保证对在同一时空中的任何两台计 ...

  6. oracle数据库出现“批处理中出现错误: ORA-00001: 违反唯一约束条件”解决方法

    最近使用oraclede impdp工具全库导入数据库时,在数据库里面使用出现如下情况. SQL state : 违反唯一约束条件 (GDXAORCL.SYS_C0055359) ; nested e ...

  7. STM32全球唯一ID读取方法

    产品唯一的身份标识非常适合:● 用来作为序列号(例如USB字符序列号或者其他的终端应用)● 用来作为密码,在编写闪存时,将此唯一标识与软件加解密算法结合使用,提高代码在闪存存储器内的安全性.● 用来激 ...

  8. poj 3311 floyd+dfs或状态压缩dp 两种方法

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6436   Accepted: 3470 ...

  9. 【霍夫曼树】poj 1339 poker card game (数组排序+辅助队列的方法,预处理O(nlogn),构造霍夫曼树O(n))

    poj.org/problem?id=1339 #include<iostream> #include<cstdio> #include<string> #incl ...

随机推荐

  1. linux下获取帮助

    -h --help man 代號 代表內容 使用者在shell中可以操作的指令或可执行档 系統核心可呼叫的函数与工具等 一些常用的函数(function)与函数库(library),大部分是C的函数库 ...

  2. 《day11---内部类&匿名内部类》

    //79-80-面向对象-内部类-体现 /* 当A类中的内容要被B类直接访问,而A类还需要去创建B类的对象,访问B的内容时, 这时可以将B类定义到A类的内部,这样访问更为便捷. 将B类称之为内部类(内 ...

  3. poj1014 dp 多重背包

    //Accepted 624 KB 16 ms //dp 背包 多重背包 #include <cstdio> #include <cstring> #include <i ...

  4. matlab的正则表达式讲解[转]

    引言.啥是正则表达式?正则表达式是干啥的?我理解就和我们在word或者其他编辑软件里点的查找.替换的作用是差不多的,不过功能要强大的多,当然使用起来也稍微复杂一些.书上的定义差不多是这样的:正则表达式 ...

  5. (转)Tomcat的目录结构

    原文:http://hi.baidu.com/qinyougen/item/beeb506abb3e1d08a1cf0ffb Tomcat的目录结构 一.TOMCAT的目录结构 /bin:存放wind ...

  6. 解决:HotSeat短信图标提醒有误

    [操作步骤]正常收发短信.彩信. [测试结果]所有短信均已阅读,但在HOME界面的短信图标仍提示有一条短信未读.重启后仍存在. 经过分析,导致该情况的主要原因为当彩信已读的时候,launcher中进行 ...

  7. 产生冠军 map 的 应用 .

    开始 比赛  ,  每一次的 比赛 都会有人失败 , 如果产生英雄的话  , 那就是产生 唯一一个 没有被打败的人  , 就是英雄, . #include<stdio.h> #includ ...

  8. Tips about Object-oriented programming

    1, Return subinterface For example, we have a parent interface: public interface A<T extends A< ...

  9. HDU 4462

    http://acm.hdu.edu.cn/showproblem.php?pid=4462 一道题意不清的水题 题意:给一个n*n的格子,在上面放草人,每个草人有恐惧范围,问最少选择几个草人可以覆盖 ...

  10. C#判断字符串是否为数字

    string i = Console.ReadLine();            int a=0;            if (int.TryParse(i, out a) == false) / ...