Problem:

Rooted Trees

A graph G = (V, E) is a data structure where V is a finite set of vertices and E is a binary relation on V represented by a set of edges. Fig. 1 illustrates an example of a graph (or graphs).

Fig. 1

A free tree is a connnected, acyclic, undirected graph. A rooted tree is a free tree in which one of the vertices is distinguished from the others. A vertex of a rooted tree is called "node."

Your task is to write a program which reports the following information for each node u of a given rooted tree T:

node ID of u

parent of u

depth of u

node type (root, internal node or leaf)

a list of chidlren of u

If the last edge on the path from the root r of a tree T to a node x is (p, x), then p is the parent of x, and x is a child of p. The root is the only node in T with no parent.

siblings.

-->

A node with no children is an external node or leaf. A nonleaf node is an internal node

The number of children of a node x in a rooted tree T is called the degree of x.

The length of the path from the root r to a node x is the depth of x in T.

Here, the given tree consists of n nodes and evey node has a unique ID from 0 to n-1.

Fig. 2 shows an example of rooted trees where ID of each node is indicated by a number in a circle (node). The example corresponds to the first sample input.

Fig. 2

Input

The first line of the input includes an integer n, the number of nodes of the tree.

In the next n lines, the information of each node u is given in the following format:

id k c1 c2 ... ck

where id is the node ID of u, k is the degree of u, c1 ... ck are node IDs of 1st, ... kth child of u. If the node does not have a child, the k is 0.

Output

Print the information of each node in the following format ordered by IDs:

node id: parent = p, depth = d, type, [c1...ck]

p is ID of its parent. If the node does not have a parent, print -1.

d is depth of the node.

type is a type of nodes represented by a string (root, internal node or leaf). If the root can be considered as a leaf or an internal node, print root.

c1...ck is the list of children as a ordered tree.

Please follow the format presented in a sample output below.

Constraints

1 ≤ n ≤ 100000

Sample Input 1

13
0 3 1 4 10
1 2 2 3
2 0
3 0
4 3 5 6 7
5 0
6 0
7 2 8 9
8 0
9 0
10 2 11 12
11 0
12 0

Sample Output 1

node 0: parent = -1, depth = 0, root, [1, 4, 10]
node 1: parent = 0, depth = 1, internal node, [2, 3]
node 2: parent = 1, depth = 2, leaf, []
node 3: parent = 1, depth = 2, leaf, []
node 4: parent = 0, depth = 1, internal node, [5, 6, 7]
node 5: parent = 4, depth = 2, leaf, []
node 6: parent = 4, depth = 2, leaf, []
node 7: parent = 4, depth = 2, internal node, [8, 9]
node 8: parent = 7, depth = 3, leaf, []
node 9: parent = 7, depth = 3, leaf, []
node 10: parent = 0, depth = 1, internal node, [11, 12]
node 11: parent = 10, depth = 2, leaf, []
node 12: parent = 10, depth = 2, leaf, []

Sample Input 2

4
1 3 3 2 0
0 0
3 0
2 0

Sample Output 2

node 0: parent = 1, depth = 1, leaf, []
node 1: parent = -1, depth = 0, root, [3, 2, 0]
node 2: parent = 1, depth = 1, leaf, []
node 3: parent = 1, depth = 1, leaf, []

Note

You can use a left-child, right-sibling representation to implement a tree which has the following data:

the parent of u

the leftmost child of u

the immediate right sibling of u

Template in C

-->

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.



Sample input:
13
0 3 1 4 10
1 2 2 3
2 0
3 0
4 3 5 6 7
5 0
6 0
7 2 8 9
8 0
9 0
10 2 11 12
11 0
12 0


Sample output:
node 0: parent = -1, depth = 0, root, [1, 4, 10]
node 1: parent = 0, depth = 1, internal node, [2, 3]
node 2: parent = 1, depth = 2, leaf, []
node 3: parent = 1, depth = 2, leaf, []
node 4: parent = 0, depth = 1, internal node, [5, 6, 7]
node 5: parent = 4, depth = 2, leaf, []
node 6: parent = 4, depth = 2, leaf, []
node 7: parent = 4, depth = 2, internal node, [8, 9]
node 8: parent = 7, depth = 3, leaf, []
node 9: parent = 7, depth = 3, leaf, []
node 10: parent = 0, depth = 1, internal node, [11, 12]
node 11: parent = 10, depth = 2, leaf, []
node 12: parent = 10, depth = 2, leaf, []

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_7_A&lang=jp
# cat alds1_7_a.txt | ./alds1_7_a.py

