A. Reposts
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
Examples
inputCopy
5
tourist reposted Polycarp
Petr reposted Tourist
WJMZBMR reposted Petr
sdya reposted wjmzbmr
vepifanov reposted sdya
outputCopy
6
inputCopy
6
Mike reposted Polycarp
Max reposted Polycarp
EveryOne reposted Polycarp
111 reposted Polycarp
VkCup reposted Polycarp
Codeforces reposted Polycarp
outputCopy
2
inputCopy
1
SoMeStRaNgEgUe reposted PoLyCaRp
outputCopy
2
题意:
由N个关系
表示A被B散播的谣言洗脑了,然后A可能继续去传播谣言
Polycarp是谣言的散播者,问从他开始有几层散播关系
思路:
用map来存人名和层数,建立一个类似树的关系,找出层数得到答案
大佬的代码,找不到是谁了。。。
#include<bits/stdc++.h>
using namespace std;
map < string , int > cnt;
string CapsLock(string s)
{
for (int i = 0; i < s.size(); i ++)
if ('Z' < s[i])
s[i] = (char)('A' + (s[i] - 'a'));
return s;
}
int main()
{
ios::sync_with_stdio(0);
int n, ans = 0;
string a, b, c;
cin >> n;
cnt["POLYCARP"] = 1;
for (int i = 0; i < n; i ++)
{
cin >> a >> b >> c;
a = CapsLock(a); c = CapsLock(c);
cnt[a] = cnt[c] + 1;
ans = max(ans, cnt[a]);
}
cout << ans;
return 0;
}