【二分图|推理+最大匹配】POJ-1043 What's In A Name?

来源:互联网 发布:黄岛java开发招聘信息 编辑:程序博客网 时间:2024/05/16 18:44
What's In A Name?
Time Limit: 1000MS Memory Limit: 10000K   

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:fatmanbooth:???clyde:frenchieknuckles:bigmanmoriarty:???mugsy:manglerugati:sinbad
————————————————————緻密な分割線————————————————————
前言:你是大侦探吗?(我看了题解和代码……唉)
思路:我们不知道关在一个小黑屋里的ID和人名之间的关系。
就算我们知道一些可能的关系,但是我们很难知道全部的可能关系。
正常思维建图一定会漏。
但是我们可以利用排除法~
不在小黑屋里的人一定不可能拥有小黑屋里面存在的ID~
通过不可能的关系,可以确定不可能的边。
而求匹配数的时候,剩余的都是可能关系。
求完了之后,枚举每个可能关系,删除它,再求一次最大匹配,如果匹配数减少,说明删除了一个唯一对应的关系。
代码如下:
/*ID: j.sure.1PROG: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 longusing namespace std;const int INF = 0x3f3f3f3f;/****************************************/const int N = 25;int nx, ny, cou[N], tot, relat[N];bool vis[N], in[N], G[N][N];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);#endifwhile(~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;}


0 0
原创粉丝点击