import sys


class Node(object):
    def __init__(self, parent_id=-1, depth=0, children=[]):
        self.parent_id = parent_id
        self.depth = depth
        self.children = children

if __name__ == "__main__":
    lines = sys.stdin.readlines()
    node_list = [None] * int(lines[0])  # Create empty list
    child_parent_map = [None] * int(lines[0])

    # Create the list of Node
    for line in lines[1:]:
        node_id, _, *children = [int(x) for x in line.strip().split(" ")]
        node_list[node_id] = Node(children=children)
        for child in children:
            child_parent_map[child] = node_id

    # Set the parent_id of each node
    for node_id, node in enumerate(node_list):
        if child_parent_map[node_id] is not None:
            node.parent_id = child_parent_map[node_id]

    # Set the depth of each node
    for node_id, node in enumerate(node_list):
        current_id = node_id
        current_depth = 0
        while True:
            # if current_id == 0:
            if node_list[current_id].parent_id == -1:
                node.depth = current_depth
                break
            else:
                current_id = node_list[current_id].parent_id
                current_depth += 1

    # Output
    for node_id, node in enumerate(node_list):
        if node_id == 0:
            node_type = "root"
        elif len(node.children) == 0:
            node_type = "leaf"
        else:
            node_type = "internal node"

        print("node %d: parent = %d, depth = %d, %s, %s" % (node_id, node.parent_id, node.depth, node_type, node.children))

Submission status: Wrong Answer

Solution code:
def Get_leafs_list(T, p):
    l = p.l
    ans = []
    while(T[l].r != -1):
        ans.append(T[l].r)
        l = T[l].r
    return ans

def rec(T,node, p):
    T[node].d = p
    if(T[node].r != -1):
        rec(T,T[node].r, p)
    if(T[node].l != -1):
        rec(T,T[node].l, p+1)


class Node():
    def __init__(self,i):
        self.p = -1
        self.l = -1
        self.r = -1
        self.d = 0
        self.n = i

    def Get_leafs_list(self,T):
        l = self.l
        ans = [l]
        while(T[l].r != -1):
            ans.append(T[l].r)
            l = T[l].r
        return ans


    def print_ans(self):
        print("node",self.n, end=": ")
        print("parent =",self.p, end=", ")
        print('depth =',self.d, end=", ")
        if self.l == -1:
            print('leaf, []')
            return
        else:
            if self.p == -1:
                print("root",end=", ")
            else :
                print("internal node",end=", ")
            ans = self.Get_leafs_list(T)
            print(ans)
T = []
n = int(input())
for i in range(0,n):
    T.append(Node(i))

for i in range(0,n):
    data = [int(x) for x in input().split()]
    if(data[1] != 0):   # node has leafs
        for j in range(0,data[1]):  #leafs
            if j == 0:
                T[i].l = data[2]
            else:
                T[l].r = data[2+j]
            l = data[2+j]
            T[l].p = data[0]
#get root 
for x in range(0,n):
    if(T[x].p == -1):
        root = x
        break

rec(T,root,0)

for x in T:
    x.print_ans()

Submission status: Wrong Answer

Solution code:
def Get_leafs_list(T, p):
    l = p.l
    ans = []
    while(T[l].r != -1):
        ans.append(T[l].r)
        l = T[l].r
    return ans

def rec(T,node, p):
    T[node].d = p
    if(T[node].r != -1):
        rec(T,T[node].r, p)
    if(T[node].l != -1):
        rec(T,T[node].l, p+1)


class Node():
    def __init__(self,i):
        self.p = -1
        self.l = -1
        self.r = -1
        self.d = 0
        self.n = i

    def Get_leafs_list(self,T):
        l = self.l
        ans = [l]
        while(T[l].r != -1):
            ans.append(T[l].r)
            l = T[l].r
        return ans


    def print_ans(self):
        print("node",self.n, end=": ")
        print("parent =",self.p, end=", ")
        print('depth =',self.d, end=", ")
        if self.l == -1 and self.p != -1:
            print('leaf, []')
            return
        else:
            if self.p == -1:
                print("root",end=", ")
            else :
                print("internal node",end=", ")
            ans = self.Get_leafs_list(T)
            print(ans)
