【二分图|推理+最大匹配】POJ-1043 What's In A Name?
2014-11-07 21:26
495 查看
What's In A Name?
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
Sample Output
————————————————————緻密な分割線————————————————————
前言:你是大侦探吗?(我看了题解和代码……唉)
思路:我们不知道关在一个小黑屋里的ID和人名之间的关系。
就算我们知道一些可能的关系,但是我们很难知道全部的可能关系。
正常思维建图一定会漏。
但是我们可以利用排除法~
不在小黑屋里的人一定不可能拥有小黑屋里面存在的ID~
通过不可能的关系,可以确定不可能的边。
而求匹配数的时候,剩余的都是可能关系。
求完了之后,枚举每个可能关系,删除它,再求一次最大匹配,如果匹配数减少,说明删除了一个唯一对应的关系。
代码如下:
Time Limit: 1000MS | Memory Limit: 10000K | |
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
————————————————————緻密な分割線————————————————————
前言:你是大侦探吗?(我看了题解和代码……唉)
思路:我们不知道关在一个小黑屋里的ID和人名之间的关系。
就算我们知道一些可能的关系,但是我们很难知道全部的可能关系。
正常思维建图一定会漏。
但是我们可以利用排除法~
不在小黑屋里的人一定不可能拥有小黑屋里面存在的ID~
通过不可能的关系,可以确定不可能的边。
而求匹配数的时候,剩余的都是可能关系。
求完了之后,枚举每个可能关系,删除它,再求一次最大匹配,如果匹配数减少,说明删除了一个唯一对应的关系。
代码如下:
/* ID: j.sure.1 PROG: LANG: C++ */ /****************************************/ #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <ctime> #include <cmath> #include <stack> #include <queue> #include <vector> #include <set> #include <map> #include <string> #include <climits> #include <iostream> #define LL long long using namespace std; const int INF = 0x3f3f3f3f; /****************************************/ const int N = 25; int nx, ny, cou , tot, relat ; bool vis , in , G ; map <string, int> name, ID; map <string, int>::iterator it1, it2; set <int> hide; set <int>::iterator its; void init() { ny = 0; memset(relat, -1, sizeof(relat)); memset(G, 0, sizeof(G)); hide.clear(); ID.clear(); name.clear(); } int dfs(int u) { for(int v = 0; v < nx; v++) { if(!G[u][v] && !vis[v]) {//对可能的关系进行匹配 vis[v] = true; if(cou[v] == -1 || dfs(cou[v])) { cou[v] = u; return 1; } } } return 0; } int match() { int ret = 0; memset(cou, -1, sizeof(cou)); for(int i = 0; i < nx; i++) { memset(vis, 0, sizeof(vis)); ret += dfs(i); } return ret; } void solve(int ans) { for(int i = 0; i < nx; i++) { for(int j = 0; j < nx; j++) if(!G[i][j]) { G[i][j] = 1;//枚举删除某个匹配,若删除的是唯一匹配,匹配数会减少 int t = match(); G[i][j] = 0; if(t < ans) { relat[i] = j; break; } } } } void PR() { for(it1 = name.begin(); it1 != name.end(); it1++) { int &i = it1->second; if(relat[i] != -1) { for(it2 = ID.begin(); it2 != ID.end(); it2++) { if(it2->second == relat[i]) { cout << it1->first << ":" << it2->first << endl; break; } } } else { cout << it1->first << ":???" << endl; } } } int main() { #ifdef J_Sure // freopen("000.in", "r", stdin); // freopen(".out", "w", stdout); #endif while(~scanf("%d", &nx)) { init(); char s[30], op[2]; for(int i = 0; i < nx; i++) { scanf("%s", s); ID[s] = i; } while(scanf("%s", op), op[0] != 'Q') { scanf("%s", s); if(op[0] == 'E') { if(!name.count(s)) name[s] = ny++; hide.insert(name[s]); } else if(op[0] == 'L') { its = hide.find(name[s]); hide.erase(its); } else if(op[0] == 'M') {//每拦截到一个ID,遍历不在房间里的人,建反边 int v = ID[s]; memset(in, 0, sizeof(in)); for(its = hide.begin(); its != hide.end(); its++) { in[*its] = 1;//给藏匿点的人染色 } for(int u = 0; u < nx; u++) if(!in[u]) { G[u][v] = 1;//不可能关系的边 } } } int ans = match(); solve(ans); PR(); } return 0; }
相关文章推荐
- POJ 1043 What's In A Name? 二分图推理
- POJ 1043 What's In A Name?(唯一的最大匹配方法)
- zoj 1059 poj 1043 What's In A Name?(二分匹配)
- POJ-1043 What's In A Name? 反面构图+枚举边计算最大匹配
- POJ 1043 What's In A Name? 已被翻译
- poj 1043 What's In A Name 二分匹配
- POJ 1274 The Perfect Stall(二分图最大匹配)
- Poj 2112 [最大流] [二分图的多重匹配]
- poj3020 二分图最大匹配(匈牙利算法)
- 【二分图+最大匹配+有难度】poj 2226 Muddy Fields
- POJ1087 A Plug for UNIX 二分图最大匹配
- 二分图最大多重匹配(poj 2289,poj 1698)
- POJ1325 Machine Schedule 二分图最大匹配
- POJ 1486 二分图的最大匹配(强化)
- 【二分图+最大匹配】北大 poj 3041 Asteroids
- 【二分图+最大匹配】北大 poj 2446 Chessboard
- POJ1087 A Plug for UNIX 二分图最大匹配
- 最大独立集,二分图最大匹配 poj 1466
- POJ 1469 二分图最大匹配
- POJ1325 Machine Schedule 二分图最大匹配