Reposts (Codeforces 522A)
Tags: DP, *special

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 Inputs:
5
tourist reposted Polycarp
Petr reposted Tourist
WJMZBMR reposted Petr
sdya reposted wjmzbmr
vepifanov reposted sdya

6
Mike reposted Polycarp
Max reposted Polycarp
EveryOne reposted Polycarp
111 reposted Polycarp
VkCup reposted Polycarp
Codeforces reposted Polycarp

1
SoMeStRaNgEgUe reposted PoLyCaRp

Sample Outputs:
6

2

2

Solution (Python):
global h
h = -1

def dfs(node, parent, current, d):
    for i in d[node]:
        if i == parent:
            continue
        else:
            global h
            h = max(h, current + 1)
            dfs(i, node, current + 1, d)

def main():
    d = dict()
    n = int(input())
    while n > 0:
        s = input()
        l = s.split()
        r1 = l[0]
        r2 = l[2]
        r1 = r1.lower()
        r2 = r2.lower()
        d.setdefault(r1, []).append(r2)
        d.setdefault(r2, []).append(r1)
        n -= 1
    dfs('polycarp', '@', 1, d)
    print(h)
main()

Solution (Python):
dic = {'polycarp': 1}
for i in range(int(input())):
    z = input().split()
    a = z[0].lower()
    b = z[-1].lower()
    dic[a] = dic[b] + 1
xyz = list(dic.values())
print(max(xyz))

Solution (Python):
n = int(input())
matrix = []
graph = {}
name_space = set()
for i in range(n):
    l = input().split(' ')
    name_space.add(l[0])
    name_space.add(l[2])
    matrix.append(l)
height = {}
for i in name_space:
    height[i.lower()] = 0
for i in list(name_space):
    graph[i.lower()] = []
for l in matrix:
    graph[l[2].lower()] += [l[0].lower()]
    graph[l[0].lower()] += [l[2].lower()]

def dfs(vertex, graph, par):
    global height
    if len(graph[vertex]) == 1 and vertex != 'polycarp':
        height[vertex] = 0
        return 0
    else:
        for i in graph[vertex]:
            if i != par:
                height[vertex] = max(dfs(i, graph, vertex), height[vertex])
        height[vertex] += 1
        return height[vertex]
dfs('polycarp', graph, '')
print(height['polycarp'] + 1)