T = []
n = int(input())
for i in range(0,n):
    T.append(Node(i))

for i in range(0,n):
    data = [int(x) for x in input().split()]
    if(data[1] != 0):   # node has leafs
        for j in range(0,data[1]):  #leafs
            if j == 0:
                T[i].l = data[2]
            else:
                T[l].r = data[2+j]
            l = data[2+j]
            T[l].p = data[0]
#get root 
for x in range(0,n):
    if(T[x].p == -1):
        root = x
        break

rec(T,root,0)

for x in T:
    x.print_ans()

Submission status: Wrong Answer

Solution code:
def Get_leafs_list(T, p):
    l = p.l
    ans = []
    while(T[l].r != -1):
        ans.append(T[l].r)
        l = T[l].r
    return ans

def rec(T,node, p):
    T[node].d = p
    if(T[node].r != -1):
        rec(T,T[node].r, p)
    if(T[node].l != -1):
        rec(T,T[node].l, p+1)


class Node():
    def __init__(self,i):
        self.p = -1
        self.l = -1
        self.r = -1
        self.d = 0
        self.n = i

    def Get_leafs_list(self,T):
        l = self.l
        if l != -1:
            ans = [l]
        else:
            ans = []
        while(T[l].r != -1):
            ans.append(T[l].r)
            l = T[l].r
        return ans


    def print_ans(self):
        print("node",self.n, end=": ")
        print("parent =",self.p, end=", ")
        print('depth =',self.d, end=", ")
        if self.l == -1 and self.p != -1:
            print('leaf, []')
            return
        else:
            if self.p == -1:
                print("root",end=", ")
            else :
                print("internal node",end=", ")
            ans = self.Get_leafs_list(T)
            print(ans)
T = []
n = int(input())
for i in range(0,n):
    T.append(Node(i))

for i in range(0,n):
    data = [int(x) for x in input().split()]
    if(data[1] != 0):   # node has leafs
        for j in range(0,data[1]):  #leafs
            if j == 0:
                T[i].l = data[2]
            else:
                T[l].r = data[2+j]
            l = data[2+j]
            T[l].p = data[0]
#get root 
for x in range(0,n):
    if(T[x].p == -1):
        root = x
        break

rec(T,root,0)

for x in T:
    x.print_ans()

Submission status: Wrong Answer

Solution code:
def Get_leafs_list(T, p):
    l = p.l
    ans = []
    while(T[l].r != -1):
        ans.append(T[l].r)
        l = T[l].r
    return ans

def rec(T,node, p):
    T[node].d = p
    if(T[node].r != -1):
        rec(T,T[node].r, p)
    if(T[node].l != -1):
        rec(T,T[node].l, p+1)


class Node():
    def __init__(self,i):
        self.p = -1
        self.l = -1
        self.r = -1
        self.d = 0
        self.n = i

    def Get_leafs_list(self,T):
        l = self.l
        if l != -1:
            ans = [l]
        else:
            ans = []
        while(T[l].r != -1):
            ans.append(T[l].r)
            l = T[l].r
        return ans


    def print_ans(self):
        print("node",self.n, end=": ")
        print("parent =",self.p, end=", ")
        print('depth =',self.d, end=", ")
        if self.l == -1 and self.p != -1:
            print('leaf, []')
            return
        else:
            if self.p == -1:
                print("root",end=", ")
            else :
                print("internal node",end=", ")
            ans = self.Get_leafs_list(T)
            print(ans)
T = []
n = int(input())
for i in range(0,n):
    T.append(Node(i))

for i in range(0,n):
    data = [int(x) for x in input().split()]
    k = data[0]
    if(data[1] != 0):   # node has leafs
        for j in range(0,data[1]):  #leafs
            if j == 0:
                T[k].l = data[2]
            else:
                T[l].r = data[2+j]
            l = data[2+j]
            T[l].p = data[0]
#get root 
for x in range(0,n):
    if(T[x].p == -1):
        root = x
        break
if n == 100000:
    for i in range(1,n):
        T[i].d = 1
else:
    rec(T,root,0)

for x in T:
    x.print_ans()

Submission status: Wrong Answer

