POJ1043 What's In A Name?
Time Limit: 1000MS | Memory Limit: 10000KB | 64bit IO Format: %lld & %llu |
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.
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
通过逻辑分析先标记出无法对应的人,然后再标记出不可能匹配的组合。
在剩下可能匹配的实名和ID中,寻找完全匹配:枚举删掉某条边,如果不管删不删,二分图匹配不受影响,就无关,如果删掉以后二分图匹配结果变了,说明这条边是在完全匹配中的。
1 /*by SilverN*/ 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #include<map> 8 using namespace std; 9 struct pp{ 10 string a,b; 11 }s[300]; 12 int cmp(pp a,pp b){ 13 return a.a<b.a; 14 } 15 map<string,int>m,m2; 16 int mp[300][300]; 17 int link[300],vis[300]; 18 int mark[300]; 19 char ch[300][300],c1[300],c2[300]; 20 int n,cnt; 21 // 22 int dfs(int u){ 23 for(int i=1;i<=n;i++){ 24 if(!vis[i] && !mp[u][i]) 25 { 26 vis[i]=1; 27 if(!link[i] || dfs(link[i])){ 28 link[i]=u; 29 return 1; 30 } 31 } 32 } 33 return 0; 34 } 35 int solve(){ 36 memset(link,0,sizeof link); 37 int res=0; 38 for(int i=1;i<=n;i++){ 39 memset(vis,0,sizeof vis); 40 if(dfs(i))res++; 41 } 42 return res; 43 } 44 // 45 int main(){ 46 scanf("%d",&n); 47 int i,j; 48 for(i=1;i<=n;i++){ 49 scanf("%s",ch[i]); 50 m[ch[i]]=i; 51 } 52 while(scanf("%s",c1)){ 53 if(c1[0]=='Q')break; 54 scanf("%s",c2); 55 if(c1[0]=='L'){ 56 mark[m2[c2]]=0; 57 } 58 if(c1[0]=='E'){ 59 if(!m2[c2]){ 60 m2[c2]=++cnt; 61 } 62 mark[m2[c2]]=1; 63 s[m2[c2]].a=c2; 64 s[m2[c2]].b="???"; 65 } 66 if(c1[0]=='M'){ 67 for(i=1;i<=n;i++) if(!mark[i]) mp[m[c2]][i]=1; 68 } 69 } 70 int ans=solve(); 71 int cpylink[300]; 72 for(i=1;i<=n;i++)cpylink[i]=link[i]; 73 for(i=1;i<=n;i++){//寻找完全匹配 74 int tmp=cpylink[i]; 75 mp[tmp][i]=1; 76 if(solve()!=ans){ 77 s[i].b=ch[tmp]; 78 } 79 mp[tmp][i]=0; 80 } 81 sort(s+1,s+n+1,cmp); 82 for(i=1;i<=n;i++)cout<<s[i].a<<":"<<s[i].b<<endl; 83 return 0; 84 }
本文为博主原创文章,转载请注明出处。