Vladislav Isenbaev is a two-time champion of Ural, vice champion of TopCoder Open 2009, and absolute champion of ACM ICPC 2009. In the time you will spend reading this problem statement Vladislav would have solved a problem. Maybe, even two…
Since Vladislav Isenbaev graduated from the Specialized Educational and Scientific Center at Ural State University, many of the former and present contestants at USU have known him for quite a few years. Some of them are proud to say that they either played in the same team with him or played in the same team with one of his teammates…
Let us define Isenbaev's number as follows. This number for Vladislav himself is 0. For people who played in the same team with him, the number is 1. For people who weren't his teammates but played in the same team with one or more of his teammates, the number is 2, and so on. Your task is to automate the process of calculating Isenbaev's numbers so that each contestant at USU would know their proximity to the ACM ICPC champion.
Input
The first line contains the number of teams n (1 ≤ n ≤ 100). In each of the following n lines you are given the names of the three members of the corresponding team. The names are separated with a space. Each name is a nonempty line consisting of English letters, and its length is at most 20 symbols. The first letter of a name is capital and the other letters are lowercase.
Output
For each contestant mentioned in the input data output a line with their name and Isenbaev's number. If the number is undefined, output “undefined” instead of it. The contestants must be ordered lexicographically.
Sample
input | output |
---|---|
7 Isenbaev Oparin Toropov Ayzenshteyn Oparin Samsonov Ayzenshteyn Chevdar Samsonov Fominykh Isenbaev Oparin Dublennykh Fominykh Ivankov Burmistrov Dublennykh Kurpilyanskiy Cormen Leiserson Rivest |
Ayzenshteyn 2 Burmistrov 3 Chevdar 3 Cormen undefined Dublennykh 2 Fominykh 1 Isenbaev 0 Ivankov 2 Kurpilyanskiy 3 Leiserson undefined Oparin 1 Rivest undefined Samsonov 2 Toropov 1 |
Problem Author: folklore Problem Source: Ural Championship 2011
Difficulty: 76 Printable version Submit solution Discussion (38) All submissions (8582) All accepted submissions (2643) Solutions rating (1698)
****************************************************************************
刚开始以为是有向图,其实是无向图,可用邻接矩阵存,此题用字典树,哈希的经典实现,加dijstra算法求最短路径。
*****************************************************************************
1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cstdio> 5 #include<cmath> 6 #include <algorithm> 7 using namespace std; 8 const int inf=1<<28; 9 const int nMax=1005; 10 struct node 11 { 12 int id;//存储编号 13 node *p[130]; 14 node(){ 15 int i; 16 id=-1; 17 for(i=0;i<130;i++)p[i]=NULL; 18 } 19 }; 20 node *root; 21 int cnt; 22 23 int getnum(char *s)//建字典树 24 { 25 int i; 26 node *r=root; 27 int l=strlen(s); 28 for(i=0;i<l;i++){//扩展树的子节点 29 if(r->p[s[i]]==NULL){ 30 r->p[s[i]]=new node(); 31 } 32 r=r->p[s[i]]; 33 } 34 if(r->id==-1){ 35 r->id=cnt;//编号(关键字) 36 cnt++; 37 } 38 return r->id; 39 } 40 41 int n,map[nMax][nMax],dis[nMax],p[nMax]; 42 void dijkstra(int from){ 43 for(int i=0;i<n;i++)dis[i]=inf; 44 memset(p,false,sizeof(p)); 45 p[from]=true; 46 dis[from]=0; 47 int mine; 48 int now=from; 49 for(int i=1;i<n;i++){ 50 for(int k=1;k<n;k++)//找当前状态下k的最短距离 51 if( map[now][k]!=inf&&dis[now]!=inf&&map[now][k]+dis[now]<dis[k]) 52 dis[k] = dis[now] + map[now][k]; 53 mine=inf; 54 for(int k=1;k<n;k++)//找下一个当前节点 55 if(mine>dis[k]&&!p[k]) 56 mine=p[now=k]; 57 p[now]=true; 58 } 59 } 60 61 struct mp{ 62 int data; 63 char str[100]; 64 }s[nMax]; 65 66 bool cmp(mp a,mp b){ 67 if(strcmp(a.str,b.str)<0)return true; 68 return false; 69 } 70 71 int main(){ 72 int m,i,j,a,b,c; 73 char str1[100],str2[100],str3[100]; 74 while(scanf("%d",&m)!=EOF){ 75 cnt=1; 76 root=new node(); 77 for(i=0;i<nMax;i++){ 78 for(j=0;j<nMax;j++){ 79 map[i][j]=inf; 80 } 81 } 82 for(i=0;i<m;i++){ 83 scanf("%s%s%s",str1,str2,str3); 84 a=getnum(str1);//哈希的真正实现(与字符串第一次输入的次序有关) 85 b=getnum(str2); 86 c=getnum(str3); 87 map[a][b]=map[b][a]=1;//邻接矩阵存储 88 map[a][c]=map[c][a]=1; 89 map[b][c]=map[c][b]=1; 90 strcpy(s[a].str,str1); 91 strcpy(s[b].str,str2); 92 strcpy(s[c].str,str3); 93 } 94 n=cnt; 95 strcpy(str1,"Isenbaev"); 96 a=getnum(str1); 97 dijkstra(a);//dijstra算法的实现 98 for(i=1;i<n;i++){ 99 s[i].data=dis[i]; 100 } 101 sort(s,s+n,cmp);//字典序排序 102 for(i=1;i<n;i++){ 103 cout<<s[i].str<<" "; 104 if(s[i].data==inf){ 105 cout<<"undefined"<<endl; 106 } 107 else{ 108 cout<<s[i].data<<endl; 109 } 110 } 111 } 112 return 0; 113 }