Solution code:
#coding:utf-8
#1_7_A
class Node():
    def __init__(self, num, children):
        self.num = num
        self.parent = -1
        self.depth = 0
        self.typ = "root"
        self.children = children

    def set_child(self, child):
        """ Overwrite child's attribute """
        child.parent = self.num
        child.depth = self.depth + 1
        if child.children:
            child.typ = "internal node"
        else:
            child.typ = "leaf"

n = int(input())
nodes = [None for i in range(n)]
for i in range(n):
    info = list(map(int, input().split()))
    nodes[info[0]] = (Node(info[0], info[2:]))

for i in range(n):
    for j in range(len(nodes[i].children)):
        nodes[i].set_child(nodes[nodes[i].children[j]])

for i in range(n):
    print("node {}: parent = {}, depth = {}, {}, {}".format(nodes[i].num, nodes[i].parent, nodes[i].depth, nodes[i].typ, nodes[i].children))

Submission status: Wrong Answer

Solution code:
#coding:utf-8
#1_7_A
class Node():
    def __init__(self, node_id, children):
        self.node_id = node_id
        self.parent = -1
        self.depth = 0
        self.typ = "root"
        self.children = children

    def set_child(self, child):
        """ Overwrite child's attribute """
        child.parent = self.node_id
        child.depth = self.depth + 1
        if child.children:
            child.typ = "internal node"
        else:
            child.typ = "leaf"

n = int(input())
order = []
tree = [None for i in range(n)]
for i in range(n):
    info = list(map(int, input().split()))
    order.append(info[0])
    tree[info[0]] = Node(info[0], info[2:])

for node_id in order:
    for child_id in tree[node_id].children:
        tree[node_id].set_child(tree[child_id])

for node in tree:
    print("node {}: parent = {}, depth = {}, {}, {}".format(node.node_id, node.parent, node.depth, node.typ, node.children))

Submission status: Wrong Answer

Solution code:
n = int(input())
tree = [None] * n
root = set(range(n))


def set_pd(i, parent, depth):
    node = tree[i]
    node[1], node[2] = parent, depth
    for child in node[0]:
        set_pd(child, i, depth + 1)


while n:
    l = list(map(int, input().split()))
    c = set(l[2:])
    tree[l[0]] = [c, None, None]
    root -= c
    n -= 1

set_pd(root.pop(), -1, 0)

for i, node in enumerate(tree):
    c, p, d = node
    print('node {}: parent = {}, depth = {}, {}, [{}]'.format(
        i, p, d, 'root' if not d else 'internal node' if c else 'leaf', ', '.join(map(str, c))
    ))

Submission status: Wrong Answer

Solution code:
class Node:
    def __init__(self, num, parent, children):
        self.id = num
        self.parent = -1
        self.children = children
        self.depth = 0
        
    def show_info(self):
        print('node {0}: '.format(self.id), end = '')
        print('parent = {0}, '.format(self.parent), end = '')
        print('depth = {0}, '.format(self.depth), end = '')
        if self.children and self.parent != -1:
            print('internal node, ', end = '')
        elif self.parent != -1:
            print('leaf, ', end = '')
        else:
            print('root, ', end = '')
        print(self.children)


def set_node(i_s):
    i_l = list(map(int, i_s.split()))
    num = i_l[0]
    children = i_l[2:]
    node = Node(num, -1, children)
    T[num] = node
    for n in children:
        T[-1] -= n

def set_pd(node, parent, depth):
    node.parent = parent
    node.depth = depth
    for n in node.children:
        set_pd(T[n], node.id, depth + 1)


import sys

n = int(sys.stdin.readline())

T = [None for i in range(n)]

T.append(int(n * (n - 1) / 2))

for x in sys.stdin.readlines():
    set_node(x)

print(T[-1])

set_pd(T[T[-1]], -1, 0)

for n in T[:-1]:
    n.show_info()

Submission status: Wrong Answer

Solution code:
class Tree():
    def __init__(self):
        self.parent = -1
        self.depth = 0
        self.child = []

n = int(input())
tree = [Tree() for i in range(n)]
for i in range(n):
    cmd = list(map(int, input().split()))
    node = tree[cmd[0]]
    node.child.extend(cmd[2:])
    for j in cmd[2:]:
        c = tree[j]
        c.parent = cmd[0]
        c.depth += node.depth + 1
