1837. Isenbaev's Number
Time limit: 0.5 second
Memory limit: 64 MB
Memory limit: 64 MB
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 |
/***
最短路问题,麻烦在字符串处理上,用map就行;
但,测试数据中可能不会出现Isenbaev这个人。。。
这个地方是个坑。。
***/
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <vector>
#include <bitset>
#include <cstdio>
#include <string>
#include <numeric>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int dx[4]= {-1,1,0,0};
int dy[4]= {0,0,-1,1}; //up down left right
bool inmap(int x,int y,int n,int m)
{
if(x<1||x>n||y<1||y>m)return false;
return true;
}
int hashmap(int x,int y,int m)
{
return (x-1)*m+y;
}
#define eps 1e-8
#define inf 0x7fffffff
#define debug puts("BUG")
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#define maxn 400
map<string,int>m;
string name[maxn];
int via[maxn];
int dis[maxn];
int s[maxn][maxn];
int num;
int flag=0;
void init()//初始化
{
memset(dis,1,sizeof(dis));
memset(s,1,sizeof(s));
m["Isenbaev"]=0;
name[0]="Isenbaev";
num=1;
}
void Dijkstra()//最短路
{
for(int i=0;i<num;i++)
{
dis[i]=s[0][i];
}
for(int i=1;i<num;i++)
{
int mins=dis[num],k=0;
for(int j=0;j<num;j++)
if(via[j]==0&&dis[j]<mins)
{
mins=dis[j];k=j;
}
via[k]=1;
for(int j=0;j<num;j++)
{
if(dis[j]>dis[k]+s[j][k])
dis[j]=dis[k]+s[j][k];
}
}
}
void print()
{
for(int i=0; i<num; i++)
{
if(flag==0&&name[i]=="Isenbaev")
continue;
cout<<name[i];
if(dis[m[name[i]]]<dis[num])//判断~~
printf(" %d\n",dis[m[name[i]]]);
else
printf(" undefined\n");
}
}
int main()
{
init();
int n;
scanf("%d",&n);
for(int i=0; i!=n; i++)
{
int l[4];
string S[4];
cin>>S[1]>>S[2]>>S[3];//题目给的字符串少,能用string和map,外加cin
for(int j=1; j<4; j++)
{
if(S[j]!="Isenbaev"&&m[S[j]]==0)
{
name[num]=S[j];
m[S[j]]=num;
num++;
}
if(S[j]=="Isenbaev")//判断大神是否出现过
flag=1;
l[j]=m[S[j]];
}
s[l[1]][l[1]]=0;//添加边~~
s[l[2]][l[2]]=0;
s[l[3]][l[3]]=0;
s[l[1]][l[2]]=1;
s[l[2]][l[1]]=1;
s[l[1]][l[3]]=1;
s[l[3]][l[1]]=1;
s[l[3]][l[2]]=1;
s[l[2]][l[3]]=1;
}
if(flag)//如果出现了那个大神,就求最短路,否则,就不用了
{
Dijkstra();
}
sort(name,name+num);//给名字排序,按字典序输出
print();
return 0;
}