poj 1043 (删边 + 最大匹配)
需要及记住的是,对于这种判断什么情况是否唯一的题目, 可以选择删掉这个看最后的结果时候会改变来判断。
一个大牛的解题报告:
题意:给定一群人的姓名和昵称,给定了一些关系,现在要求判定姓名和昵称能够一一对应的有哪些?
解法:一开始直接使用藏匿点的所有人和邮件进行构边,再用删除来判定,结果出错,为什么呢?因为我们将藏匿点的所有人和邮件连边确定的就是一种可能关系,然而题目中还隐藏了许多的可能关系,比如某人在藏匿点但是没有发邮件,那么其和其他未出现的昵称之间存在可能关系。正确的解法是确定不可能关系,因为这样更加简单,在藏匿点外的人不可能与邮件有关系。初始化所有人和所有昵称都有关系,通过排除不可能的关系即确定了可能的关系。之后再枚举每一条边,将其删除看通过最大匹配是否减小来断定这条边是不是被唯一对应。注意:如果某一组名字-昵称关系是唯一确定的话,那么删除完所有的不可能情况后其在二分图中连到该昵称的边只有一条。
看完后才豁然开朗, 果真思维还是比较局限。。。
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 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 Source |
#include <stdio.h> #include <string.h> #include <algorithm> #include <string> #include <map> #include <iostream> using namespace std; struct node { string x,y; }save[22]; map<string,int> g; string sg[22]; map<string,int> g1; int n; int cnt; int mark[22]; int map1[22][22]; int pre[22]; int dfs(int s) { for(int i=1;i<=n;i++) { if( mark[i]==1 || map1[s][i]==0 ) continue; mark[i]=1; if( pre[i]==-1 || dfs(pre[i])==1) { pre[i]=s; return 1; } } return 0; } int xyl() { int sum=0; memset(pre,-1,sizeof(pre)); for(int i=1;i<=n;i++) { memset(mark,0,sizeof(mark)); sum += dfs(i); } return sum; } int cmp(node t,node t1) { return t.x < t1.x; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { string tmp; cin>>tmp; g[tmp]=i; sg[i]=tmp; } memset(mark,0,sizeof(mark)); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) map1[i][j]=1; int cnt=1; char key; while(cin>>key&&key!='Q') { string tmp; cin>>tmp; if( key=='E' ) { if( g1[tmp]==0 ) g1[tmp] = cnt++; mark[ g1[tmp] ]=1; //sg1[ g1[tmp] ]=tmp; save[ g1[tmp] ].x=tmp; save[ g1[tmp] ].y="???"; continue; } if(key=='M') { for(int i=1;i<=n;i++) { if( mark[i]==0 ) { map1[i][ g[tmp] ]=0; } } continue; } if(key=='L') { if( g1[tmp]==0 ) g1[tmp]=cnt++; mark[ g1[tmp] ] = 0; //sg[ g1[tmp] ]=tmp; save[ g1[tmp] ].x=tmp; save[ g1[tmp] ].y="???"; continue; } } int sum = xyl(); int tpre[22]; for(int i=1;i<=n;i++) tpre[i]=pre[i]; for(int j=1;j<=n;j++) { int i=tpre[j]; map1[i][j]=0; if( xyl() != sum ) { save[i].y = sg[j]; } map1[i][j]=1; } sort(save+1,save+n+1,cmp); for(int i=1;i<=n;i++) { cout<<save[i].x<<":"<<save[i].y<<endl; } return 0; }