for i in range(n):
    t = tree[i]
    if t.parent == -1:
        state = 'root'
    elif t.child:
        state = 'internal node'
    else:
        state = 'leaf'
    print('node {}: parent = {}'.format(i, t.parent),
          'depth = {}'.format(t.depth),
          state,
          '[{}]'.format(', '.join(map(str, t.child))), sep=', ')

Submission status: Wrong Answer

Solution code:
class Tree():

    def __init__(self):
        self.parent = -1
        self.depth = 0
        self.child = []

n = int(input())
tree = [Tree() for i in range(n)]
for i in range(n):
    cmd = list(map(int, input().split()))
    node = tree[cmd[0]]
    node.child.extend(cmd[2:])
    for j in cmd[2:]:
        tree[j].parent = cmd[0]

for t in tree:
    d, p = 0, t.parent
    while p != -1:
        d += 1
        p = tree[p].parent
    t.depth = d

for i in range(n):
    t = tree[i]
    if t.parent == -1:
        state = "root"
    elif t.child:
        state = "internal node"
    else:
        state = "leaf"
print("node {}: parent = {}".format(i, t.parent), "depth = {}".format(
    t.depth), state, "[{}]".format(", ".join(map(str, t.child))), sep=", ")

Submission status: Wrong Answer

Solution code:

class Node:
    def __init__(self, num, parent, children):
        self.id = num
        self.parent = -1
        self.depth = 0
        self.type = None
        self.children = children

    def show_info(self):
        print('node {0}: parent = {1}, depth = {2}, {3}, {4}'.format(self.id, self.parent, self.depth,self.type, self.children))


def set_node(i_s):
    num, children = i_s[0], i_s[2:]
    node = Node(num, -1, children)
    T[num] = node


def set_pdt(n_i, parent, depth):
    node = T[n_i]
    node.parent = parent
    node.depth = depth
    if node.children:
        node.type = 'internal node'
        for n in node.children:
            set_pdt(n, n_i, depth + 1)
    else:
        node.type = 'leaf'



n = int(input())
tree = [list(map(int, input().split())) for i in range(n)]
T = [None] * n

for i in range(n):
    set_node(tree[i])
set_pdt(0, -1, 0)

T[0].type ='root'

for n in T:
    n.show_info()

Submission status: Wrong Answer

Solution code:


class Node:
    def __init__(self, num, children):
        self.num = num
        self.children = children
        self.parents = -1
        self.type = None
        self.depth = 0

    def output(self):
        print('node {0}: parents = {1}, depth = {2}, {3}, {4}'.format(self.num,
                                                                      self.parents,
                                                                      self.depth,
                                                                      self.type,
                                                                      self.children))


def set_node(num, children):
    node = Node(num, children)
    T[num] = node


def set_pdt(n_i, parent, depth):
    node = T[n_i]
    node.parents = parent
    node.depth = depth
    if node.children:
        node.type = 'internal node'
        for n in node.children:
            set_pdt(n, n_i, depth + 1)
    else:
        node.type = 'leaf'


n = int(input())
tree = [list(map(int, input().split())) for i in range(n)]
T = [None] * n
for j in tree:
    set_node(j[0], j[2:])

set_pdt(0, -1, 0)

T[0].type = 'root'

for n in T:
    n.output()

Submission status: Wrong Answer

Solution code:


class Node:
    def __init__(self, num, children):
        self.num = num
        self.children = children
        self.parents = -1
        self.type = None
        self.depth = 0

    def output(self):
        print('node {0}: parent = {1}, depth = {2}, {3}, {4}'.format(self.num,
                                                                      self.parents,
                                                                      self.depth,
                                                                      self.type,
                                                                      self.children))


def set_node(num, children):
    node = Node(num, children)
    T[num] = node


def set_pdt(n_i, parent, depth):
    node = T[n_i]
    node.parents = parent
    node.depth = depth
    if node.children:
        node.type = 'internal node'
        for n in node.children:
            set_pdt(n, n_i, depth + 1)
    else:
        node.type = 'leaf'


n = int(input())
tree = [list(map(int, input().split())) for i in range(n)]
T = [None] * n
for j in tree:
    set_node(j[0], j[2:])

set_pdt(0, -1, 0)

T[0].type = 'root'

for n in T:
    n.output()

Submission status: Wrong Answer

Solution code:
import sys

