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 "name1reposted 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
题意:A reposted B , 大小写不区分,问最长路是多少
思路:全部变成小写字母,MAP对应的节点编号,dfs求深度
复杂度 O(n)
#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define bug cout << "bug" << endl
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long ll;
typedef pair<int,int > pii;
const int MAX_N=1e3+3;
vector <int> edge[MAX_N];
map <string,int> mp;
int par[MAX_N],dep[MAX_N];
int Find(){
int x=1;
while(par[x]!=x){
x=par[x];
if(par[x]==x) break;
}
return x;
}
void dfs(int u,int d){
dep[u]=d;
for(int i=0;i<edge[u].size();i++){
dfs(edge[u][i],d+1);
}
return ;
}
int main(void){
int n;
cin >> n ;
int cnt=1;
for(int i=1;i<=600;i++) par[i]=i;
for(int i=1;i<=n;i++){
string s1,s2,s3;
cin >>s1>>s2>>s3;
for(int i=0;i<(int)s1.size();i++)
if(s1[i]>='A'&&s1[i]<='Z') s1[i]=s1[i]-'A'+'a';
for(int i=0;i<(int)s3.size();i++)
if(s3[i]>='A'&&s3[i]<='Z') s3[i]=s3[i]-'A'+'a';
int u=mp[s1],v=mp[s3];
if(!mp[s1]) mp[s1]=cnt,u=cnt++;
if(!mp[s3]) mp[s3]=cnt,v=cnt++;
edge[v].push_back(u);
par[u]=v;
}
int root=Find();
dfs(root,1);
// cout <<"root="<<root << endl;
// for(int i=1;i<=cnt-1;i++) {
// printf("%d:",i);
// for(int j=0;j<edge[i].size();j++)
// printf(" %d",edge[i][j]);puts("");
// }
int mx=-INF;
for(int i=1;i<=cnt-1;i++)
if(dep[i]>mx) mx=dep[i];
cout << mx << endl;
}