One day Polycarp published a funny picture in a social network making a poll about the color of his handle. Many of his friends started reposting Polycarp's joke to their news feed. Some of them reposted the reposts and so on.
These events are given as a sequence of strings "name1 reposted name2", where name1 is the name of the person who reposted the joke, and name2 is the name of the person from whose news feed the joke was reposted. It is guaranteed that for each string "name1 reposted name2" user "name1" didn't have the joke in his feed yet, and "name2" already had it in his feed by the moment of repost. Polycarp was registered as "Polycarp" and initially the joke was only in his feed.
Polycarp measures the popularity of the joke as the length of the largest repost chain. Print the popularity of Polycarp's joke.
The first line of the input contains integer n (1 ≤ n ≤ 200) — the number of reposts. Next follow the reposts in the order they were made. Each of them is written on a single line and looks as "name1 reposted name2". All the names in the input consist of lowercase or uppercase English letters and/or digits and have lengths from 2 to 24 characters, inclusive.
We know that the user names are case-insensitive, that is, two names that only differ in the letter case correspond to the same social network user.
Print a single integer — the maximum length of a repost chain.
5 tourist reposted Polycarp Petr reposted Tourist WJMZBMR reposted Petr sdya reposted wjmzbmr vepifanov reposted sdya
6
6 Mike reposted Polycarp Max reposted Polycarp EveryOne reposted Polycarp 111 reposted Polycarp VkCup reposted Polycarp Codeforces reposted Polycarp
2
1 SoMeStRaNgEgUe reposted PoLyCaRp
2
题目大意:
给你n条信息,表示a把信息传递给b。a传递给b,其中链中包含两个人。问最长的链包含几个人。
思路:
1、因为n不大,那么其总人数(总节点)个数也不大,我们使用Floyd求一遍最短路,然后维护最大值即可。
2、题意很明确,只不过在测试的过程中容易忽略第一组样例的输出 ..................大小写名字只要对应上了就算是一个人...................
Ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<map>
using namespace std;
char a[2500];
char b[2500];
char c[2500];
int dis[250][250];
int n;
int main()
{
int m;
while(~scanf("%d",&m))
{
n=1;
memset(dis,0x3f3f3f3f,sizeof(dis));
map<string,int >s;
for(int i=0;i<m;i++)
{
scanf("%s%s%s",a,b,c);
for(int j=0;j<strlen(a);j++)
{
if(a[j]>='A'&&a[j]<='Z')a[j]+=32;
}
for(int j=0;j<strlen(c);j++)
{
if(c[j]>='A'&&c[j]<='Z')c[j]+=32;
}
if(s[a]==0)
{
s[a]=n++;
}
if(s[c]==0)
{
s[c]=n++;
}
dis[s[a]][s[c]]=1;
}
for(int i=1;i<n;i++)
{
for(int j=1;j<n;j++)
{
for(int k=1;k<n;k++)
{
dis[j][k]=min(dis[j][k],dis[j][i]+dis[i][k]);
}
}
}
int output=0;
for(int i=1;i<n;i++)
{
for(int j=1;j<n;j++)
{
if(dis[i][j]==0x3f3f3f3f)continue;
output=max(dis[i][j],output);
}
}
printf("%d\n",output+1);
}
}