ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1837. Isenbaev's Number

Always WA1 what am i doing wrong?
Posted by newcomer 25 Jul 2016 18:13
class Node:
    def __init__(self, name, number):
        self.name = name
        self.number = number
        self.children = []

    def d(self, name):
        for i in self.children:
            if i.name == name:
                self.children.remove(i)
        for i in self.children:
            i.d(name)


def fnc(cmds, item, isenb, root):
    for c in cmds:
        if item.name in c:
            for n in c:
                if not n in isenb:
                    isenb[n] = item.number + 1
                    item.children.append(Node(n, item.number + 1))
                elif isenb[n] > (item.number + 1):
                    isenb[n] = item.number + 1
                    root.d(n)
                    item.children.append(Node(n, item.number + 1))
    for i in item.children:
        fnc(cmds, i, isenb, root)

import sys
if __name__=='__main__':
    n_command = input()
    if(int(n_command) > 0):
        isenb = {}
        cmds = []
        for i in range(0,int(n_command)):
            cmds.append(input().split())
        flag = False
        for c in cmds:
            if "Isenbaev" in c:
                flag = True
                break
        if flag:
            root = Node("Isenbaev", 0)
            isenb["Isenbaev"] = 0
            fnc(cmds, root, isenb, root)
        for c in cmds:
            for i in c:
                if not i in isenb:
                    isenb[i] = "underfined"
        s = ''
        for key,value in sorted(isenb.items()):
            if s == '':
                s += (str(key) + ' ' + str(value))
            else:
                s += ('\n' + str(key) + ' ' + str(value))
        sys.stdout.write(s)
    else:
        sys.stdout.flush()