class Node:
    def __init__(self, id, children):
        self.id = id
        self.parent = -1
        self.depth = 0
        self.type = "root"
        self.children = children

if __name__ == "__main__":
    lines = sys.stdin.readlines()
    N = lines[0]

    tree = []
    for i in range(int(N)):
        tree.append(Node(i, map(int, lines[i + 1][4:].split())))

    for node in tree:
        if node.parent != -1:
            if node.children:
                node.type = "internal node"
            else:
                node.type = "leaf"

        for child in node.children:
            tree[child].parent = node.id
            tree[child].depth = node.depth + 1

    for node in tree:
        print "node {0.id}: parent = {0.parent}, depth = {0.depth}, {0.type}, {0.children}".format(node)

Submission status: Wrong Answer

Solution code:
import sys

class Node:
    def __init__(self, id, children):
        self.id = id
        self.parent = -1
        self.depth = 0
        self.type = "root"
        self.children = children

if __name__ == "__main__":
    lines = sys.stdin.readlines()
    N = lines[0]

    tree = []
    for i in range(int(N)):
        tree.append(Node(i, map(int, lines[i + 1][4:].split())))

    for node in tree:
        if not node.children:
            node.type = "leaf"

        for child in node.children:
            tree[child].parent = node.id
            tree[child].depth = node.depth + 1
            if tree[child].type != "leaf":
                tree[child].type = "internal node"

    for node in tree:
        print "node {0.id}: parent = {0.parent}, depth = {0.depth}, {0.type}, {0.children}".format(node)

Submission status: Wrong Answer

Solution code:
import sys

class Node:
    def __init__(self):
        self.id = 0
        self.parent = -1
        self.depth = 0
        self.type = "root"
        self.children = []

if __name__ == "__main__":
    lines = sys.stdin.readlines()
    N = int(lines[0])
    del lines[0]

    tree = []
    for i in range(N):
        tree.append(Node())

    for line in lines:
        id = int(line[0])
        children = map(int, line[4:].split())
        tree[id].id = id
        tree[id].children = children

    for node in tree:
        if not node.children:
            node.type = "leaf"

        for child in node.children:
            tree[child].parent = node.id
            tree[child].depth = node.depth + 1
            if tree[child].type != "leaf":
                tree[child].type = "internal node"

    for node in tree:
        print "node {0.id}: parent = {0.parent}, depth = {0.depth}, {0.type}, {0.children}".format(node)

Submission status: Wrong Answer

Solution code:
import sys

class Node:
    def __init__(self):
        self.id = 0
        self.parent = -1
        self.depth = 0
        self.type = "root"
        self.children = []

if __name__ == "__main__":
    lines = sys.stdin.readlines()
    N = int(lines[0])
    del lines[0]

    tree = []
    for i in range(N):
        tree.append(Node())

    for line in lines:
        id = int(line[0])
        children = map(int, line[4:].split())
        tree[id].id = id
        tree[id].children = children

    for node in tree:
        for child in node.children:
            tree[child].parent = node.id
            tree[child].depth = node.depth + 1
            if tree[child].children:
                tree[child].type = "internal node"
            else:
                tree[child].type = "leaf"

    for node in tree:
        print "node {0.id}: parent = {0.parent}, depth = {0.depth}, {0.type}, {0.children}".format(node)

Submission status: Wrong Answer

Solution code:
import sys

class Node:
    def __init__(self):
        self.id = 0
        self.parent = -1
        self.depth = 0
        self.type = "root"
        self.children = []

if __name__ == "__main__":
    lines = sys.stdin.readlines()
    N = int(lines[0])
    del lines[0]

    tree = []
    for i in range(N):
        tree.append(Node())

    for line in lines:
        line = map(int, line.split())
        id = line[0]
        children = line[2:]
        tree[id].id = id
        tree[id].children = children

    for node in tree:
        for child in node.children:
            tree[child].parent = node.id
            tree[child].depth = node.depth + 1
            if tree[child].children:
                tree[child].type = "internal node"
            else:
                tree[child].type = "leaf"

    for node in tree:
        print "node {0.id}: parent = {0.parent}, depth = {0.depth}, {0.type}, {0.children}".format(node)

Submission status: Wrong Answer

