CodeForces 522A(DFS||floyd最短路)

来源:互联网 发布:java 获取进程id 编辑:程序博客网 时间:2024/04/28 23:12

A. Reposts
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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.

Output

Print a single integer — the maximum length of a repost chain.

Sample test(s)
input
5tourist reposted PolycarpPetr reposted TouristWJMZBMR reposted Petrsdya reposted wjmzbmrvepifanov reposted sdya
output
6
input
6Mike reposted PolycarpMax reposted PolycarpEveryOne reposted Polycarp111 reposted PolycarpVkCup reposted PolycarpCodeforces reposted Polycarp
output
2
input
1SoMeStRaNgEgUe reposted PoLyCaRp
output
2


好久没做题了,好懒呀敲打,被期末折磨死了


题意:题意很简单,给你n个字符串,A采访B,B采访C ,这一条的采访链长度是3,求最长的链。


题解:这一题开始想的是使DFS强行搜出最长链,发现可能会超时,想记忆化搜索但是不会,这时候我想到可以直接求任意间最短路,然后直接输出最长的那条路就是最长的链,好啦就这样把这一题转换为最短路问题。先使用map映射为数字,然后直接Floyd,最后枚举点找最大值就可了。


#include<iostream>#include<cstdio>#include<map>#include<algorithm>#include<string>#include<cstring>#include<vector>using namespace std;#define N 250#define inf 0x3f3f3f3fmap<string,int>Hash;inline void change(string &s){for(int i=0;i<s.length();i++){if(s[i]>='A'&&s[i]<='Z'){s[i]+=('a'-'A');}}}int g[N][N];int main(){#ifdef CDZSCfreopen("i.txt","r",stdin);#endifstring s1,s2,s3;int n;while(~scanf("%d",&n)){memset(g,inf,sizeof(g));Hash.clear();int hs=0;for(int i=0;i<n;i++){cin>>s1>>s2>>s3;change(s1);change(s3);if(!Hash.count(s1)){Hash[s1]=++hs;}if(!Hash.count(s3)){Hash[s3]=++hs;}g[Hash[s1]][Hash[s3]]=1;}for(int k=1;k<=hs;k++){for(int i=1;i<=hs;i++){for(int j=1;j<=hs;j++){g[i][j]=min(g[i][k]+g[k][j],g[i][j]);}}}int ans=-inf;for(int i=1;i<=hs;i++){for(int j=1;j<=hs;j++){if(g[i][j]<inf){ans=max(ans,g[i][j]);}}}printf("%d\n",ans+1);}return 0;}









0 0