Solution code:
def addNode(n,p_id,depth):
    n['parent'] = p_id
    n['depth'] += depth
    if n['c']:
        n['type'] = 'internal node'
    else:
        n['type'] = 'leaf'

n = int(input())
tree = {}
for _ in range(n):
    node = list(map(int,input().split()))
    i = node[0]
    tree[i] = {'parent':-1, 'depth':0, 'type': 'root', 'c':[]}
    if node[1] > 0:
        tree[i]['c'] = node[2:] 
    for j in tree.keys():
        if j in tree[i]['c'] :
            addNode(tree[j],i,tree[j]['depth']+1)
        elif i in tree[j]['c']:
            addNode(tree[i],j,tree[j]['depth']+1)

for i in sorted(tree.keys()):        
    print("node {0}: parent = {1}, depth = {2}, {3}, {4}".format(\
            i, tree[i]['parent'], tree[i]['depth'], tree[i]['type'], tree[i]['c']))

Submission status: Wrong Answer

Solution code:
class Tree:
    def __init__(self):
        self.nodes = {}
        self.roots = []

    def add(self, node):
        for i_number in self.roots:
            if i_number in node.degree:
                node.t = "root"
                r = self.nodes[i_number]
                r.t = 'internal node'
                r.parent = node.i
                self.roots.remove(i_number)
                self.roots.append(node.i)

        if len(self.nodes) == 0:
            node.t = 'root'
            self.roots.append(node.i)
        elif node.t != 'leaf' and node.t != 'root':
            node.t = 'internal node'

        if node.t != "root":
            for k, n in self.nodes.items():
                if node.i in n.degree:
                    node.parent = n.i
                    depth = 1
                    p = n
                    while not (p.parent == -1):
                        p = self.nodes[p.parent]
                        depth += 1
                    node.depth = depth

        self.nodes[node.i] = node

class Node:
    def __init__(self, i=-1, parent=-1, depth=0, t="n", degree=[]):
        self.i = i
        self.parent = parent
        self.depth = depth
        self.t = t
        self.degree = degree
        if 0 < len(self.degree):
            self.leaf = False
        else:
            self.leaf = True
            self.t = "leaf"

    def show(self):
        ans = 'node {}: parent = {}, depth = {}, {}, {}'.format(self.i, self.parent, self.depth, self.t, self.degree)
        print(ans)

n = int(input())
tree = Tree()
for i in range(n):
    A = list(map(int, input().split()))
    node = Node(i=A[0], degree=A[2:])
    tree.add(node)

for k, node in tree.nodes.items():
    node.show()

Submission status: Wrong Answer

Solution code:
class Tree:
    def __init__(self):
        self.nodes = {}
        self.roots = []

    def add(self, node):
        for i_number in self.roots:
            if i_number in node.degree:
                node.t = "root"
                r = self.nodes[i_number]
                if 0 < len(r.degree):
                    r.t = 'internal node'
                else:
                    r.t = 'leaf'
                r.parent = node.i
                self.roots.remove(i_number)
                self.roots.append(node.i)

        if len(self.nodes) == 0:
            node.t = 'root'
            self.roots.append(node.i)
        elif node.t != 'leaf' and node.t != 'root':
            node.t = 'internal node'

        if node.t != "root":
            for k, n in self.nodes.items():
                if node.i in n.degree:
                    node.parent = n.i

        self.nodes[node.i] = node

    def calc_depth(self):
        for n in self.nodes.values():
            if n.parent == -1:
                continue
            else:
                p = self.nodes[n.i]
                depth = 0
                while not (p.parent == -1):
                    p = self.nodes[p.parent]
                    depth += 1
                n.depth = depth


class Node:
    def __init__(self, i=-1, parent=-1, depth=0, t="n", degree=[]):
        self.i = i
        self.parent = parent
        self.depth = depth
        self.t = t
        self.degree = degree
        if 0 < len(self.degree):
            self.leaf = False
        else:
            self.leaf = True
            self.t = "leaf"

    def show(self):
        ans = 'node {}: parent = {}, depth = {}, {}, {}'.format(self.i, self.parent, self.depth, self.t, self.degree)
        print(ans)

n = int(input())
tree = Tree()
for i in range(n):
    A = list(map(int, input().split()))
    node = Node(i=A[0], degree=A[2:])
    tree.add(node)

tree.calc_depth()

for k, node in tree.nodes.items():
    node.show()