s_id string | p_id string | u_id string | date string | language string | original_language string | filename_ext string | status string | cpu_time string | memory string | code_size string | code string | error string | stdout string |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
s347631973 | p02279 | u851695354 | 1489593783 | Python | Python3 | py | Runtime Error | 20 | 7772 | 1416 | class Tree:
def __init__(self,parent,left,right):
self.parent = parent
self.left = left
self.right = right
def setdepth(u, p):
depths[u] = p
if trees[u].right != -1:
# ???????????????????????±???????¨????
setdepth(trees[u].right, p)
if trees[u].left != -1:
# ??????????????????????????±???+1????¨????
setdepth(trees[u].left, p+1)
N = int(input())
# ?????????
trees = [Tree(-1,-1,-1) for i in range(N)]
for i in range(N):
l = list(map(int, input().split()))
no = l[0]
jisu = l[1]
# 2???????????????????????§???????´??????????
childs = l[2:]
for j in range(jisu):
child = childs[j]
if j == 0:
trees[no].left = child
if j != len(childs)-1:
trees[child].right = childs[j+1]
trees[child].parent = no
r = 0
for i in range(N):
if trees[i].parent == -1:
r = i
depths = [-1] * N
setdepth(r, 0)
for i in range(N):
type = ""
if trees[i].parent == -1: type = "root"
elif trees[i].left == -1: type = "leaf"
else: type = "internal node"
childs = "["
child = trees[i].left
while child != -1:
childs += str(child)
child = trees[child].right
if child != -1: childs += ", "
childs += "]"
print("node {0}: parent = {1}, depth = {2}, {3}, {4}".format(i, trees[i].parent, depths[i], type, childs)) | Traceback (most recent call last):
File "/tmp/tmpgu4sr240/tmpojm58ccy.py", line 18, in <module>
N = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s810513317 | p02279 | u851695354 | 1489594520 | Python | Python3 | py | Runtime Error | 710 | 72020 | 1457 | import sys
class Tree:
def __init__(self,parent,left,right):
self.parent = parent
self.left = left
self.right = right
def setdepth(u, p):
depths[u] = p
if trees[u].right != -1:
# ???????????????????????±???????¨????
setdepth(trees[u].right, p)
if trees[u].left != -1:
# ??????????????????????????±???+1????¨????
setdepth(trees[u].left, p+1)
sys.setrecursionlimit(200000)
N = int(input())
# ?????????
trees = [Tree(-1,-1,-1) for i in range(N)]
for i in range(N):
l = list(map(int, input().split()))
no = l[0]
jisu = l[1]
# 2???????????????????????§???????´??????????
childs = l[2:]
for j in range(jisu):
child = childs[j]
if j == 0:
trees[no].left = child
if j != len(childs)-1:
trees[child].right = childs[j+1]
trees[child].parent = no
r = 0
for i in range(N):
if trees[i].parent == -1:
r = i
depths = [-1] * N
setdepth(r, 0)
for i in range(N):
type = ""
if trees[i].parent == -1: type = "root"
elif trees[i].left == -1: type = "leaf"
else: type = "internal node"
childs = "["
child = trees[i].left
while child != -1:
childs += str(child)
child = trees[child].right
if child != -1: childs += ", "
childs += "]"
print("node {0}: parent = {1}, depth = {2}, {3}, {4}".format(i, trees[i].parent, depths[i], type, childs)) | Traceback (most recent call last):
File "/tmp/tmpcylx1v7o/tmpgly2o1c_.py", line 20, in <module>
N = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s504449296 | p02279 | u370086573 | 1493981083 | Python | Python3 | py | Runtime Error | 0 | 0 | 1241 | class Node:
parent = NIL
left = NIL
right = NIL
label = ""
def show_info(u):
global T, D
if T[i].parent == NIL:
T[u].label = "root"
elif T[i].left == NIL:
T[u].label = "leaf"
else:
T[u].label = "internal node"
print('node {0}: parent = {1}, depth = {2}, {3}, {4}'.format(u, T[u].parent, D[u], T[u].label, getChildren(u)))
def getChildren(u):
global T
c = T[u].left
R =[]
while c != NIL:
R.append(c)
c = T[c].right
return R
def setDepth(u,p):
global T, D
D[u] = p
if T[u].right != NIL:
setDepth(T[u].right, p)
if T[u].left != NIL:
setDepth(T[u].left, p + 1)
if __name__ == "__main__":
n = int(input())
T = [None] * n
D = [None] * n
for i in range(n):
T[i] = Node()
for i in range(n):
tmp = list(map(int, input().split()))
id = tmp.pop(0)
k = tmp.pop(0)
C = tmp
if k != 0:
T[id].left = C[0]
for j in range(len(C)):
T[C[j]].parent = id
for j in range(len(C) - 1):
T[C[j]].right = C[j + 1]
# ??±????¨????
setDepth(0, 0)
for i in range(n):
show_info(i) | Traceback (most recent call last):
File "/tmp/tmp6yefgw4a/tmpi3a8h1v0.py", line 1, in <module>
class Node:
File "/tmp/tmp6yefgw4a/tmpi3a8h1v0.py", line 2, in Node
parent = NIL
^^^
NameError: name 'NIL' is not defined
| |
s246975764 | p02279 | u370086573 | 1493981121 | Python | Python3 | py | Runtime Error | 0 | 0 | 1241 | class Node:
parent = NIL
left = NIL
right = NIL
label = ""
def show_info(u):
global T, D
if T[i].parent == NIL:
T[u].label = "root"
elif T[i].left == NIL:
T[u].label = "leaf"
else:
T[u].label = "internal node"
print('node {0}: parent = {1}, depth = {2}, {3}, {4}'.format(u, T[u].parent, D[u], T[u].label, getChildren(u)))
def getChildren(u):
global T
c = T[u].left
R =[]
while c != NIL:
R.append(c)
c = T[c].right
return R
def setDepth(u,p):
global T, D
D[u] = p
if T[u].right != NIL:
setDepth(T[u].right, p)
if T[u].left != NIL:
setDepth(T[u].left, p + 1)
if __name__ == "__main__":
n = int(input())
T = [None] * n
D = [None] * n
for i in range(n):
T[i] = Node()
for i in range(n):
tmp = list(map(int, input().split()))
id = tmp.pop(0)
k = tmp.pop(0)
C = tmp
if k != 0:
T[id].left = C[0]
for j in range(len(C)):
T[C[j]].parent = id
for j in range(len(C) - 1):
T[C[j]].right = C[j + 1]
# ??±????¨????
setDepth(0, 0)
for i in range(n):
show_info(i) | Traceback (most recent call last):
File "/tmp/tmpy3yzxwih/tmpvso5_4i4.py", line 1, in <module>
class Node:
File "/tmp/tmpy3yzxwih/tmpvso5_4i4.py", line 2, in Node
parent = NIL
^^^
NameError: name 'NIL' is not defined
| |
s863472533 | p02279 | u370086573 | 1493982005 | Python | Python3 | py | Runtime Error | 20 | 7824 | 1411 | NIL = -1
class Node:
parent = NIL
left = NIL
right = NIL
label = ""
def show_info(u):
global T, D
print('node {0}: parent = {1}, depth = {2}, {3}, {4}'.format(u, T[u].parent, D[u], T[u].label, getChildren(u)))
def setLabel(n):
for i in range(n):
if T[i].parent == NIL:
T[i].label = "root"
elif T[i].left == NIL:
T[i].label = "leaf"
else:
T[i].label = "internal node"
def getChildren(u):
global T
c = T[u].left
R =[]
while c != NIL:
R.append(c)
c = T[c].right
return R
def setDepth(u,p):
global T, D
D[u] = p
if T[u].right != NIL:
setDepth(T[u].right, p)
if T[u].left != NIL:
setDepth(T[u].left, p + 1)
if __name__ == "__main__":
n = int(input())
T = [None] * n
D = [0] * n
for i in range(n):
T[i] = Node()
for i in range(n):
tmp = list(map(int, input().split()))
id = tmp.pop(0)
k = tmp.pop(0)
C = tmp
if k != 0:
T[id].left = C[0]
for j in range(len(C)):
T[C[j]].parent = id
for j in range(len(C) - 1):
T[C[j]].right = C[j + 1]
setLabel(n)
for i in range(n):
if T[i].label == "root":
root = i
# ??±????¨????
setDepth(root, 0)
for i in range(n):
show_info(i) | Traceback (most recent call last):
File "/tmp/tmpl__5er2t/tmpet8wxenb.py", line 46, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s231836538 | p02279 | u370086573 | 1493983619 | Python | Python3 | py | Runtime Error | 20 | 7716 | 1168 | NIL = -1
class Node:
parent = NIL
left = NIL
right = NIL
label = ""
def setDepth(u,d):
D[u] = d
if T[u].left != NIL:
setDepth(T[u].left,d + 1)
if T[u].right != NIL:
setDepth(T[u].right, d)
def getChildren(u):
c = T[u].left
result = []
while c != NIL:
result.append(c)
c = T[c].right
return result
n = int(input())
T = [0] * n
D = [0] * n
root_num = NIL
# ?????????????????¨??????
for i in range(n):
T[i] = Node()
for i in range(n):
tmp = list(map(int, input().split()))
id = tmp.pop(0)
k = tmp.pop(0)
C = tmp
if k != 0:
for c in C:
T[c].parent = id
T[id].left = C[0]
for j in range(len(C) - 1):
T[C[j]].right = C[j + 1]
# ??????????????°&??±????¨????
for i in range(n):
if T[i].parent == NIL:
root_num = i
T[i].label = "root"
elif T[i].left == NIL:
T[i].label = "leaf"
else:
T[i].label = "internal node"
setDepth(root_num, 0)
for i in range(n):
print("node {0}: parent = {1}, depth = {2}, {3}, {4}".format(i, T[i].parent, D[i], T[i].label, getChildren(i))) | Traceback (most recent call last):
File "/tmp/tmpolohavdw/tmppneb23ga.py", line 28, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s315737401 | p02279 | u603049633 | 1496297030 | Python | Python3 | py | Runtime Error | 0 | 0 | 1017 | 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(self):
print('node {0}: parent = {1}, depth = {2}, {3}, {4}'.format(self.id,self.parent,self.depth,self.type,self.children))
def set_node(node_data):
L = list(map(int, node_data.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_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())
T = [None] * n
T.append(int(n * (n - 1) / 2))
for i in range(n):
x = input()
set_node(x)
set_pdt(T[-1], -1, 0)
T[T[-1]].type ='root'
for n in T[:-1]:
n.show() | Traceback (most recent call last):
File "/tmp/tmpfd6var5w/tmp86u5oaxt.py", line 33, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s805276496 | p02279 | u091533407 | 1499484337 | Python | Python3 | py | Runtime Error | 30 | 7836 | 1298 | import sys
def dep(u, p):
D[u] = p
if tree[u][2] != NIL:
dep(tree[u][2], p)
if tree[u][1] != NIL:
dep(tree[u][1], p+1)
def print_ans():
for i in range(n):
if tree[i][0] == NIL:
typea = "root"
elif tree[i][1] == NIL:
typea = "leaf"
else:
typea = "internal node"
child = []
b = tree[i]
if b[1] != NIL:
child.append(b[1])
b = tree[b[1]]
while b[2] != NIL:
child.append(b[2])
b = tree[b[2]]
print("node {}: parent = {}, depth = {}, {}, {}"
.format(i, tree[i][0], D[i], typea, child))
if __name__ == "__main__":
NIL = -1
n = int(sys.stdin.readline())
tree = [[NIL, NIL, NIL] for i in range(n)]
D = [0 for i in range(n)]
for a in sys.stdin.readlines():
a = list(map(int, a.split()))
if a[1] > 0:
tree[a[0]][1] = a[2] # ???????????????
for i in range(a[1]):
tree[a[i+2]][0] = a[0] # ????????????
if i != a[1] - 1:
tree[a[i+2]][2] = a[i+3] # ??????????????????
for i in range(n):
if tree[i][0] == NIL:
r = i
break
dep(r, 0)
print_ans() | Traceback (most recent call last):
File "/tmp/tmp5_13jgbb/tmpazqklkna.py", line 34, in <module>
n = int(sys.stdin.readline())
^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: ''
| |
s894313617 | p02279 | u091533407 | 1499484917 | Python | Python3 | py | Runtime Error | 30 | 7792 | 1118 | import sys
def dep(u, p):
D[u] = p
if tree[u][2] != NIL:
dep(tree[u][2], p)
if tree[u][1] != NIL:
dep(tree[u][1], p+1)
def print_ans():
for i in range(n):
if tree[i][0] == NIL:
typea = "root"
elif tree[i][1] == NIL:
typea = "leaf"
else:
typea = "internal node"
print("node {}: parent = {}, depth = {}, {}, {}"
.format(i, tree[i][0], D[i], typea, tree[i][3]))
if __name__ == "__main__":
NIL = -1
n = int(sys.stdin.readline())
tree = [[NIL, NIL, NIL] for i in range(n)]
D = [0 for i in range(n)]
for a in sys.stdin.readlines():
a = list(map(int, a.split()))
tree[a[0]].append(a[2:])
if a[1] > 0:
tree[a[0]][1] = a[2] # ???????????????
for i in range(a[1]):
tree[a[i+2]][0] = a[0] # ????????????
if i != a[1] - 1:
tree[a[i+2]][2] = a[i+3] # ??????????????????
for i in range(n):
if tree[i][0] == NIL:
r = i
break
dep(r, 0)
print_ans() | Traceback (most recent call last):
File "/tmp/tmptehfwmw2/tmpn6fw8gcc.py", line 26, in <module>
n = int(sys.stdin.readline())
^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: ''
| |
s909488974 | p02279 | u091533407 | 1499485434 | Python | Python3 | py | Runtime Error | 20 | 7828 | 1099 | import sys
def dep(u, p):
D[u] = p
if tree[u][2] != NIL:
dep(tree[u][2], p)
if tree[u][1] != NIL:
dep(tree[u][1], p+1)
def print_ans():
for i in range(n):
print("node {}: parent = {}, depth = {}, {}, {}"
.format(i, tree[i][0], D[i], tree[i][4], tree[i][3]))
if __name__ == "__main__":
NIL = -1
n = int(sys.stdin.readline())
tree = [[NIL, NIL, NIL] for i in range(n)]
D = [0 for i in range(n)]
for a in sys.stdin.readlines():
a = list(map(int, a.split()))
tree[a[0]].append(a[2:])
if a[1] > 0:
tree[a[0]][1] = a[2] # ???????????????
for i in range(a[1]):
tree[a[i+2]][0] = a[0] # ????????????
if i != a[1] - 1:
tree[a[i+2]][2] = a[i+3] # ??????????????????
for i in range(n):
if tree[i][0] == NIL:
tree[i].append("root")
r = i
elif tree[i][1] == NIL:
tree[i].append("leaf")
else:
tree[i].append("internal node")
dep(r, 0)
print_ans() | Traceback (most recent call last):
File "/tmp/tmpaaida3ix/tmpgr2q85p6.py", line 20, in <module>
n = int(sys.stdin.readline())
^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: ''
| |
s354701814 | p02279 | u091533407 | 1499486147 | Python | Python3 | py | Runtime Error | 30 | 7812 | 1044 | import sys
def dep(u, p):
D[u] = p
if tree[u][2] != NIL:
dep(tree[u][2], p)
if tree[u][1] != NIL:
dep(tree[u][1], p+1)
def print_ans():
for i in range(n):
print("node {}: parent = {}, depth = {}, {}, {}"
.format(i, tree[i][0], D[i],
"root" if tree[i][0] == NIL else "leaf" if tree[i][1] == NIL else "internal node",
tree[i][3]))
if __name__ == "__main__":
NIL = -1
n = int(sys.stdin.readline())
tree = [[NIL, NIL, NIL] for i in range(n)]
D = [0 for i in range(n)]
root = set(range(n))
for a in sys.stdin.readlines():
a = list(map(int, a.split()))
tree[a[0]].append(a[2:])
if a[1] > 0:
tree[a[0]][1] = a[2] # ???????????????
for i in range(a[1]):
tree[a[i+2]][0] = a[0] # ????????????
if i != a[1] - 1:
tree[a[i+2]][2] = a[i+3] # ??????????????????
root -= set(a[2:])
dep(root.pop(), 0)
print_ans() | Traceback (most recent call last):
File "/tmp/tmpscsz3l4o/tmpm7sufvbp.py", line 22, in <module>
n = int(sys.stdin.readline())
^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: ''
| |
s298757286 | p02279 | u510829608 | 1502427720 | Python | Python3 | py | Runtime Error | 0 | 0 | 854 | import sys
NIL = -1
def set_depth(v, depth):
tree[v][2] = depth
if child in tree[v][1]:
set_depth(child, depth + 1)
N = int(input())
tree = [[NIL, [], 0, 'leaf'] for _ in range(N)]
for _ in range(N):
line = [int(i) for i in sys.stdin.readline().split()]
t_id = line[0]
children = line[2:]
tree[t_id][1] = children
if children:
tree[t_id][3] = 'internal node'
for child in children:
tree[child][0] = t_id
for t_id in range(N):
if tree[t_id][0] == NIL:
tree[t_id][3] = 'root'
r = t_id
break
set_depth(r, 0)
for t_id in range(N):
parent = tree[t_id][0]
depth = tree[t_id][2]
children = tree[t_id][1]
type_t = tree[t_id][3]
print('node {}: parent = {}, depth = {}, {}, {}'.format(t_id, parent, depth, type_t, children)) | Traceback (most recent call last):
File "/tmp/tmpymsomg5r/tmpl0ym965g.py", line 11, in <module>
N = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s594490118 | p02279 | u519227872 | 1502546830 | Python | Python3 | py | Runtime Error | 0 | 0 | 877 | n = int(input())
tree = {}
for i in range(n):
row = [int(k) for k in input().split()]
id = row[0]
ch = row[2:]
val = tree.get(id, [])
val += ch
tree[id] = val
def printInfo(id):
parent = -1
depth = 0
for p in tree.keys():
if id in tree[p]:
parent = p
break
while True:
for p in tree.keys():
if id in tree[p]:
depth += 1
id = p
break
if p == tree.keys()[-1]:
break
for p in tree.keys():
if id in tree[p]:
depth += 1
if parent == -1:
type = 'root'
elif tree[id] != []:
type = 'internal node'
else:
type = "leaf"
print('node %d: parent = %d, depth = %d, %s, %s' %(id,parent,depth,type,str(tree[id])))
print(tree)
for p in tree.keys():
printInfo(p) | Traceback (most recent call last):
File "/tmp/tmpfsk25ms9/tmp7cw4pe_4.py", line 1, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s076579817 | p02279 | u519227872 | 1502548428 | Python | Python3 | py | Runtime Error | 0 | 0 | 866 | n = int(input())
tree = {}
for i in range(n):
row = [int(k) for k in input().split()]
id = row[0]
ch = row[2:]
val = tree.get(id, [])
val += ch
tree[id] = val
def printInfo(id):
parent = -1
depth = 0
for p in tree.keys():
if id in tree[p]:
parent = p
break
while True:
for p in tree.keys():
if id in tree[p]:
depth += 1
id = p
break
if p == tree.keys()[-1]:
break
for p in tree.keys():
if id in tree[p]:
depth += 1
if parent == -1:
type = 'root'
elif tree[id] != []:
type = 'internal node'
else:
type = "leaf"
print('node %d: parent = %d, depth = %d, %s, %s' %(id,parent,depth,type,str(tree[id])))
for p in tree.keys():
printInfo(p) | Traceback (most recent call last):
File "/tmp/tmplt_d0026/tmpuej7ql4b.py", line 1, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s705696186 | p02279 | u519227872 | 1502566042 | Python | Python3 | py | Runtime Error | 0 | 0 | 1036 | n = int(input())
class Node(object):
def __init__(self, p, l, r):
self.p = p
self.l = l
self.r = r
nodes = [Node(None, None, None) for i in range(n)]
for i in range(n):
id, left, right = map(int, input().split())
nodes[id].l = left
nodes[id].r = right
if left != -1:
nodes[left].p = i
if right != -1:
nodes[right].p = i
def pre(node):
print(node, end=" ")
if nodes[node].l != -1:
pre(nodes[node].l)
if nodes[node].r != -1:
pre(nodes[node].r)
def ino(node):
if nodes[node].l != -1:
ino(nodes[node].l)
print(node, end=" ")
if nodes[node].r != -1:
ino(nodes[node].r)
def post(node):
if nodes[node].l != -1:
post(nodes[node].l)
if nodes[node].r != -1:
post(nodes[node].r)
print(node, end=" ")
for ind, n in enumerate(nodes):
if n.p == None:
root = ind
break
print('Preorder')
pre(root)
print('\n')
print('InOrder')
ino(root)
print('\n')
print('Postorder')
post(root) | Traceback (most recent call last):
File "/tmp/tmp7cr5x5wt/tmpis8n4rt0.py", line 1, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s855761963 | p02279 | u855199458 | 1502682358 | Python | Python3 | py | Runtime Error | 20 | 7764 | 1131 | # -*- coding: utf-8 -*-
N = int(input())
tree = {n:{"parent":-1, "left": -1, "right": -1} for n in range(N)} # left-child right-sibing
parent = set([n for n in range(N)])
for n in range(N):
inp = [int(n) for n in input().split()]
tree[inp[0]]["children"] = inp[2:]
tree[inp[0]]["left"] = inp[2] if len(inp) > 2 else -1
for ip in inp[2:]:
tree[ip]["parent"] = inp[0]
for i, ip in enumerate(inp[2:-1]):
tree[inp[2+i]]["right"] = inp[3+i]
parent = parent.difference(inp[2:])
def set_depth(n, depth):
tree[n]["depth"] = depth
if tree[n]["left"] != -1:
set_depth(tree[n]["left"], depth+1)
if tree[n]["right"] != -1:
set_depth(tree[n]["right"], depth)
set_depth(parent.pop(), 0)
for n in range(N):
if tree[n]["parent"] == -1:
tree[n]["type"] = "root"
elif tree[n]["children"] != []:
tree[n]["type"] = "internal node"
else:
tree[n]["type"] = "leaf"
print("node {}: parent = {}, depth = {}, {}, {}"\
.format(n, tree[n]["parent"], tree[n]["depth"], tree[n]["type"], tree[n]["children"])) | Traceback (most recent call last):
File "/tmp/tmpynzgk_gk/tmp7rvgd_y6.py", line 2, in <module>
N = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s487278562 | p02279 | u855199458 | 1502682541 | Python | Python3 | py | Runtime Error | 820 | 102288 | 1174 | # -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(114514)
N = int(input())
tree = {n:{"parent":-1, "left": -1, "right": -1} for n in range(N)} # left-child right-sibing
parent = set([n for n in range(N)])
for n in range(N):
inp = [int(n) for n in input().split()]
tree[inp[0]]["children"] = inp[2:]
tree[inp[0]]["left"] = inp[2] if len(inp) > 2 else -1
for ip in inp[2:]:
tree[ip]["parent"] = inp[0]
for i, ip in enumerate(inp[2:-1]):
tree[inp[2+i]]["right"] = inp[3+i]
parent = parent.difference(inp[2:])
def set_depth(n, depth):
tree[n]["depth"] = depth
if tree[n]["left"] != -1:
set_depth(tree[n]["left"], depth+1)
if tree[n]["right"] != -1:
set_depth(tree[n]["right"], depth)
set_depth(parent.pop(), 0)
for n in range(N):
if tree[n]["parent"] == -1:
tree[n]["type"] = "root"
elif tree[n]["children"] != []:
tree[n]["type"] = "internal node"
else:
tree[n]["type"] = "leaf"
print("node {}: parent = {}, depth = {}, {}, {}"\
.format(n, tree[n]["parent"], tree[n]["depth"], tree[n]["type"], tree[n]["children"])) | Traceback (most recent call last):
File "/tmp/tmpswxmpwt2/tmpx3nhwgyu.py", line 6, in <module>
N = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s916523003 | p02279 | u918276501 | 1508083526 | Python | Python3 | py | Runtime Error | 0 | 0 | 584 | def set_pdt(i, p, d):
u = tree[i]
u[0], u[1] = p, d
for c in u[3]:
set_pdt(c, i, d+1)
else:
if u[0] == -1:
u[2] = "root"
elif u[3]:
u[2] = "internal node"
else:
u[2] = "leaf"
n = int(input())
# p d t ch
tree = [[-1,0,0,0] for _ in range(n)]
root = set(range(n))
for _ in range(n):
i, k, *c = map(int, input().split())
tree[i][3] = c
root -= set(c)
set_pdt(*root, -1, 0)
for i in range(n):
u = tree[i]
print("node {}: parent = {}, depth = {}, {}, {}".format(i, *u)) | Traceback (most recent call last):
File "/tmp/tmph6tgv443/tmp818_du1s.py", line 14, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s763350241 | p02279 | u918276501 | 1508083659 | Python | Python3 | py | Runtime Error | 0 | 0 | 586 | def set_pdt(i, p, d):
u = tree[i]
u[0], u[1] = p, d
for c in u[3]:
set_pdt(c, i, d+1)
else:
if u[0] == -1:
u[2] = "root"
elif u[3]:
u[2] = "internal node"
else:
u[2] = "leaf"
n = int(input())
# p d t ch
tree = [[-1,0,0,0] for _ in range(n)]
root = set(range(n))
for _ in range(n):
i, k, *c = map(int, input().split())
tree[i][3] = c
root -= set(c)
set_pdt(root[0], -1, 0)
for i in range(n):
u = tree[i]
print("node {}: parent = {}, depth = {}, {}, {}".format(i, *u)) | Traceback (most recent call last):
File "/tmp/tmprbo2mcbh/tmpex0wxtx2.py", line 14, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s802325114 | p02279 | u662418022 | 1517121524 | Python | Python3 | py | Runtime Error | 30 | 5620 | 1461 | # -*- coding: utf-8 -*-
class Node():
def __init__(self, ID, parent=-1, left=-1, right=-1, depth=0, children=[]):
self.id = ID
self.parent = parent
self.left = left
self.right = right
self.depth = depth
self.children = children
if __name__ == '__main__':
n = int(input())
tree = [Node(i) for i in range(n)]
for _ in range(n):
line = [int(l) for l in input().split(" ")]
ID = line[0]
d = line[1]
if d == 0:
continue
else:
children = line[2:]
tree[ID].children = children
tree[ID].left = children[0]
for j in range(0, d):
tree[children[j]].parent = ID
if j < d - 1:
tree[children[j]].right = children[j + 1]
def setDepth(u, p):
tree[u].depth = p
if tree[u].right != -1:
setDepth(tree[u].right, p)
if tree[u].left != -1:
setDepth(tree[u].left, p + 1)
for node in tree:
if node.parent == -1:
root = node.id
setDepth(root, 0)
for node in tree:
if node.parent == -1:
n_type = "root"
elif node.left != -1:
n_type = "internal node"
else:
n_type = "leaf"
print("node {}: parent = {}, depth = {}, {}, {}".format(
node.id, node.parent, node.depth, n_type, node.children))
| Traceback (most recent call last):
File "/tmp/tmp755q3c30/tmp7l7jxqdh.py", line 16, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s386181066 | p02279 | u662418022 | 1517121615 | Python | Python3 | py | Runtime Error | 20 | 5620 | 1461 | # -*- coding: utf-8 -*-
class Node():
def __init__(self, ID, parent=-1, left=-1, right=-1, depth=0, children=[]):
self.id = ID
self.parent = parent
self.left = left
self.right = right
self.depth = depth
self.children = children
if __name__ == '__main__':
n = int(input())
tree = [Node(i) for i in range(n)]
for _ in range(n):
line = [int(l) for l in input().split(" ")]
ID = line[0]
d = line[1]
if d == 0:
continue
else:
children = line[2:]
tree[ID].children = children
tree[ID].left = children[0]
for j in range(0, d):
tree[children[j]].parent = ID
if j < d - 1:
tree[children[j]].right = children[j + 1]
def setDepth(u, p):
tree[u].depth = p
if tree[u].right != -1:
setDepth(tree[u].right, p)
if tree[u].left != -1:
setDepth(tree[u].left, p + 1)
for node in tree:
if node.parent == -1:
root = node.id
setDepth(root, 0)
for node in tree:
if node.parent == -1:
n_type = "root"
elif node.left != -1:
n_type = "internal node"
else:
n_type = "leaf"
print("node {}: parent = {}, depth = {}, {}, {}".format(
node.id, node.parent, node.depth, n_type, node.children))
| Traceback (most recent call last):
File "/tmp/tmped6znof3/tmpslbgonsm.py", line 16, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s981545829 | p02279 | u662418022 | 1517121928 | Python | Python3 | py | Runtime Error | 20 | 5620 | 1461 | # -*- coding: utf-8 -*-
class Node():
def __init__(self, ID, parent=-1, left=-1, right=-1, depth=0, children=[]):
self.id = ID
self.parent = parent
self.left = left
self.right = right
self.depth = depth
self.children = children
if __name__ == '__main__':
n = int(input())
tree = [Node(i) for i in range(n)]
for _ in range(n):
line = [int(l) for l in input().split(" ")]
ID = line[0]
d = line[1]
if d == 0:
continue
else:
children = line[2:]
tree[ID].children = children
tree[ID].left = children[0]
for j in range(0, d):
tree[children[j]].parent = ID
if j < d - 1:
tree[children[j]].right = children[j + 1]
def setDepth(u, p):
tree[u].depth = p
if tree[u].right != -1:
setDepth(tree[u].right, p)
if tree[u].left != -1:
setDepth(tree[u].left, p + 1)
for node in tree:
if node.parent == -1:
root = node.id
setDepth(root, 0)
for node in tree:
if node.parent == -1:
n_type = "root"
elif node.left != -1:
n_type = "internal node"
else:
n_type = "leaf"
print("node {}: parent = {}, depth = {}, {}, {}".format(
node.id, node.parent, node.depth, n_type, node.children))
| Traceback (most recent call last):
File "/tmp/tmpwthffyl1/tmp37w4ptfm.py", line 16, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s912170547 | p02279 | u996463517 | 1518360735 | Python | Python3 | py | Runtime Error | 20 | 5616 | 1059 | class tree:
def __init__(self,name,parent,left,right):
self.name = name
self.parent = parent
self.left = left
self.right = right
trees = {}
trees['0'] = tree('0','-1','-1','-1')
n = int(input())
for i in range(n):
s = [i for i in input().split()]
p = s[0]
k = s[1]
s_t = ['-1']+s[2:]+['-1']
for j in range(1,int(k)+1):
trees[s_t[j]] = tree(s_t[j],p,s_t[j-1],s_t[j+1])
def getDepth(u):
d = 0
while trees[u].parent != '-1':
u = trees[u].parent
d += 1
return d
def getChildren(u):
c = []
for i in trees.values():
if i.parent == u:
c.append(i.name)
return c
for i in range(n):
par = trees[str(i)].parent
dep = getDepth(str(i))
r = trees[str(i)].right
l = trees[str(i)].left
ch = sorted([int(i) for i in getChildren(str(i))])
if par == '-1':sit = 'root'
elif l == '-1':sit = 'internal node'
else:sit = 'leaf'
print('node {}: parent = {}, depth = {}, {}, {}'.format(i,par,dep,sit,ch))
| Traceback (most recent call last):
File "/tmp/tmpziw0m2xl/tmp6j4u7ged.py", line 10, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s166192334 | p02279 | u996463517 | 1518361118 | Python | Python3 | py | Runtime Error | 20 | 5612 | 889 | class tree:
def __init__(self,name,parent):
self.name = name
self.parent = parent
trees = {}
trees['0'] = tree('0','-1')
n = int(input())
for i in range(n):
s = [i for i in input().split()]
p = s[0]
s_t = s[2:]
for j in range(len(s_t)):
trees[s_t[j]] = tree(s_t[j],p)
def getDepth(u):
d = 0
while trees[u].parent != '-1':
u = trees[u].parent
d += 1
return d
def getChildren(u):
c = []
for i in trees.values():
if i.parent == u:
c.append(i.name)
return c
for i in range(n):
par = trees[str(i)].parent
dep = getDepth(str(i))
ch = sorted([int(i) for i in getChildren(str(i))])
if par == '-1':sit = 'root'
elif len(ch) != 0:sit = 'internal node'
else:sit = 'leaf'
print('node {}: parent = {}, depth = {}, {}, {}'.format(i,par,dep,sit,ch))
| Traceback (most recent call last):
File "/tmp/tmpjc96471d/tmpfhfwejto.py", line 9, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s351286234 | p02279 | u150984829 | 1519044624 | Python | Python3 | py | Runtime Error | 0 | 0 | 321 | t={}
p={}
for _ in[0]*int(input()):
e=input().split()
t[e[0]]=e[2:]
for i in e[2:]:p[i]=e[0]
r=(set(t)-set(p)).pop()
p[r]='-1'
d={r:0}
for
for i in t:
f='root,'if'-1'==p[i]else'internal node,'if t[i]else'leaf,'
d='0'
print('node',i+':','parent =',p[i]+',','depth =',d+',',f,list(map(int,t[i])))
| File "/tmp/tmpqn9am601/tmpldjxkioj.py", line 10
for
^
SyntaxError: invalid syntax
| |
s226845553 | p02279 | u150984829 | 1519046580 | Python | Python3 | py | Runtime Error | 0 | 0 | 363 | def q(a,h):
d[a]=str(h)
for b in t[a]:q(b,h+1)
t,p,d={},{},{}
for _ in[0]*int(input()):
e=input().split()
t[e[0]]=e[2:]
for i in e[2:]:p[i]=e[0]
r=(set(t)-set(p)).pop()
p[r]='-1'
q(r,0)
for i in sorted(map(int,t)):
i=str(i);print(f'node {i}: parent = {p[i]}, depth = {d[i]}, {'root'if'-1'==p[i]else'internal node'if t[i]else'leaf'}, {list(map(int,t[i]))}')
| File "/tmp/tmpvsjex3lb/tmps62ayzvt.py", line 13
i=str(i);print(f'node {i}: parent = {p[i]}, depth = {d[i]}, {'root'if'-1'==p[i]else'internal node'if t[i]else'leaf'}, {list(map(int,t[i]))}')
^^^^
SyntaxError: f-string: expecting '}'
| |
s603846184 | p02279 | u843404779 | 1525272917 | Python | Python3 | py | Runtime Error | 20 | 5612 | 1466 | def rec_function(i, p):
node_info[i]['depth'] = p
if node_info[i]['parent'] == -1:
node_info[i]['type'] = 'root'
elif node_info[i]['left'] == None:
node_info[i]['type'] = 'leaf'
else:
node_info[i]['type'] = 'internal node'
if node_info[i]['right'] != None:
rec_function(node_info[i]['right'], p)
if node_info[i]['left'] != None:
rec_function(node_info[i]['left'], p + 1)
n = int(input())
node_info = [ {
'parent': None,
'right': None,
'left': None,
'depth': None,
'type': None,
'children': [],
} for _ in range(n) ]
for _ in range(n):
input_info = list(map(int, input().split()))
node = input_info[0]
children = input_info[2:]
node_info[node]['children'] = children
if len(children) > 0:
node_info[node]['left'] = children[0]
for i in range(len(children)):
node_info[children[i]]['parent'] = node
if i < len(children) - 1:
node_info[children[i]]['right'] = children[i+1]
root_node = 0
for i in range(len(node_info)):
if node_info[i]['parent'] == None:
node_info[i]['parent'] = -1
root_node = i
break
rec_function(root_node, 0)
for i in range(len(node_info)):
print("node {0}: parent = {1}, depth = {2}, {3}, {4}".format(
i,
node_info[i]['parent'],
node_info[i]['depth'],
node_info[i]['type'],
node_info[i]['children']
))
| Traceback (most recent call last):
File "/tmp/tmpw1ycul_6/tmp_klesdb7.py", line 17, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s277061852 | p02279 | u843404779 | 1525272944 | Python | Python3 | py | Runtime Error | 20 | 5612 | 1466 | def rec_function(i, p):
node_info[i]['depth'] = p
if node_info[i]['parent'] == -1:
node_info[i]['type'] = 'root'
elif node_info[i]['left'] == None:
node_info[i]['type'] = 'leaf'
else:
node_info[i]['type'] = 'internal node'
if node_info[i]['right'] != None:
rec_function(node_info[i]['right'], p)
if node_info[i]['left'] != None:
rec_function(node_info[i]['left'], p + 1)
n = int(input())
node_info = [ {
'parent': None,
'right': None,
'left': None,
'depth': None,
'type': None,
'children': [],
} for _ in range(n) ]
for _ in range(n):
input_info = list(map(int, input().split()))
node = input_info[0]
children = input_info[2:]
node_info[node]['children'] = children
if len(children) > 0:
node_info[node]['left'] = children[0]
for i in range(len(children)):
node_info[children[i]]['parent'] = node
if i < len(children) - 1:
node_info[children[i]]['right'] = children[i+1]
root_node = 0
for i in range(len(node_info)):
if node_info[i]['parent'] == None:
node_info[i]['parent'] = -1
root_node = i
break
rec_function(root_node, 0)
for i in range(len(node_info)):
print("node {0}: parent = {1}, depth = {2}, {3}, {4}".format(
i,
node_info[i]['parent'],
node_info[i]['depth'],
node_info[i]['type'],
node_info[i]['children']
))
| Traceback (most recent call last):
File "/tmp/tmprq6073zc/tmp4wje1wpo.py", line 17, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s926495814 | p02279 | u843404779 | 1525273034 | Python | Python3 | py | Runtime Error | 20 | 5616 | 1466 | def rec_function(i, p):
node_info[i]['depth'] = p
if node_info[i]['parent'] == -1:
node_info[i]['type'] = 'root'
elif node_info[i]['left'] == None:
node_info[i]['type'] = 'leaf'
else:
node_info[i]['type'] = 'internal node'
if node_info[i]['right'] != None:
rec_function(node_info[i]['right'], p)
if node_info[i]['left'] != None:
rec_function(node_info[i]['left'], p + 1)
n = int(input())
node_info = [ {
'parent': None,
'right': None,
'left': None,
'depth': None,
'type': None,
'children': [],
} for _ in range(n) ]
for _ in range(n):
input_info = list(map(int, input().split()))
node = input_info[0]
children = input_info[2:]
node_info[node]['children'] = children
if len(children) > 0:
node_info[node]['left'] = children[0]
for i in range(len(children)):
node_info[children[i]]['parent'] = node
if i < len(children) - 1:
node_info[children[i]]['right'] = children[i+1]
root_node = 0
for i in range(len(node_info)):
if node_info[i]['parent'] == None:
node_info[i]['parent'] = -1
root_node = i
break
rec_function(root_node, 0)
for i in range(len(node_info)):
print("node {0}: parent = {1}, depth = {2}, {3}, {4}".format(
i,
node_info[i]['parent'],
node_info[i]['depth'],
node_info[i]['type'],
node_info[i]['children']
))
| Traceback (most recent call last):
File "/tmp/tmpl1aryi6z/tmplj5n6r3y.py", line 17, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s414402941 | p02279 | u843404779 | 1525273054 | Python | Python3 | py | Runtime Error | 30 | 5616 | 1466 | def rec_function(i, p):
node_info[i]['depth'] = p
if node_info[i]['parent'] == -1:
node_info[i]['type'] = 'root'
elif node_info[i]['left'] == None:
node_info[i]['type'] = 'leaf'
else:
node_info[i]['type'] = 'internal node'
if node_info[i]['right'] != None:
rec_function(node_info[i]['right'], p)
if node_info[i]['left'] != None:
rec_function(node_info[i]['left'], p + 1)
n = int(input())
node_info = [ {
'parent': None,
'right': None,
'left': None,
'depth': None,
'type': None,
'children': [],
} for _ in range(n) ]
for _ in range(n):
input_info = list(map(int, input().split()))
node = input_info[0]
children = input_info[2:]
node_info[node]['children'] = children
if len(children) > 0:
node_info[node]['left'] = children[0]
for i in range(len(children)):
node_info[children[i]]['parent'] = node
if i < len(children) - 1:
node_info[children[i]]['right'] = children[i+1]
root_node = 0
for i in range(len(node_info)):
if node_info[i]['parent'] == None:
node_info[i]['parent'] = -1
root_node = i
break
rec_function(root_node, 0)
for i in range(len(node_info)):
print("node {0}: parent = {1}, depth = {2}, {3}, {4}".format(
i,
node_info[i]['parent'],
node_info[i]['depth'],
node_info[i]['type'],
node_info[i]['children']
))
| Traceback (most recent call last):
File "/tmp/tmpl9fg27dd/tmpg350z1er.py", line 17, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s076171997 | p02279 | u843404779 | 1525273084 | Python | Python3 | py | Runtime Error | 20 | 5616 | 1466 | def rec_function(i, p):
node_info[i]['depth'] = p
if node_info[i]['parent'] == -1:
node_info[i]['type'] = 'root'
elif node_info[i]['left'] == None:
node_info[i]['type'] = 'leaf'
else:
node_info[i]['type'] = 'internal node'
if node_info[i]['right'] != None:
rec_function(node_info[i]['right'], p)
if node_info[i]['left'] != None:
rec_function(node_info[i]['left'], p + 1)
n = int(input())
node_info = [ {
'parent': None,
'right': None,
'left': None,
'depth': None,
'type': None,
'children': [],
} for _ in range(n) ]
for _ in range(n):
input_info = list(map(int, input().split()))
node = input_info[0]
children = input_info[2:]
node_info[node]['children'] = children
if len(children) > 0:
node_info[node]['left'] = children[0]
for i in range(len(children)):
node_info[children[i]]['parent'] = node
if i < len(children) - 1:
node_info[children[i]]['right'] = children[i+1]
root_node = 0
for i in range(len(node_info)):
if node_info[i]['parent'] == None:
node_info[i]['parent'] = -1
root_node = i
break
rec_function(root_node, 0)
for i in range(len(node_info)):
print("node {0}: parent = {1}, depth = {2}, {3}, {4}".format(
i,
node_info[i]['parent'],
node_info[i]['depth'],
node_info[i]['type'],
node_info[i]['children']
))
| Traceback (most recent call last):
File "/tmp/tmpd4qn2k7q/tmpqr49evef.py", line 17, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s890669953 | p02279 | u724548524 | 1525337148 | Python | Python3 | py | Runtime Error | 20 | 5628 | 1415 | class Node:
def __init__(self, parent = -1, left = -1, right = -1, depth = 0):
self.parent = parent
self.left = left
self.right = right
self.depth = depth
def print_node(i):
global node
print("node " + str(i) + ": ", end = "")
print("parent = " + str(node[i].parent) +", ", end = "")
print("depth = " + str(node[i].depth) + ", ", end = "")
if node[i].parent == -1:
print("root, [", end = "")
elif node[i].left != -1:
print("internal node, [", end = "")
else:
print("leaf, [", end = "")
if node[i].left != -1:
print(str(node[i].left), end = "")
j = node[i].left
while node[j].right != -1:
j = node[j].right
print(", " + str(j), end = "")
print("]")
n = int(input())
node = [Node() for i in range(n)]
def input_depth(i, n):
global node
node[i].depth = n
if node[i].right != -1:
input_depth(node[i].right, n)
if node[i].left != -1:
input_depth(node[i].left, n + 1)
for i in range(n):
p, k, *c = list(map(int, input().split()))
if k > 0:
node[p].left = c[0]
for j in range(k):
node[c[j]].parent = p
if j != k - 1:
node[c[j]].right = c[j + 1]
for i in range(n):
if node[i].parent == -1:
input_depth(i, 0)
break
for i in range(n):
print_node(i)
| Traceback (most recent call last):
File "/tmp/tmplwtk_dso/tmpezvaa_bc.py", line 28, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s404197502 | p02279 | u126478680 | 1525580764 | Python | Python3 | py | Runtime Error | 30 | 6348 | 1687 | from enum import Enum, auto
class NodeType(Enum):
LEAF = auto()
INTERNAL_NODE = auto()
ROOT = auto()
class Node():
def __init__(self, parent=None, left=None, right=None, depth=None, nodetype=None):
self.parent = parent
self.left = left
self.right = right
self.depth = depth
self.nodetype = nodetype
n = int(input())
nodes = [Node() for i in range(n)]
for i in range(n):
arr = list(map(int, input().split(' ')))
if arr[1] == 0:
nodes[arr[0]].nodetype = NodeType.LEAF
continue
id, k, c = arr[0], arr[1], arr[2:]
nodes[id].nodetype = NodeType.INTERNAL_NODE
nodes[id].left = c[0]
for j in range(k-1):
nodes[c[j]].right = c[j+1]
nodes[c[j]].parent = id
nodes[c[k-1]].parent = id
root = 0
for i in range(n):
if nodes[i].parent == None:
root = i
nodes[i].nodetype = NodeType.ROOT
# 深さの探索
# h < 20 なので再帰的にたどっても良い
def rec(u, d):
nodes[u].depth = d
if nodes[u].right: rec(nodes[u].right, d)
if nodes[u].left: rec(nodes[u].left, d+1)
rec(root, 0)
for i in range(n):
if nodes[i].nodetype == NodeType.LEAF:
print('node %d: parent = %d, depth = %d, leaf, []'%(i, nodes[i].parent, nodes[i].depth))
c = nodes[i].left
child_list = []
while c:
child_list.append(c)
c = nodes[c].right
if nodes[i].nodetype == NodeType.ROOT:
print('node %d: parent = -1, depth = 0, root,'%(i), child_list)
elif nodes[i].nodetype == NodeType.INTERNAL_NODE:
print('node %d: parent = %d, depth = %d, internal node,'%(i, nodes[i].parent, nodes[i].depth), child_list)
| Traceback (most recent call last):
File "/tmp/tmp8ik_rzhz/tmp9zgrl2zo.py", line 16, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s547746570 | p02279 | u126478680 | 1525581219 | Python | Python3 | py | Runtime Error | 40 | 6352 | 1711 | from enum import Enum, auto
class NodeType(Enum):
LEAF = auto()
INTERNAL_NODE = auto()
ROOT = auto()
class Node():
def __init__(self, parent=None, left=None, right=None, depth=None, nodetype=None):
self.parent = parent
self.left = left
self.right = right
self.depth = depth
self.nodetype = nodetype
n = int(input())
nodes = [Node() for i in range(n)]
for i in range(n):
arr = list(map(int, input().split(' ')))
if arr[1] == 0:
nodes[arr[0]].nodetype = NodeType.LEAF
continue
id, k, c = arr[0], arr[1], arr[2:]
nodes[id].nodetype = NodeType.INTERNAL_NODE
nodes[id].left = c[0]
for j in range(k-1):
nodes[c[j]].right = c[j+1]
nodes[c[j]].parent = id
nodes[c[k-1]].parent = id
root = 0
for i in range(n):
if nodes[i].parent == None:
root = i
nodes[i].nodetype = NodeType.ROOT
# 深さの探索
# h < 20 なので再帰的にたどっても良い
def rec(u, d):
nodes[u].depth = d
if nodes[u].right != None: rec(nodes[u].right, d)
if nodes[u].left != None: rec(nodes[u].left, d+1)
rec(root, 0)
for i in range(n):
if nodes[i].nodetype == NodeType.LEAF:
print('node %d: parent = %d, depth = %d, leaf, []'%(i, nodes[i].parent, nodes[i].depth))
c = nodes[i].left
child_list = []
while c != None:
child_list.append(c)
c = nodes[c].right
if nodes[i].nodetype == NodeType.ROOT:
print('node %d: parent = -1, depth = 0, root,'%(i), child_list)
elif nodes[i].nodetype == NodeType.INTERNAL_NODE:
print('node %d: parent = %d, depth = %d, internal node,'%(i, nodes[i].parent, nodes[i].depth), child_list)
| Traceback (most recent call last):
File "/tmp/tmpszigh3kv/tmp3znjiri8.py", line 16, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s432968966 | p02279 | u126478680 | 1525581291 | Python | Python3 | py | Runtime Error | 30 | 6352 | 1711 | from enum import Enum, auto
class NodeType(Enum):
LEAF = auto()
INTERNAL_NODE = auto()
ROOT = auto()
class Node():
def __init__(self, parent=None, left=None, right=None, depth=None, nodetype=None):
self.parent = parent
self.left = left
self.right = right
self.depth = depth
self.nodetype = nodetype
n = int(input())
nodes = [Node() for i in range(n)]
for i in range(n):
arr = list(map(int, input().split(' ')))
if arr[1] == 0:
nodes[arr[0]].nodetype = NodeType.LEAF
continue
id, k, c = arr[0], arr[1], arr[2:]
nodes[id].nodetype = NodeType.INTERNAL_NODE
nodes[id].left = c[0]
for j in range(k-1):
nodes[c[j]].right = c[j+1]
nodes[c[j]].parent = id
nodes[c[k-1]].parent = id
root = 0
for i in range(n):
if nodes[i].parent == None:
root = i
nodes[i].nodetype = NodeType.ROOT
# 深さの探索
# h < 20 なので再帰的にたどっても良い
def rec(u, d):
nodes[u].depth = d
if nodes[u].right != None: rec(nodes[u].right, d)
if nodes[u].left != None: rec(nodes[u].left, d+1)
rec(root, 0)
for i in range(n):
if nodes[i].nodetype == NodeType.LEAF:
print('node %d: parent = %d, depth = %d, leaf, []'%(i, nodes[i].parent, nodes[i].depth))
c = nodes[i].left
child_list = []
while c != None:
child_list.append(c)
c = nodes[c].right
if nodes[i].nodetype == NodeType.ROOT:
print('node %d: parent = -1, depth = 0, root,'%(i), child_list)
elif nodes[i].nodetype == NodeType.INTERNAL_NODE:
print('node %d: parent = %d, depth = %d, internal node,'%(i, nodes[i].parent, nodes[i].depth), child_list)
| Traceback (most recent call last):
File "/tmp/tmpv95znj7k/tmph15hqdml.py", line 16, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s748575478 | p02279 | u126478680 | 1525584922 | Python | Python3 | py | Runtime Error | 0 | 0 | 1772 | from enum import Enum, auto
import sys
class NodeType(Enum):
LEAF = auto()
INTERNAL_NODE = auto()
ROOT = auto()
class Node():
def __init__(self, parent=None, left=None, right=None, depth=None, nodetype=None):
self.parent = parent
self.left = left
self.right = right
self.depth = depth
self.nodetype = nodetype
n = int(input())
nodes = [Node() for i in range(n)]
# 再帰上限の変更
sys.setrecursionlimit(n)
for i in range(n):
arr = list(map(int, input().split(' ')))
if arr[1] == 0:
nodes[arr[0]].nodetype = NodeType.LEAF
continue
id, k, c = arr[0], arr[1], arr[2:]
nodes[id].nodetype = NodeType.INTERNAL_NODE
nodes[id].left = c[0]
for j in range(k-1):
nodes[c[j]].right = c[j+1]
nodes[c[j]].parent = id
nodes[c[k-1]].parent = id
root = 0
for i in range(n):
if nodes[i].parent == None:
root = i
nodes[i].nodetype = NodeType.ROOT
# 深さの探索
# h < 20 なので再帰的にたどっても良い
def rec(u, d):
nodes[u].depth = d
if nodes[u].right != None: rec(nodes[u].right, d)
if nodes[u].left != None: rec(nodes[u].left, d+1)
rec(root, 0)
for i in range(n):
if nodes[i].nodetype == NodeType.LEAF:
print('node %d: parent = %d, depth = %d, leaf, []'%(i, nodes[i].parent, nodes[i].depth))
c = nodes[i].left
child_list = []
while c != None:
child_list.append(c)
c = nodes[c].right
if nodes[i].nodetype == NodeType.ROOT:
print('node %d: parent = -1, depth = 0, root,'%(i), child_list)
elif nodes[i].nodetype == NodeType.INTERNAL_NODE:
print('node %d: parent = %d, depth = %d, internal node,'%(i, nodes[i].parent, nodes[i].depth), child_list)
| Traceback (most recent call last):
File "/tmp/tmprh7p93hd/tmpz9ho1v90.py", line 17, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s141030735 | p02279 | u126478680 | 1525584987 | Python | Python3 | py | Runtime Error | 30 | 6352 | 1777 | from enum import Enum, auto
import sys
class NodeType(Enum):
LEAF = auto()
INTERNAL_NODE = auto()
ROOT = auto()
class Node():
def __init__(self, parent=None, left=None, right=None, depth=None, nodetype=None):
self.parent = parent
self.left = left
self.right = right
self.depth = depth
self.nodetype = nodetype
n = int(input())
nodes = [Node() for i in range(n)]
# 再帰上限の変更
sys.setrecursionlimit(100000)
for i in range(n):
arr = list(map(int, input().split(' ')))
if arr[1] == 0:
nodes[arr[0]].nodetype = NodeType.LEAF
continue
id, k, c = arr[0], arr[1], arr[2:]
nodes[id].nodetype = NodeType.INTERNAL_NODE
nodes[id].left = c[0]
for j in range(k-1):
nodes[c[j]].right = c[j+1]
nodes[c[j]].parent = id
nodes[c[k-1]].parent = id
root = 0
for i in range(n):
if nodes[i].parent == None:
root = i
nodes[i].nodetype = NodeType.ROOT
# 深さの探索
# h < 20 なので再帰的にたどっても良い
def rec(u, d):
nodes[u].depth = d
if nodes[u].right != None: rec(nodes[u].right, d)
if nodes[u].left != None: rec(nodes[u].left, d+1)
rec(root, 0)
for i in range(n):
if nodes[i].nodetype == NodeType.LEAF:
print('node %d: parent = %d, depth = %d, leaf, []'%(i, nodes[i].parent, nodes[i].depth))
c = nodes[i].left
child_list = []
while c != None:
child_list.append(c)
c = nodes[c].right
if nodes[i].nodetype == NodeType.ROOT:
print('node %d: parent = -1, depth = 0, root,'%(i), child_list)
elif nodes[i].nodetype == NodeType.INTERNAL_NODE:
print('node %d: parent = %d, depth = %d, internal node,'%(i, nodes[i].parent, nodes[i].depth), child_list)
| Traceback (most recent call last):
File "/tmp/tmp9rwe2zi0/tmpbmq6giki.py", line 17, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s159046336 | p02279 | u126478680 | 1526297528 | Python | Python3 | py | Runtime Error | 30 | 6352 | 1683 | from enum import Enum, auto
import sys
class NodeType(Enum):
LEAF = auto()
INTERNAL_NODE = auto()
ROOT = auto()
class Node():
def __init__(self, parent=None, left=None, right=None, depth=None, nodetype=None):
self.parent = parent
self.left = left
self.right = right
self.depth = depth
self.nodetype = nodetype
n = int(input())
nodes = [Node() for i in range(n)]
sys.setrecursionlimit(100000)
for i in range(n):
arr = list(map(int, input().split(' ')))
if arr[1] == 0:
nodes[arr[0]].nodetype = NodeType.LEAF
continue
id, k, c = arr[0], arr[1], arr[2:]
nodes[id].nodetype = NodeType.INTERNAL_NODE
nodes[id].left = c[0]
for j in range(k-1):
nodes[c[j]].right = c[j+1]
nodes[c[j]].parent = id
nodes[c[k-1]].parent = id
root = 0
for i in range(n):
if nodes[i].parent == None:
root = i
nodes[i].nodetype = NodeType.ROOT
def rec(u, d):
nodes[u].depth = d
if nodes[u].right != None: rec(nodes[u].right, d)
if nodes[u].left != None: rec(nodes[u].left, d+1)
rec(root, 0)
for i in range(n):
if nodes[i].nodetype == NodeType.LEAF:
print('node %d: parent = %d, depth = %d, leaf, []'%(i, nodes[i].parent, nodes[i].depth))
c = nodes[i].left
child_list = []
while c != None:
child_list.append(c)
c = nodes[c].right
if nodes[i].nodetype == NodeType.ROOT:
print('node %d: parent = -1, depth = 0, root,'%(i), child_list)
elif nodes[i].nodetype == NodeType.INTERNAL_NODE:
print('node %d: parent = %d, depth = %d, internal node,'%(i, nodes[i].parent, nodes[i].depth), child_list)
| Traceback (most recent call last):
File "/tmp/tmp2m_pl24v/tmp30k1zuj5.py", line 17, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s147821497 | p02279 | u126478680 | 1526299886 | Python | Python3 | py | Runtime Error | 20 | 5620 | 1353 | import sys
sys.setrecursionlimit(100000)
class Node():
def __init__(self, p=None, l=None, r=None):
self.parent = p
self.left = l
self.right = r
self.depth = None
n = int(input())
nodes = [Node() for i in range(n)]
for i in range(n):
id, k, *c = list(map(int, input().split(' ')))
if k == 0: continue
nodes[id].left = c[0]
nodes[c[0]].parent = id
for j in range(1, k):
nodes[c[j-1]].right = c[j]
nodes[c[j]].parent = id
root = None
for i in range(n):
if nodes[i].parent == None:
root = i
def get_childs(i):
if nodes[i].left == None: return []
j = 0
childs = [nodes[i].left]
while nodes[childs[j]].right != None:
childs.append(nodes[childs[j]].right)
j += 1
return childs
def set_depth(s, d):
nodes[s].depth = d
if nodes[s].right != None:
set_depth(nodes[s].right, d)
if nodes[s].left != None:
set_depth(nodes[s].left, d+1)
set_depth(root, 0)
for i in range(n):
nodetype = 'internal node'
childs = get_childs(i)
if childs == []:
nodetype = 'leaf'
parent = nodes[i].parent
if parent == None:
parent = -1
nodetype = 'root'
print('node ' + str(i) + ': parent = ' + str(parent) + ', depth = ' + str(nodes[i].depth) + ', ' + nodetype + ', ' + str(childs))
| Traceback (most recent call last):
File "/tmp/tmpi3p_hmw0/tmpf0tojg00.py", line 11, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s895233144 | p02279 | u255317651 | 1526775750 | Python | Python3 | py | Runtime Error | 20 | 5632 | 1856 | # -*- coding: utf-8 -*-
"""
Created on Sat May 19 19:34:44 2018
ALDS1_7_A
@author: maezawa
"""
n = int(input())
parent =[]
left = []
right = [None for _ in range(n)]
child = [[] for _ in range(n)]
depth = [None for _ in range(n)]
def get_parent(i):
for j in range(n):
if i in child[j]:
return j
return -1
def get_left(i):
if len(child[i]) == 0:
return None
return child[i][0]
def set_right():
global right
for j in range(n):
for i, k in enumerate(child[j]):
#if len(child[j]) > i+1:
try:
right[k] = child[j][i+1]
except:
continue
#def get_right(i):
# j = parent[i]
# try:
# ind = child[j].index(i)
# except:
# return None
# if len(child[j]) > ind+1:
# return child[j][ind+1]
# else:
# return None
def set_depth(i, p):
global depth
if depth[i] != None:
return
depth[i] = p
if right[i] != None:
set_depth(right[i], p)
if left[i] != None:
set_depth(left[i], p+1)
def get_depth(i):
global depth
if depth[i] != None:
return depth[i]
d = 0
u = i
while parent[u] != -1:
u = parent[u]
d += 1
depth[i] = d
return d
for i in range(n):
line = list(map(int, input().split()))
child[line[0]] = [line[i+2] for i in range(line[1])]
for i in range(n):
parent.append(get_parent(i))
left.append(get_left(i))
get_depth(i)
set_right()
for i in range(n):
out_str = 'node {}: parent = {}, depth = {}, '.format(i,parent[i],depth[i])
if parent[i] == -1:
out_str += 'root, '
elif len(child[i]) == 0:
out_str += 'leaf, '
else:
out_str += 'internal node, '
out_str += str(child[i])
print(out_str)
| Traceback (most recent call last):
File "/tmp/tmpp3mbc1v8/tmpjp0xfzu8.py", line 8, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s025479206 | p02279 | u255317651 | 1526776193 | Python | Python3 | py | Runtime Error | 20 | 5628 | 1860 | # -*- coding: utf-8 -*-
"""
Created on Sat May 19 19:34:44 2018
ALDS1_7_A
@author: maezawa
"""
n = int(input())
parent =[]
left = []
right = [None for _ in range(n)]
child = [[] for _ in range(n)]
depth = [None for _ in range(n)]
def get_parent(i):
for j in range(n):
if i in child[j]:
return j
return -1
def get_left(i):
if len(child[i]) == 0:
return None
return child[i][0]
def set_right():
global right
for j in range(n):
for i, k in enumerate(child[j]):
#if len(child[j]) > i+1:
try:
right[k] = child[j][i+1]
except:
continue
#def get_right(i):
# j = parent[i]
# try:
# ind = child[j].index(i)
# except:
# return None
# if len(child[j]) > ind+1:
# return child[j][ind+1]
# else:
# return None
def set_depth(i, p):
global depth
if depth[i] != None:
return
depth[i] = p
if right[i] != None:
set_depth(right[i], p)
if left[i] != None:
set_depth(left[i], p+1)
def get_depth(i):
global depth
if depth[i] != None:
return depth[i]
d = 0
u = i
while parent[u] != -1:
u = parent[u]
d += 1
depth[i] = d
return d
for i in range(n):
line = list(map(int, input().split()))
child[line[0]] = [line[i+2] for i in range(line[1])]
for i in range(n):
parent.append(get_parent(i))
left.append(get_left(i))
get_depth(i)
set_right()
for i in range(n):
out_str = 'node {}: parent = {}, depth = {}, '.format(i,parent[i],get_depth(i))
if parent[i] == -1:
out_str += 'root, '
elif len(child[i]) == 0:
out_str += 'leaf, '
else:
out_str += 'internal node, '
out_str += str(child[i])
print(out_str)
| Traceback (most recent call last):
File "/tmp/tmpfwth4klr/tmpy7g5_k6a.py", line 8, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s441715577 | p02279 | u088372268 | 1526803808 | Python | Python3 | py | Runtime Error | 30 | 5616 | 1568 | class Node:
def __init__(self, parent=-1,left=-1, right=-1):
self.parent = parent
self.left = left
self.right = right
n = int(input())
rooted_tree = [Node() for i in range(n)]
d = [0 for k in range(n)]
def set_depth(u, p):
d[u] = p
if rooted_tree[u].right != -1:
set_depth(rooted_tree[u].right, p)
if rooted_tree[u].left != -1:
set_depth(rooted_tree[u].left, p+1)
def print_child(u):
cur = rooted_tree[u].left
child_lst = []
while cur != -1:
child_lst.append(cur)
cur = rooted_tree[cur].right
return child_lst
def print_tree(u):
if rooted_tree[u].parent == -1:
t = "root"
elif rooted_tree[u].left == -1:
t = "leaf"
else:
t = "internal node"
print("node {}: parent = {}, depth = {}, {}, {}".
format(u, rooted_tree[u].parent, d[u], t, print_child(u)))
def main():
# 根付き木の構築
for i in range(n):
data = list(map(int, input().split()))
for j in range(data[1]):
rooted_tree[data[2+j]].parent = data[0]
if rooted_tree[data[0]].left == -1:
rooted_tree[data[0]].left = data[2+j]
if j != data[1] - 1:
rooted_tree[data[2+j]].right = data[3+j]
# 根の探索
root_idx = 0
for i in range(n):
if rooted_tree[i].parent == -1:
root_idx = i
# 深さの探索
set_depth(root_idx, 0)
# 木の出力
for i in range(n):
print_tree(i)
if __name__ == '__main__':
main()
| Traceback (most recent call last):
File "/tmp/tmpff30hagv/tmpsrx77o0z.py", line 8, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s294930554 | p02279 | u088372268 | 1526804484 | Python | Python3 | py | Runtime Error | 20 | 5620 | 1596 | import sys
class Node:
def __init__(self, parent=-1,left=-1, right=-1):
self.parent = parent
self.left = left
self.right = right
n = int(input())
rooted_tree = [Node() for i in range(n)]
d = [0 for k in range(n)]
def set_depth(u, p):
d[u] = p
if rooted_tree[u].right != -1:
set_depth(rooted_tree[u].right, p)
if rooted_tree[u].left != -1:
set_depth(rooted_tree[u].left, p+1)
def print_child(u):
cur = rooted_tree[u].left
child_lst = []
while cur != -1:
child_lst.append(cur)
cur = rooted_tree[cur].right
return child_lst
def print_tree(u):
if rooted_tree[u].parent == -1:
t = "root"
elif rooted_tree[u].left == -1:
t = "leaf"
else:
t = "internal node"
print("node {}: parent = {}, depth = {}, {}, {}".
format(u, rooted_tree[u].parent, d[u], t, print_child(u)))
def main():
# 根付き木の構築
for i in range(n):
data = [int(j) for j in sys.stdin.readline().split()]
for j in range(data[1]):
rooted_tree[data[2+j]].parent = data[0]
if rooted_tree[data[0]].left == -1:
rooted_tree[data[0]].left = data[2+j]
if j != data[1] - 1:
rooted_tree[data[2+j]].right = data[3+j]
# 根の探索
root_idx = 0
for i in range(n):
if rooted_tree[i].parent == -1:
root_idx = i
# 深さの探索
set_depth(root_idx, 0)
# 木の出力
for i in range(n):
print_tree(i)
if __name__ == '__main__':
main()
| Traceback (most recent call last):
File "/tmp/tmplz7qdrlt/tmp2n60067u.py", line 11, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s857527386 | p02279 | u843404779 | 1526910927 | Python | Python3 | py | Runtime Error | 20 | 5612 | 1466 | def rec_function(i, p):
node_info[i]['depth'] = p
if node_info[i]['parent'] == -1:
node_info[i]['type'] = 'root'
elif node_info[i]['left'] == None:
node_info[i]['type'] = 'leaf'
else:
node_info[i]['type'] = 'internal node'
if node_info[i]['right'] != None:
rec_function(node_info[i]['right'], p)
if node_info[i]['left'] != None:
rec_function(node_info[i]['left'], p + 1)
n = int(input())
node_info = [ {
'parent': None,
'right': None,
'left': None,
'depth': None,
'type': None,
'children': [],
} for _ in range(n) ]
for _ in range(n):
input_info = list(map(int, input().split()))
node = input_info[0]
children = input_info[2:]
node_info[node]['children'] = children
if len(children) > 0:
node_info[node]['left'] = children[0]
for i in range(len(children)):
node_info[children[i]]['parent'] = node
if i < len(children) - 1:
node_info[children[i]]['right'] = children[i+1]
root_node = 0
for i in range(len(node_info)):
if node_info[i]['parent'] == None:
node_info[i]['parent'] = -1
root_node = i
break
rec_function(root_node, 0)
for i in range(len(node_info)):
print("node {0}: parent = {1}, depth = {2}, {3}, {4}".format(
i,
node_info[i]['parent'],
node_info[i]['depth'],
node_info[i]['type'],
node_info[i]['children']
))
| Traceback (most recent call last):
File "/tmp/tmppl3jx8rq/tmpzwa9uqqz.py", line 17, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s738425317 | p02279 | u684241248 | 1527832719 | Python | Python3 | py | Runtime Error | 30 | 5612 | 2830 | class Tree:
def __init__(self, ary):
self.nodes = [Node(node, self) for node in ary]
[node.set() for node in self.nodes]
for node in self.nodes:
if node.parent == -1:
node.set_depth(0)
break
def output(self):
[node.output() for node in sorted(self.nodes, key=lambda x: x.no)]
class Node:
def __init__(self, node, tree):
self.tree = tree
self.no = node[0]
self.parent = -1
self.right = False
self.left = False
self.children = node[2:]
def set(self):
if self.children:
for i, child in enumerate(self.children):
self.tree.nodes[child].parent = self.no
if i < len(self.children) - 1:
self.tree.nodes[child].right = self.children[i + 1]
self.left = self.children[0]
def set_depth(self, d):
self.depth = d
if self.right:
self.tree.nodes[self.right].set_depth(d)
if self.left:
self.tree.nodes[self.left].set_depth(d + 1)
def output(self):
if self.parent == -1:
kind = 'root'
elif not self.children:
kind = 'leaf'
else:
kind = 'internal node'
print('node {}: parent = {}, depth = {}, {}, {}'.format(
self.no, self.parent, self.depth, kind, self.children))
if __name__ == '__main__':
import sys
n = int(input())
ary = [[int(_) for _ in line.strip().split()] for line in sys.stdin]
# ary = [[int(_) for _ in input().split()] for i in range(n)]
tree = Tree(ary)
tree.output()
# ary.sort(key=lambda x: x[0])
# bry = [{
# 'id': i,
# 'parent': -1,
# 'depth': 0,
# 'type': 'root',
# 'children': []
# } for i in range(n)]
# for node in ary:
# no = node[0]
# if node[1]:
# for cno in node[2:]:
# bry[no]['children'].append(cno)
# bry[cno]['parent'] = no
# if ary[cno][1]:
# bry[cno]['type'] = 'internal node'
# else:
# bry[cno]['type'] = 'leaf'
# else:
# if n > 1:
# bry[no]['type'] = 'leaf'
# tmp = [(node['id'], node['parent']) for node in bry]
# root = [min(tmp, key=lambda x: x[1])[0]]
# flag = True
# while flag:
# flag = False
# tmp = []
# for i in root:
# for cno in bry[i]['children']:
# bry[cno]['depth'] = bry[i]['depth'] + 1
# tmp.append(cno)
# flag = True
# root = tmp
# for node in bry:
# output(node['id'], node['parent'], node['depth'], node['type'],
# node['children'])
| Traceback (most recent call last):
File "/tmp/tmpu1tro84a/tmpgpwq6s96.py", line 53, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s942671362 | p02279 | u684241248 | 1527833038 | Python | Python3 | py | Runtime Error | 30 | 5612 | 2858 | class Tree:
def __init__(self, ary):
self.nodes = [Node(node, self) for node in ary]
[node.set() for node in self.nodes]
for node in self.nodes:
if node.parent == -1:
node.set_depth(0)
break
def output(self):
[node.output() for node in sorted(self.nodes, key=lambda x: x.no)]
class Node:
def __init__(self, node, tree):
self.tree = tree
self.no = node[0]
self.parent = -1
self.right = False
self.left = False
self.children = node[2:]
def set(self):
if self.children:
for i, child in enumerate(self.children):
self.tree.nodes[child].parent = self.no
if i < len(self.children) - 1:
self.tree.nodes[child].right = self.children[i + 1]
self.left = self.children[0]
def set_depth(self, d):
self.depth = d
if type(self.right) != bool:
self.tree.nodes[self.right].set_depth(d)
if type(self.left) != bool:
self.tree.nodes[self.left].set_depth(d + 1)
def output(self):
if self.parent == -1:
kind = 'root'
elif not self.children:
kind = 'leaf'
else:
kind = 'internal node'
print('node {}: parent = {}, depth = {}, {}, {}'.format(
self.no, self.parent, self.depth, kind, self.children))
if __name__ == '__main__':
import sys
n = int(input())
ary = [[int(_) for _ in line.strip().split()] for line in sys.stdin]
# ary = [[int(_) for _ in input().split()] for i in range(n)]
tree = Tree(ary)
tree.output()
# ary.sort(key=lambda x: x[0])
# bry = [{
# 'id': i,
# 'parent': -1,
# 'depth': 0,
# 'type': 'root',
# 'children': []
# } for i in range(n)]
# for node in ary:
# no = node[0]
# if node[1]:
# for cno in node[2:]:
# bry[no]['children'].append(cno)
# bry[cno]['parent'] = no
# if ary[cno][1]:
# bry[cno]['type'] = 'internal node'
# else:
# bry[cno]['type'] = 'leaf'
# else:
# if n > 1:
# bry[no]['type'] = 'leaf'
# tmp = [(node['id'], node['parent']) for node in bry]
# root = [min(tmp, key=lambda x: x[1])[0]]
# flag = True
# while flag:
# flag = False
# tmp = []
# for i in root:
# for cno in bry[i]['children']:
# bry[cno]['depth'] = bry[i]['depth'] + 1
# tmp.append(cno)
# flag = True
# root = tmp
# for node in bry:
# output(node['id'], node['parent'], node['depth'], node['type'],
# node['children'])
| Traceback (most recent call last):
File "/tmp/tmp1b485y7e/tmp6_g8pkx6.py", line 53, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s858134064 | p02279 | u684241248 | 1527834627 | Python | Python3 | py | Runtime Error | 20 | 5620 | 1714 | class Tree:
def __init__(self, ary):
self.nodes = [
Node(node, self) for node in sorted(ary, key=lambda x: x[0])
]
[node.set() for node in self.nodes]
for node in self.nodes:
if node.parent == -1:
node.set_depth(0)
break
def output(self):
[node.output() for node in self.nodes]
class Node:
def __init__(self, node, tree):
self.tree = tree
self.no = node[0]
self.parent = -1
self.right = False
self.left = False
self.children = node[2:]
def set(self):
if self.children:
for i, child in enumerate(self.children):
self.tree.nodes[child].parent = self.no
if i < len(self.children) - 1:
self.tree.nodes[child].right = self.children[i + 1]
self.left = self.children[0]
def set_depth(self, d):
self.depth = d
if type(self.right) != bool:
self.tree.nodes[self.right].set_depth(d)
if type(self.left) != bool:
self.tree.nodes[self.left].set_depth(d + 1)
def output(self):
if self.parent == -1:
kind = 'root'
elif not self.children:
kind = 'leaf'
else:
kind = 'internal node'
print('node {}: parent = {}, depth = {}, {}, {}'.format(
self.no, self.parent, self.depth, kind, self.children))
if __name__ == '__main__':
import sys
n = int(input())
ary = [[int(_) for _ in line.strip().split()] for line in sys.stdin]
# ary = [[int(_) for _ in input().split()] for i in range(n)]
tree = Tree(ary)
tree.output()
| Traceback (most recent call last):
File "/tmp/tmpwj5hyj5p/tmpe_kmupd7.py", line 55, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s431174756 | p02279 | u007270338 | 1528359251 | Python | Python3 | py | Runtime Error | 0 | 0 | 1409 | #coding:utf-8
n = int(input())
T = [list(map(int, input().split())) for i in range(n)]
A = []
def parentSearch():
numList = []
for t in T:
if t[1] != 0:
for num in t[2:]:
numList.append(num)
numList.sort()
for i in range(n-1):
if numList[i] != i:
return i
break
def rootedTrees(node, parent, depth):
t = T[node]
if t[1] != 0:
if parent == -1:
kind = "root"
else:
kind = "internal node"
A.append([t[0], parent, depth, kind, t[2:]])
parent = node
for i in t[2:]:
rootedTrees(i, parent, depth+1)
else:
A.append([t[0], parent, depth, "leaf", []])
def Merge(A, left, mid, right):
L = A[left:mid]
R = A[mid:right]
L.append([510000])
R.append([510000])
i,j = 0,0
for k in range(left,right):
if L[i][0] <= R[j][0]:
A[k] = L[i]
i += 1
else:
A[k] = R[j]
j += 1
def MergeSort(A, left, right):
if left+1 < right:
mid = (left + right)//2
MergeSort(A, left, mid)
MergeSort(A, mid, right)
Merge(A, left, mid, right)
node = parentSearch()
depth = 0
parent = -1
rootedTrees(node, parent, depth)
MergeSort(A, 0, n)
for t in A:
print("node {}: parent = {}, depth = {}, {}, {}".format(t[0],t[1],t[2],t[3],t[4]))
| Traceback (most recent call last):
File "/tmp/tmp4cy_403l/tmpu4v3n45f.py", line 3, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s006713076 | p02279 | u007270338 | 1528359865 | Python | Python3 | py | Runtime Error | 20 | 5624 | 1529 | #coding:utf-8
n = int(input())
T = [list(map(int, input().split())) for i in range(n)]
A = []
def parentSearch():
numList = []
for t in T:
if t[1] != 0:
for num in t[2:]:
numList.append(num)
numList.sort()
for i in range(n-1):
if numList[i] != i:
return i
break
def rootedTrees(node, parent, depth):
t = T[node]
if t[1] != 0:
if parent == -1:
kind = "root"
else:
kind = "internal node"
A.append([t[0], parent, depth, kind, t[2:]])
parent = node
for i in t[2:]:
rootedTrees(i, parent, depth+1)
else:
if parent == -1:
kind = "root"
else:
kind = "leaf"
A.append([t[0], parent, depth, kind, []])
def Merge(A, left, mid, right):
L = A[left:mid]
R = A[mid:right]
L.append([510000])
R.append([510000])
i,j = 0,0
for k in range(left,right):
if L[i][0] <= R[j][0]:
A[k] = L[i]
i += 1
else:
A[k] = R[j]
j += 1
def MergeSort(A, left, right):
if left+1 < right:
mid = (left + right)//2
MergeSort(A, left, mid)
MergeSort(A, mid, right)
Merge(A, left, mid, right)
node = parentSearch()
if node == None:
node = 0
depth = 0
parent = -1
rootedTrees(node, parent, depth)
MergeSort(A, 0, n)
for t in A:
print("node {}: parent = {}, depth = {}, {}, {}".format(t[0],t[1],t[2],t[3],t[4]))
| Traceback (most recent call last):
File "/tmp/tmpv80xau3a/tmpts3flgl0.py", line 3, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s942814181 | p02279 | u766183517 | 1529829373 | Python | Python3 | py | Runtime Error | 60 | 7164 | 3018 | from typing import List
class Node:
def __init__(self, node_id, parent_id):
self.node_id = node_id
self.parent_id = parent_id
self.children = []
self.depth = None
self.node_type =None
def __str__(self):
return str(self.node_id)
def __repr__(self):
return self.__str__()
class Tree:
def __init__(self, n):
self.root = None
self.n_node = n
self.node_list = [None for _ in range(n)]
def find_node(self, node_id: int):
if self.node_list[node_id] != None:
return self.node_list[node_id]
return None
def add_children_from_ids(self, node_id: int, children_ids: List[int]):
# check parent already exists
parent_node = self.find_node(node_id)
if parent_node == None:
# create new node from parent_id
parent_node = Node(node_id = node_id, parent_id = -1)
self.node_list[node_id] = parent_node
if len(children_ids) > 0:
# add all the children (unregistered)
# register parent_id, node_id, children
for child_id in children_ids:
child_node = self.find_node(child_id)
if child_node == None:
child_node = Node(node_id = child_id, parent_id = node_id)
else:
child_node.parent.parent_id = node_id
parent_node.children.append(child_node)
self.node_list[child_id] = child_node
def overwrite_node_type(self):
# find root and overwrite node type
for node in self.node_list:
# root, internal node, leaf
if node.parent_id == -1:
node_type = "root"
self.root = node
else:
if node.children != []:
node_type = "internal node"
else:
node_type = "leaf"
node.node_type = node_type
def overwrite_depth(self, node, depth):
if node.children == []:
node.depth = depth
# print("node.id: ", node.node_id, ", node.depth: ", node.depth)
return None
else:
for child_node in node.children:
self.overwrite_depth(child_node, depth+1)
return None
def print_nodes(self):
for node in self.node_list:
print("node {}: parent = {}, depth = {}, {}, {}".format(
node.node_id, node.parent_id, node.depth, node.node_type, node.children
))
def main():
n = int(input())
tree = Tree(n)
for _ in range(n):
node_id, k, *children = map(int, input().split())
tree.add_children_from_ids(node_id = node_id, children_ids = children)
tree.overwrite_node_type()
tree.overwrite_depth(tree.root, 0)
tree.print_nodes()
main()
| Traceback (most recent call last):
File "/tmp/tmp4e3zk_2k/tmpzqnys9yd.py", line 93, in <module>
main()
File "/tmp/tmp4e3zk_2k/tmpzqnys9yd.py", line 84, in main
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s726852183 | p02280 | u760378812 | 1541046252 | Python | Python3 | py | Runtime Error | 0 | 0 | 2264 | #include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <deque>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <unordered_map>
using namespace std;
#define MAX 30
#define NIL -1
struct Node{
int parent,left,right;
};
Node T[MAX];
int n, D[MAX], H[MAX];
void setDepth(int u, int d) {
if( u == NIL){
return;
}
D[u] = d;
setDepth(T[u].left, d+1);
setDepth(T[u].right, d+1);
}
int setHeight(int u){
int h1 = 0, h2=0;
if(T[u].left != NIL){
h1 = setHeight(T[u].left) + 1;
}
if(T[u].right != NIL){
h2 = setHeight(T[u].right) + 1;
}
return H[u] = h1 > h2 ? h1:h2;
}
int getSibling(int u){
if(T[u].parent == NIL){
return NIL;
}
if(T[T[u].parent].left != u and T[T[u].parent].left != NIL){
return T[T[u].parent].left;
}
if(T[T[u].parent].right != u and T[T[u].parent].right != NIL){
return T[T[u].parent].right;
}
return NIL;
}
void print(int u){
cout << "node " << u << ": ";
cout << "parent = " << T[u].parent << ", ";
cout << "sibling = " << getSibling(u) << ", ";
int deg = 0;
if(T[u].left != NIL){
deg++;
}
if(T[u].right != NIL){
deg ++;
}
cout << "degree = " << deg << ", ";
cout << "depth = " << D[u] << ", ";
cout << "height = " << H[u] << ", ";
if(T[u].parent == NIL){
cout << "root" << endl;
}else if (T[u].left != NIL or T[u].right != NIL){
cout << "internal node" << endl;
}else{
cout << "leaf" << endl;
}
}
int main() {
int v,l,r, root = 0;
cin >> n;
for (int i=0;i<n;i++){
T[i].parent = NIL;
}
for (int i=0;i<n;i++){
cin >> v >> l >> r;
T[v].left = l;
T[v].right = r;
if(l!= NIL){
T[l].parent = v;
}
if(r != NIL){
T[r].parent = v;
}
}
for(int i=0;i<n;i++){
if(T[i].parent == NIL){
root = i;
}
}
setDepth(root,0);
setHeight(root);
for (int i=0;i<n;i++){
print(i);
}
}
| File "/tmp/tmps9cl3umc/tmpxvns4abf.py", line 19
using namespace std;
^^^^^^^^^
SyntaxError: invalid syntax
| |
s051944439 | p02280 | u096660561 | 1556099151 | Python | Python3 | py | Runtime Error | 0 | 0 | 2134 | n = int(input())
class BiTree():
def __init__(self, node, left, right):
self.node = node
self.left = left
self.right = right
dali = [0] * n
def inputdata(i):
node, left, right = list(map(int, input().split()))
dali[i] = BiTree(node, left, right)
def getParent(i):
for k in range(n):
if(dali[k].right == dali[i].node) or (dali[k].left == dali[i].node):
dali[i].parent = dali[k].node
return dali[i].parent
dali[i].parent = -1
return dali[i].parent
def getDepth(i):
h = 0
tmp = dali[i].parent
while True:
if tmp == -1:
dali[i].depth = h
break
else:
tmp = getParent(tmp)
h += 1
def getHeight(i): # i == daliの要素番号
h1 = 0
h2 = 0
if dali[i].right != -1:
h1 = getHeight(dali[i].right) + 1
if dali[i].left != -1:
h2 = getHeight(dali[i].left) + 1
return max(h1, h2)
def getDegree(i):
if dali[i].right == -1:
dali[i].deg = 0
else:
dali[i].deg = 2
def getType(i):
if dali[i].parent == -1:
dali[i].type = "root"
elif dali[i].deg == 0:
dali[i].type = "leaf"
else:
dali[i].type = "internal node"
def getSibling(i):
if dali[i].type == "root":
dali[i].sibling = -1
return 0
sib = []
sib.append((dali[dali[i].parent].right))
sib.append(dali[dali[i].parent].left)
if sib[0] == dali[i].node:
dali[i].sibling = sib[1]
else:
dali[i].sibling = sib[0]
for j in range(n):
inputdata(j)
for j in range(n):
getParent(j)
for j in range(n):
getDepth(j)
h1 = 0
h2 = 0
for j in range(n):
dali[j].height = getHeight(j)
print(h1,h2,j)
h1 = 0
h2 = 0
for j in range(n):
getDegree(j)
for j in range(n):
getType(j)
for j in range(n):
getSibling(j)
for j in range(n):
print("node %d: parent = %d,sibling = %d, degree = %d, depth = %d, height = %d, %s" % (
dali[j].node, dali[j].parent,dali[j].sibling, dali[j].deg, dali[j].depth, dali[j].height, dali[j].type))
| Traceback (most recent call last):
File "/tmp/tmpyhsqmlpm/tmpydu669s_.py", line 1, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s729774311 | p02280 | u096660561 | 1556099228 | Python | Python3 | py | Runtime Error | 0 | 0 | 2134 | n = int(input())
class BiTree():
def __init__(self, node, left, right):
self.node = node
self.left = left
self.right = right
dali = [0] * n
def inputdata(i):
node, left, right = list(map(int, input().split()))
dali[i] = BiTree(node, left, right)
def getParent(i):
for k in range(n):
if(dali[k].right == dali[i].node) or (dali[k].left == dali[i].node):
dali[i].parent = dali[k].node
return dali[i].parent
dali[i].parent = -1
return dali[i].parent
def getDepth(i):
h = 0
tmp = dali[i].parent
while True:
if tmp == -1:
dali[i].depth = h
break
else:
tmp = getParent(tmp)
h += 1
def getHeight(i): # i == daliの要素番号
h1 = 0
h2 = 0
if dali[i].right != -1:
h1 = getHeight(dali[i].right) + 1
if dali[i].left != -1:
h2 = getHeight(dali[i].left) + 1
return max(h1, h2)
def getDegree(i):
if dali[i].right == -1:
dali[i].deg = 0
else:
dali[i].deg = 2
def getType(i):
if dali[i].parent == -1:
dali[i].type = "root"
elif dali[i].deg == 0:
dali[i].type = "leaf"
else:
dali[i].type = "internal node"
def getSibling(i):
if dali[i].type == "root":
dali[i].sibling = -1
return 0
sib = []
sib.append((dali[dali[i].parent].right))
sib.append(dali[dali[i].parent].left)
if sib[0] == dali[i].node:
dali[i].sibling = sib[1]
else:
dali[i].sibling = sib[0]
for j in range(n):
inputdata(j)
for j in range(n):
getParent(j)
for j in range(n):
getDepth(j)
h1 = 0
h2 = 0
for j in range(n):
dali[j].height = getHeight(j)
print(h1,h2,j)
h1 = 0
h2 = 0
for j in range(n):
getDegree(j)
for j in range(n):
getType(j)
for j in range(n):
getSibling(j)
for j in range(n):
print("node %d: parent = %d,sibling = %d, degree = %d, depth = %d, height = %d, %s" % (
dali[j].node, dali[j].parent,dali[j].sibling, dali[j].deg, dali[j].depth, dali[j].height, dali[j].type))
| Traceback (most recent call last):
File "/tmp/tmpahon7p7t/tmpju78b9yg.py", line 1, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s912016184 | p02280 | u096660561 | 1556100905 | Python | Python3 | py | Runtime Error | 0 | 0 | 2253 | n = int(input())
class BiTree():
def __init__(self, node, left, right):
self.node = node
self.left = left
self.right = right
dali = [0] * n
def inputdata(i):
node, left, right = list(map(int, input().split()))
dali[node] = BiTree(node, left, right)
def getParent(i):
for k in range(n):
if(dali[k].right == dali[i].node) or (dali[k].left == dali[i].node):
dali[i].parent = dali[k].node
return dali[i].parent
dali[i].parent = -1
return dali[i].parent
def getDepth(i):
h = 0
tmp = dali[i].parent
while True:
if tmp == -1:
dali[i].depth = h
break
else:
tmp = getParent(tmp)
h += 1
def getHeight(i): # i == daliの要素番号
h1 = 0
h2 = 0
if dali[i].right != -1:
h1 = getHeight(dali[i].right) + 1
if dali[i].left != -1:
h2 = getHeight(dali[i].left) + 1
return max(h1, h2)
def getDegree(i):
if dali[i].right == -1 and dali[i].left == -1:
dali[i].deg = 0
elif. (dali[i].right) * dali[i].left == -1:
dali[i].deg = 1
else:
dali[i].deg = 2
def getType(i):
if dali[i].parent == -1:
dali[i].type = "root"
elif dali[i].deg == 0:
dali[i].type = "leaf"
else:
dali[i].type = "internal node"
def getSibling(i):
if dali[i].type == "root":
dali[i].sibling = -1
return 0
sib = []
sib.append((dali[dali[i].parent].right))
sib.append(dali[dali[i].parent].left)
if sib[0] == dali[i].node:
dali[i].sibling = sib[1]
else:
dali[i].sibling = sib[0]
for j in range(n):
inputdata(j)
for j in range(n):
getParent(dali[j].node)
for j in range(n):
getDepth(dali[j].node)
h1 = 0
h2 = 0
for j in range(n):
dali[j].height = getHeight(dali[j].node)
h1 = 0
h2 = 0
for j in range(n):
getDegree(dali[j].node)
for j in range(n):
getType(dali[j].node)
for j in range(n):
getSibling(dali[j].node)
for j in range((dali[j].node)+1):
print("node %d: parent = %d, sibling = %d, degree = %d, depth = %d, height = %d, %s" % (dali[j].node, dali[j].parent, dali[j].sibling, dali[j].deg, dali[j].depth, dali[j].height, dali[j].type))
| File "/tmp/tmphp3y5duv/tmpyvkcz3x5.py", line 42
elif. (dali[i].right) * dali[i].left == -1:
^
SyntaxError: invalid syntax
| |
s062785662 | p02280 | u805716376 | 1556793552 | Python | Python3 | py | Runtime Error | 20 | 5628 | 930 | n = int(input())
deg = [0]*n
child, parent = {}, {}
sib = [None]*n
for i in range(n):
a = list(map(int, input().split()))
if a[1] != -1:
child[a[0]] = a[1:]
sib[a[1]] = a[2]
sib[a[2]] = a[1]
deg[a[0]] = 2
for j in child[a[0]]:
parent[j] = a[0]
else:
child[a[0]] = []
root = (set(child)-set(parent)).pop()
depth = [None]*n
depth[root] = 0
hei = [None]*n
parent[root] = -1
sib[root] = -1
def dfs(s):
if len(child[s]) == 0:
hei[s] = 0
return
for i in child[s]:
depth[i] = depth[s] + 1
dfs(i)
hei[s] = max(hei[i] for i in child[s]) + 1
dfs(root)
for i in range(n):
node_type = 'root' if parent[i] == -1 else 'leaf' if len(child[i]) == 0 else 'internal node'
print(f'node {i}: parent = {parent[i]}, sibling = {sib[i]}, degree = {deg[i]}, depth = {depth[i]}, height = {hei[i]}, {node_type}')
| Traceback (most recent call last):
File "/tmp/tmp693bcecb/tmpnsupo7zl.py", line 1, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s333370671 | p02280 | u805716376 | 1556794192 | Python | Python3 | py | Runtime Error | 20 | 5632 | 1072 | n = int(input())
deg = [0]*n
child, parent = {}, {}
sib = [None]*n
for i in range(n):
a = list(map(int, input().split()))
if a[1] != -1:
if a[2] == -1:
child[a[0]] = [a[1]]
sib[a[1]] = a[2]
deg[a[0]] = 1
else:
child[a[0]] = a[1:]
sib[a[1]] = a[2]
sib[a[2]] = a[1]
deg[a[0]] = 2
for j in child[a[0]]:
parent[j] = a[0]
else:
child[a[0]] = []
root = (set(child)-set(parent)).pop()
depth = [None]*n
depth[root] = 0
hei = [None]*n
parent[root] = -1
sib[root] = -1
def dfs(s):
if len(child[s]) == 0:
hei[s] = 0
return
for i in child[s]:
depth[i] = depth[s] + 1
dfs(i)
hei[s] = max(hei[i] for i in child[s]) + 1
dfs(root)
for i in range(n):
node_type = 'root' if parent[i] == -1 else 'leaf' if len(child[i]) == 0 else 'internal node'
print(f'node {i}: parent = {parent[i]}, sibling = {sib[i]}, degree = {deg[i]}, depth = {depth[i]}, height = {hei[i]}, {node_type}')
| Traceback (most recent call last):
File "/tmp/tmp6dl959yi/tmpwnhui5m8.py", line 1, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s327342142 | p02280 | u888548672 | 1556871625 | Python | Python3 | py | Runtime Error | 0 | 0 | 2951 | #include <iostream>
#define TREE_SIZE 30
#define NIL -1
using namespace std;
struct Node
{
int parent_node = NIL;
int left_child = NIL;
int right_child = NIL;
};
Node Tree[TREE_SIZE];
int Depth[TREE_SIZE];
int Height[TREE_SIZE];
// set depth of all nodes by recursion
void Set_depth_of_node(int node_id, int depth)
{
Depth[node_id] = depth;
// if there is a child
if (Tree[node_id].left_child != NIL) Set_depth_of_node(Tree[node_id].left_child, depth+1);
if (Tree[node_id].right_child != NIL) Set_depth_of_node(Tree[node_id].right_child, depth+1);
}
// set height of all nodes by recursion
int Set_height_of_node(int node_id)
{
int l_height = 0, r_height = 0;
// if there is a child
if (Tree[node_id].left_child != NIL) l_height = Set_height_of_node(Tree[node_id].left_child) + 1;
if (Tree[node_id].right_child != NIL) r_height = Set_height_of_node(Tree[node_id].right_child) + 1;
return Height[node_id] = (l_height < r_height ? r_height : l_height);
}
int main()
{
int node_num, node_id, l_child, r_child;
scanf("%d", &node_num);
for (int i = 0; i < node_num; ++i)
{
scanf("%d %d %d", &node_id, &l_child, &r_child);
Tree[node_id].left_child = l_child; // set left child
Tree[node_id].right_child = r_child; // set right child
// set parent if the node has children
if (l_child != -1) Tree[l_child].parent_node = node_id;
if (r_child != -1) Tree[r_child].parent_node = node_id;
}
// search root of Tree
int root = 0;
while (Tree[root].parent_node != NIL)
{
root++;
}
// set depth of all nodes
Set_depth_of_node(root, 0);
// set height of all nodes
Set_height_of_node(root);
// print out below
for (int i = 0; i < node_num; ++i)
{
printf("node %d: parent = %d, ", i, Tree[i].parent_node);
// print out siblings
if (Tree[i].parent_node == NIL) printf("sibling = -1, "); // if this is root
else
{
if (i == Tree[Tree[i].parent_node].left_child) // if this is left child, print right child
{
printf("sibling = %d, ", Tree[Tree[i].parent_node].right_child);
}
else // if this is right child, print left child
{
printf("sibling = %d, ", Tree[Tree[i].parent_node].left_child);
}
}
// print out degree (number of children)
int degree = 0;
if (Tree[i].left_child != NIL) degree++;
if (Tree[i].right_child != NIL) degree++;
printf("degree = %d, ", degree);
// print out depth
printf("depth = %d, ", Depth[i]);
// print out height
printf("height = %d, ", Height[i]);
// print out position
if (i == root) printf("root\n");
else if (degree == 0) printf("leaf\n");
else printf("internal node\n");
}
}
| File "/tmp/tmp85ydkyy6/tmp6xr3inja.py", line 4
using namespace std;
^^^^^^^^^
SyntaxError: invalid syntax
| |
s070505849 | p02280 | u888548672 | 1556871686 | Python | Python3 | py | Runtime Error | 0 | 0 | 2930 | #include <iostream>
#define TREE_SIZE 30
#define NIL -1
struct Node
{
int parent_node = NIL;
int left_child = NIL;
int right_child = NIL;
};
Node Tree[TREE_SIZE];
int Depth[TREE_SIZE];
int Height[TREE_SIZE];
// set depth of all nodes by recursion
void Set_depth_of_node(int node_id, int depth)
{
Depth[node_id] = depth;
// if there is a child
if (Tree[node_id].left_child != NIL) Set_depth_of_node(Tree[node_id].left_child, depth+1);
if (Tree[node_id].right_child != NIL) Set_depth_of_node(Tree[node_id].right_child, depth+1);
}
// set height of all nodes by recursion
int Set_height_of_node(int node_id)
{
int l_height = 0, r_height = 0;
// if there is a child
if (Tree[node_id].left_child != NIL) l_height = Set_height_of_node(Tree[node_id].left_child) + 1;
if (Tree[node_id].right_child != NIL) r_height = Set_height_of_node(Tree[node_id].right_child) + 1;
return Height[node_id] = (l_height < r_height ? r_height : l_height);
}
int main()
{
int node_num, node_id, l_child, r_child;
scanf("%d", &node_num);
for (int i = 0; i < node_num; ++i)
{
scanf("%d %d %d", &node_id, &l_child, &r_child);
Tree[node_id].left_child = l_child; // set left child
Tree[node_id].right_child = r_child; // set right child
// set parent if the node has children
if (l_child != -1) Tree[l_child].parent_node = node_id;
if (r_child != -1) Tree[r_child].parent_node = node_id;
}
// search root of Tree
int root = 0;
while (Tree[root].parent_node != NIL)
{
root++;
}
// set depth of all nodes
Set_depth_of_node(root, 0);
// set height of all nodes
Set_height_of_node(root);
// print out below
for (int i = 0; i < node_num; ++i)
{
printf("node %d: parent = %d, ", i, Tree[i].parent_node);
// print out siblings
if (Tree[i].parent_node == NIL) printf("sibling = -1, "); // if this is root
else
{
if (i == Tree[Tree[i].parent_node].left_child) // if this is left child, print right child
{
printf("sibling = %d, ", Tree[Tree[i].parent_node].right_child);
}
else // if this is right child, print left child
{
printf("sibling = %d, ", Tree[Tree[i].parent_node].left_child);
}
}
// print out degree (number of children)
int degree = 0;
if (Tree[i].left_child != NIL) degree++;
if (Tree[i].right_child != NIL) degree++;
printf("degree = %d, ", degree);
// print out depth
printf("depth = %d, ", Depth[i]);
// print out height
printf("height = %d, ", Height[i]);
// print out position
if (i == root) printf("root\n");
else if (degree == 0) printf("leaf\n");
else printf("internal node\n");
}
}
| File "/tmp/tmp461atlmr/tmpyyf33q1q.py", line 5
struct Node
^^^^
SyntaxError: invalid syntax
| |
s819383985 | p02280 | u198069342 | 1416408213 | Python | Python | py | Runtime Error | 0 | 0 | 513 | def f(n,d):
t=[f(i,d+1)+1 for i in l[n][1]]
h=0 if t==[] else max(t)
c[n][3:3]=[len(l[n][1]),d,h,"root" if d<1 else "leaf" if h<1 else "internal node"]
return h
m=input()
c=[0]*m
l=sorted([map(int,raw_input().split()) for _ in range(m)])
for i in l:
n,a,b=i
if a>0:c[a]=[a,n,b]
if b>0:c[b]=[b,n,a]
i[1:]=[[j for j in i[1:] if j>=0]]
n=c.index(0)
c[n]=[n,-1,-1]
f(n,0)
for i in c:print "node {0}: parent = {1}, sibling = {2}, degree = {3}, depth = {4}, height = {5}, {6}".format(*i) | File "/tmp/tmpkwd5emio/tmpm4b57a6q.py", line 17
for i in c:print "node {0}: parent = {1}, sibling = {2}, degree = {3}, depth = {4}, height = {5}, {6}".format(*i)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
| |
s171568375 | p02280 | u567281053 | 1460132212 | Python | Python | py | Runtime Error | 0 | 0 | 807 | 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) | File "/tmp/tmp3uikpxg4/tmp6gbb0htb.py", line 26
if tree[child].type != "leaf"
^
SyntaxError: expected ':'
| |
s340419842 | p02280 | u567281053 | 1460221194 | Python | Python | py | Runtime Error | 10 | 6420 | 2130 | import sys
class Node:
def __init__(self, id, left, right, degree, depth, type):
self.id = id
self.parent = None
self.sibling = -1
self.degree = degree
self.depth = depth
self.height = 0
self.type = type
self.left = left
self.right = right
def printInfo(self):
print "node {0.id}: parent = {0.parent.id}, sibling = {0.sibling}, degree = {0.degree}, depth = {0.depth}, height = {0.height}, {0.type}".format(self)
def buildTree(id, nodeList, depth):
leftId, rightId = nodeList[id][0], nodeList[id][1]
if leftId == -1 and rightId == -1:
dummy = Node(-1, None, None, 0, 0, "")
return Node(id, dummy, dummy, 0, depth, "leaf")
degree = 0
if leftId == -1:
left = Node(-1, None, None, 0, 0, "")
else:
left = buildTree(leftId, nodeList, depth + 1)
degree += 1
if rightId == -1:
right = Node(-1, None, None, 0, 0, "")
else:
right = buildTree(rightId, nodeList, depth + 1)
degree += 1
node = Node(id, left, right, degree, depth, "internal node")
node.left.parent = node
node.left.sibling = node.right.id
node.right.parent = node
node.right.sibling = node.left.id
node.height = max(node.left.height, node.right.height) + 1
return node
def tree2linear(node, list):
list[node.id] = node
if node.left.id != -1:
tree2linear(node.left, list)
if node.right.id != -1:
tree2linear(node.right, list)
if __name__ == "__main__":
lines = sys.stdin.readlines()
N = int(lines[0])
del lines[0]
nodeList = [[]] * N
sum = N * (N - 1) / 2
for line in lines:
line = map(int, line.split())
id, children = line[0], line[1:]
nodeList[id] = children
for child in children:
if child == -1:
break
sum -= child
root = buildTree(sum, nodeList, 0)
root.type = "root"
root.parent = Node(-1, None, None, 0, 0, "")
nodeList = [0] * N
tree2linear(root, nodeList)
for node in nodeList:
node.printInfo() | File "/tmp/tmpnh00swkt/tmp2wluippc.py", line 16
print "node {0.id}: parent = {0.parent.id}, sibling = {0.sibling}, degree = {0.degree}, depth = {0.depth}, height = {0.height}, {0.type}".format(self)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
| |
s380453206 | p02280 | u890722286 | 1474811819 | Python | Python3 | py | Runtime Error | 30 | 7828 | 1510 | def print_node(i, node):
parent = -1 if node[1] == None else node[1]
degree = len([x for x in node[0] if x != -1])
sibling = -1 if parent == -1 else node[2]
depth = node[3]
height = node[4]
t = 'root' if parent == -1 else 'leaf' if height == 0 else 'internal node'
ans = "node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, {}".format(i, parent, sibling, degree, depth, height, t)
print(ans)
n = int(input())
root = set([x for x in range(n)])
T = [None] * n
for x in range(n):
i, l, r = list(map(int, input().split()))
# children, parent, sibling, depth, height
T[i] = [[l, r], None, None, None, None]
root -= set([l, r])
def set_info(i, depth):
node = T[i]
node[3] = depth
l = None
r = None
if node[0][0] != -1:
l = T[node[0][0]]
l[1] = i
set_info(node[0][0], depth + 1)
if node[0][1] != -1:
r = T[node[0][1]]
r[1] = i
set_info(node[0][1], depth + 1)
if l != None and r != None:
l[2] = node[0][1]
r[2] = node[0][0]
node[4] = height(i)
def height(i):
node = T[i]
l = node[0][0]
r = node[0][1]
lh = None
rh = None
h = None
if l != -1:
lh = height(l)
if r != -1:
rh = height(r)
if lh != None or rh != None:
h = max(lh, rh)
if h == None:
return 0
else:
return 1 + h
r = root.pop()
T[r][1] = -1
set_info(r, 0)
for i, n in enumerate(T):
print_node(i, n) | Traceback (most recent call last):
File "/tmp/tmpmwdxzabo/tmpq1jbei92.py", line 11, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s383395691 | p02280 | u711765449 | 1486880084 | Python | Python3 | py | Runtime Error | 0 | 0 | 1636 | NIL = -1
class Node:
parent,left,right = NIL,NIL,NIL
type = NIL
depth = NIL
sibling = NIL
degree = NIL
def setHeight(H,u):
h1 = h2 = 0
if T[u].right != NIL:
h1 = setHeight(H,T[u].right) + 1
if T[u].left != NIL:
h2 = setHeight(H,T[u].left) + 1
return max(h1,h2)
def getDepth(u):
d = 0
while T[u].parent != NIL:
u = T[u].parent
d += 1
return d
def getSibling(u):
if T[u].parent == NIL:
return NIL
if T[T[u].parent].left != u and T[T[u].parent].left != NIL:
return T[T[u].parent].left
if T[T[u].parent].right != u and T[T[u].parent].right != NIL:
return T[T[u].parent].right
def getDegree(u):
deg = 0
if T[u].left != NIL:
deg += 1
if T[u].right != NIL:
deg += 1
return deg
n = int(input())
T = [0]*n
for i in range(n):
T[i] = Node()
T[i].id, T[i].left, T[i].right = map(int,input().split())
for i in range(n):
if T[i].left != NIL:
T[T[i].left].parent = T[i].id
if T[i].right != NIL:
T[T[i].right].parent = T[i].id
for i in range(n):
if T[i].parent == NIL:
T[i].type = 'root'
elif T[i].parent != NIL and (T[i].left != NIL or T[i].right != NIL):
T[i].type = 'internal node'
else:
T[i].type = 'leaf'
for i in range(n):
T[i].depth = getDepth(i)
T[i].sibling = getSibling(i)
T[i].degree = getDegree(i)
for i in range(n):
print('node ',T[i].id,': parent = ',T[i].parent,', sibling = ',T[i].sibling,', degree = ',T[i].degree,', depth = ',T[i].depth,', height = ',setHeight(0,i),', ',T[i].type,sep = '') | Traceback (most recent call last):
File "/tmp/tmpqwtktad3/tmpazuluyrm.py", line 40, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s916917488 | p02280 | u370086573 | 1493717312 | Python | Python3 | py | Runtime Error | 0 | 0 | 1742 | class Node:
def __init__(self, id, left, right):
self.id = id
self.right = right
self.left = left
self.parent = -1
def setHeight(u):
global T, H
hr = hl = 0
if T[u].right != -1:
hr = setHeight(T[u].right) + 1
if T[u].left != -1:
hl = setHeight(T[u].left) + 1
H[u] = max(hr, hl)
return H[u]
def setDepth(u, d):
global T, D
D[u] = d
if T[u].right != -1: setDepth(T[u].right, d + 1)
if T[u].left != -1: setDepth(T[u].left, d + 1)
def getSibling(u):
global T
if T[u].parent == -1: return False
if T[T[u].parent].right != u and T[T[u].parent].right != -1:
return T[T[u].parent].right
if T[T[u].parent].left != u and T[T[u].parent].left != -1:
return T[T[u].parent].left
return -1
def deg(u):
global T
dg = 0
if T[u].right != -1: dg += 1
if T[u].left != -1: dg += 1
return dg
def label(u):
if T[u].parent == -1:
return "root"
elif T[u].right == -1 and T[u].left == -1:
return "leaf"
else:
return "internal node"
if __name__ == "__main__":
n = int(input())
T = []
D = [-1] * n
H = [-1] * n
for i in range(n):
tmp = list(map(int, input().split()))
T.append(Node(tmp[0], tmp[1], tmp[2]))
# parent?????£??\
for i in range(n):
if T[i].right != -1:
T[T[i].right].parent = i
if T[i].left != -1:
T[T[i].left].parent = i
setHeight(0)
setDepth(0, 0)
for i in range(n):
print("node {0}: parent = {1}, sibling = {2}, degree = {3}, depth = {4}, height = {5}, {6}" \
.format(T[i].id, T[i].parent, getSibling(i), deg(i), D[i], H[i], label(i))) | Traceback (most recent call last):
File "/tmp/tmpkrx5m_ir/tmpwecob3wt.py", line 57, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s534724905 | p02280 | u370086573 | 1493717345 | Python | Python3 | py | Runtime Error | 0 | 0 | 1742 | class Node:
def __init__(self, id, left, right):
self.id = id
self.right = right
self.left = left
self.parent = -1
def setHeight(u):
global T, H
hr = hl = 0
if T[u].right != -1:
hr = setHeight(T[u].right) + 1
if T[u].left != -1:
hl = setHeight(T[u].left) + 1
H[u] = max(hr, hl)
return H[u]
def setDepth(u, d):
global T, D
D[u] = d
if T[u].right != -1: setDepth(T[u].right, d + 1)
if T[u].left != -1: setDepth(T[u].left, d + 1)
def getSibling(u):
global T
if T[u].parent == -1: return False
if T[T[u].parent].right != u and T[T[u].parent].right != -1:
return T[T[u].parent].right
if T[T[u].parent].left != u and T[T[u].parent].left != -1:
return T[T[u].parent].left
return -1
def deg(u):
global T
dg = 0
if T[u].right != -1: dg += 1
if T[u].left != -1: dg += 1
return dg
def label(u):
if T[u].parent == -1:
return "root"
elif T[u].right == -1 and T[u].left == -1:
return "leaf"
else:
return "internal node"
if __name__ == "__main__":
n = int(input())
T = []
D = [-1] * n
H = [-1] * n
for i in range(n):
tmp = list(map(int, input().split()))
T.append(Node(tmp[0], tmp[1], tmp[2]))
# parent?????£??\
for i in range(n):
if T[i].right != -1:
T[T[i].right].parent = i
if T[i].left != -1:
T[T[i].left].parent = i
setHeight(0)
setDepth(0, 0)
for i in range(n):
print("node {0}: parent = {1}, sibling = {2}, degree = {3}, depth = {4}, height = {5}, {6}" \
.format(T[i].id, T[i].parent, getSibling(i), deg(i), D[i], H[i], label(i))) | Traceback (most recent call last):
File "/tmp/tmp4id179bt/tmp68ck_2gd.py", line 57, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s996209854 | p02280 | u370086573 | 1493717673 | Python | Python3 | py | Runtime Error | 0 | 0 | 1739 | class Node:
def __init__(self, id, left, right):
self.id = id
self.right = right
self.left = left
self.parent = -1
def setHeight(u):
global T, H
hr = hl = 0
if T[u].right != -1:
hr = setHeight(T[u].right) + 1
if T[u].left != -1:
hl = setHeight(T[u].left) + 1
H[u] = max(hr, hl)
return H[u]
def setDepth(u, d):
global T, D
D[u] = d
if T[u].right != -1: setDepth(T[u].right, d + 1)
if T[u].left != -1: setDepth(T[u].left, d + 1)
def getSibling(u):
global T
if T[u].parent == -1: return -1
if T[T[u].parent].right != u and T[T[u].parent].right != -1:
return T[T[u].parent].right
if T[T[u].parent].left != u and T[T[u].parent].left != -1:
return T[T[u].parent].left
return -1
def deg(u):
global T
dg = 0
if T[u].right != -1: dg += 1
if T[u].left != -1: dg += 1
return dg
def label(u):
if T[u].parent == -1:
return "root"
elif T[u].right == -1 and T[u].left == -1:
return "leaf"
else:
return "internal node"
if __name__ == "__main__":
n = int(input())
T = []
D = [-1] * n
H = [-1] * n
for i in range(n):
tmp = list(map(int, input().split()))
T.append(Node(tmp[0], tmp[1], tmp[2]))
# parent?????£??\
for i in range(n):
if T[i].right != -1:
T[T[i].right].parent = i
if T[i].left != -1:
T[T[i].left].parent = i
setHeight(0)
setDepth(0, 0)
for i in range(n):
print("node {0}: parent = {1}, sibling = {2}, degree = {3}, depth = {4}, height = {5}, {6}" \
.format(T[i].id, T[i].parent, getSibling(i), deg(i), D[i], H[i], label(i))) | Traceback (most recent call last):
File "/tmp/tmpipnyp3pt/tmpvcpl_gtb.py", line 57, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s437840033 | p02280 | u990398499 | 1497590127 | Python | Python3 | py | Runtime Error | 0 | 0 | 1781 | N = int(input())
M = []
type = ['root','internal node','leaf']
class node:
def __init__(self,idx,parent,left,right):
self.idx = idx
self.parent = parent
self.left = left
self.right= right
self.sibling = -1
self.degree= -1
self.depth = -1
self.height = 0
self.type=-1
def tostr(self):
print('node %d: parent = %d, sibling = %d, degree = %d, depth = %d, height = %d, %s' % (self.idx, self.parent, self.sibling, self.degree, self.depth, self.height, type[self.type]))
tree = []
#'idx, parent, sibling, degree, depth, height, type'
for a in range(N):
idx,left,right = map(int,input().strip().split(' '))
tree.append(node(idx, -1, left,right))
for a in range(N):
nd = tree[a]
if nd.left != -1:
tree[nd.left].parent = a
if nd.right != -1:
tree[nd.right].parent = a
tree[nd.left].sibling = nd.right
tree[nd.right].sibling = nd.left
if nd.left == nd.right :
nd.type=2
nd.degree=0
else:
nd.type=1
if nd.left == -1 or nd.right == -1:
nd.degree = 1
else:
nd.degree = 2
root = ''
for a in range(N):
if tree[a].parent == -1:
root = a
tree[root].type=0
def search(root,depth):
root = tree[root]
root.depth = depth
if root.type == 2:
return
else:
search(root.right,depth+1)
search(root.left,depth+1)
search(root,0)
def height(root,hei):
root = tree[root]
if root.type==2:
root.height=0
return hei
else:
return max(height(root.left,hei+1),height(root.right,hei+1))
for i in range(N):
tree[i].height=height(i,0)
for a in range(N):
tree[a].tostr()
| Traceback (most recent call last):
File "/tmp/tmpbezn162c/tmpgcngzyxc.py", line 1, in <module>
N = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s695674752 | p02280 | u990398499 | 1497590392 | Python | Python3 | py | Runtime Error | 0 | 0 | 1945 | N = int(input())
M = []
type = ['root','internal node','leaf']
class node:
def __init__(self,idx,parent,left,right):
self.idx = idx
self.parent = parent
self.left = left
self.right= right
self.sibling = -1
self.degree= -1
self.depth = -1
self.height = 0
self.type=-1
def tostr(self):
print('node %d: parent = %d, sibling = %d, degree = %d, depth = %d, height = %d, %s' % (self.idx, self.parent, self.sibling, self.degree, self.depth, self.height, type[self.type]))
tree = []
#'idx, parent, sibling, degree, depth, height, type'
for a in range(N):
idx,left,right = map(int,input().strip().split(' '))
tree.append(node(idx, -1, left,right))
for a in range(N):
nd = tree[a]
if nd.left != -1:
tree[nd.left].parent = a
if nd.right != -1:
tree[nd.right].parent = a
tree[nd.left].sibling = nd.right
tree[nd.right].sibling = nd.left
if nd.left == nd.right :
nd.type=2
nd.degree=0
else:
nd.type=1
if nd.left == -1 or nd.right == -1:
nd.degree = 1
else:
nd.degree = 2
root = ''
for a in range(N):
if tree[a].parent == -1:
root = a
tree[root].type=0
def search(root,depth):
root = tree[root]
root.depth = depth
if root.type == 2:
return
else:
search(root.right,depth+1)
search(root.left,depth+1)
search(root,0)
def height(root,hei):
root = tree[root]
if root.type==2:
root.height=0
return hei
else:
if root.left == -1:
return height(root.right,hei+1)
elif root.right == -1:
return height(root.left,hei+1)
else:
return max(height(root.left,hei+1),height(root.right,hei+1))
for i in range(N):
tree[i].height=height(i,0)
for a in range(N):
tree[a].tostr()
| Traceback (most recent call last):
File "/tmp/tmp7wr37ej4/tmpk3321alj.py", line 1, in <module>
N = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s793673401 | p02280 | u990398499 | 1497591417 | Python | Python3 | py | Runtime Error | 30 | 7828 | 2003 | N = int(input())
M = []
type = ['root','internal node','leaf']
class node:
def __init__(self,idx,parent,left,right):
self.idx = idx
self.parent = parent
self.left = left
self.right= right
self.sibling = -1
self.degree= -1
self.depth = -1
self.height = 0
self.type=-1
def tostr(self):
print('node %d: parent = %d, sibling = %d, degree = %d, depth = %d, height = %d, %s' % (self.idx, self.parent, self.sibling, self.degree, self.depth, self.height, type[self.type]))
tree = [[] for x in range(N)]
#'idx, parent, sibling, degree, depth, height, type'
for a in range(N):
idx,left,right = map(int,input().strip().split(' '))
tree[idx] = node(idx, -1, left,right)
for a in range(N):
nd = tree[a]
if nd.left != -1:
tree[nd.left].parent = a
if nd.right != -1:
tree[nd.right].parent = a
if nd.left != -1:
tree[nd.left].sibling = nd.right
if nd.right != -1:
tree[nd.right].sibling = nd.left
if nd.left == nd.right :
nd.type=2
nd.degree=0
else:
nd.type=1
if nd.left == -1 or nd.right == -1:
nd.degree = 1
else:
nd.degree = 2
root = ''
for a in range(N):
if tree[a].parent == -1:
root = a
tree[root].type=0
def search(root,depth):
root = tree[root]
root.depth = depth
if root.type == 2:
return
else:
search(root.right,depth+1)
search(root.left,depth+1)
search(root,0)
def height(root,hei):
root = tree[root]
if root.type==2:
root.height=0
return hei
else:
if root.left == -1:
return height(root.right,hei+1)
elif root.right == -1:
return height(root.left,hei+1)
else:
return max(height(root.left,hei+1),height(root.right,hei+1))
for i in range(N):
tree[i].height=height(i,0)
for a in range(N):
tree[a].tostr() | Traceback (most recent call last):
File "/tmp/tmprp99adql/tmp7kj5qbnh.py", line 1, in <module>
N = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s021848038 | p02280 | u533883485 | 1499147510 | Python | Python3 | py | Runtime Error | 0 | 0 | 2109 | # coding=utf-8
class Node:
def __init__(self, ID, left, right):
### ?????¬?????± ###
self.id = ID
self.parent = -1 #??????
self.sibling = -1 #??????
self.degree = -1 #??????
self.depth = 0 #??????
self.height = 0 #??????
self.type = None #??????
### ?£??¶??????± ###
self.left_child = left
self.right_child = right
def calc_depth(i):
global node_list
if node_list[i].depth != 0:
return node_list[i].depth
depth = 0
if node_list[i].parent != -1:
depth = calc_depth(node_list[i].parent) + 1
node_list[i].depth = depth
return depth
def calc_height(i):
global node_list
if node_list[i].height != 0:
return node_list[i].height
h1 = h2 = 0
if node_list[i].right_child != -1:
h1 = calc_height(node_list[i].right_child) + 1
if node_list[i].left_child != -1:
h2 = calc_height(node_list[i].left_child) + 1
height = max(h1, h2)
node_list[i].height = height
return height
n = int(input())
data = [tuple(map(int, input().split())) for x in range(n)]
node_list = []
for data_row in data:
ID, left, right = data_row
node = Node(ID, left, right)
node_list.append(node)
for i, node in enumerate(node_list):
left = node.left_child
right = node.right_child
degree = 0
### ???????????± ###
if left != -1:
degree += 1
node_list[left].parent = i
node_list[left].sibling = right
if right != -1:
degree += 1
node_list[right].parent = i
node_list[right].sibling = left
### ??????????????± ###
node.degree = degree
for i in range(n):
calc_depth(i)
calc_height(i)
node = node_list[i]
if node.depth == 0:
node.type = 'root'
elif node.height == 0:
node.type = 'leaf'
else:
node.type = 'internal node'
print("node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, {}".format(node.id, node.parent, node.sibling, node.degree, node.depth, node.height, node.type)) | Traceback (most recent call last):
File "/tmp/tmp1bwbgub7/tmpb_qwq0qu.py", line 48, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s714427497 | p02280 | u491916705 | 1502419296 | Python | Python | py | Runtime Error | 0 | 0 | 1796 | class Node():
def __init__(self, id, left, right, parent=-1, sibling=-1, degree=-1, depth=-1, height=-1, type=None):
self.id = id
self.left = left
self.right = right
self.parent = parent
self.sibling = sibling
self.degree = degree
self.depth = depth
self.height = height
self.type = type
def show(self):
print 'node %d: parent = %d, sibling = %d, degree = %d, depth = %d, height = %d, %s' % (self.id, self.parent, self.sibling, self.degree, self.depth, self.height, self.type)
def hasChild(x):
if x == -1:
return 0
else:
return 1
def calcDepth(node):
if node.parent == -1:
return 0
return 1 + calcDepth(a[node.parent])
def calcHeight(node):
if node.degree == 0:
return 0
return 1 + max(calcHeight(a[node.left]), calcHeight(a[node.right]))
n = int(raw_input())
a = []
for i in range(n):
tmp = map(int, raw_input().split())
if tmp[1] == -1 and tmp[2] == -1:
a.append(Node(tmp[0], tmp[1], tmp[2], degree=0, type='leaf'))
else:
a.append(Node(tmp[0], tmp[1], tmp[2], degree=hasChild(tmp[1])+hasChild(tmp[2])))
for i in range(n):
if a[i].right != -1:
a[a[i].right].parent = a[i].id
if a[i].left != -1:
a[a[i].left].parent = a[i].id
for i in range(n):
a[i].depth = calcDepth(a[i])
a[i].height = calcHeight(a[i])
for i in range(n):
if a[i].parent != -1 and a[i].degree != 0:
a[i].type = 'internal node'
elif a[i].parent == -1 and a[i].degree > 0:
a[i].type = 'root'
for j in range(n):
if i == j:
continue
if a[i].depth == a[j].depth and a[i].parent == a[j].parent:
a[i].sibling = j
for el in a:
el.show() | File "/tmp/tmpsqmotbh1/tmptjzw5lhk.py", line 13
print 'node %d: parent = %d, sibling = %d, degree = %d, depth = %d, height = %d, %s' % (self.id, self.parent, self.sibling, self.degree, self.depth, self.height, self.type)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
| |
s258477386 | p02280 | u798803522 | 1509864011 | Python | Python3 | py | Runtime Error | 0 | 0 | 1616 | from collections import defaultdict
def dfs(here, p, d, conn, parent, depth, height):
parent[here] = p
depth[here] = d
l_d, r_d = 0, 0
for c in conn[here]:
if c[0] >= 0:
l_d = dfs(c[0], here, d + 1, conn, parent, depth, height)
if c[1] >= 0
r_d = dfs(c[1], here, d + 1, conn, parent, depth, height)
if len(conn[here]):
height[here] = max(max(l_d, r_d) - d, 0)
return max(l_d, r_d, d)
v_num = int(input())
conn = defaultdict(list)
in_v = [0 for n in range(v_num + 1)]
for _ in range(v_num):
v, left, right = (int(n) for n in input().split(" "))
conn[v].append([left, right])
in_v[left] += 1
in_v[right] += 1
degree[v] += int(left >= 0) + int(right >= 0)
for i, n in enumerate(in_v):
if not n:
root = i
break
parent = [-1 for n in range(v_num)]
depth = [0 for n in range(v_num)]
height = [0 for n in range(v_num)]
dfs(root, -1, 0, conn, parent, depth, height)
sibling = [-1 for n in range(v_num)]
degree = [0 for n in range(v_num)]
for i in range(v_num):
for j in range(v_num):
if i == j:
continue
elif parent[i] == parent[j]:
sibling[i] = j
break
v_type = ["" for n in range(v_num)]
for i in range(v_num):
if i == root:
v_type[i] = "root"
elif len(conn[i]) != 0:
v_type[i] = "internal node"
else:
v_type[i] = "leaf"
for i in range(v_num):
print("node {0}: parent = {1}, sibling = {2}, degree = {3}, depth = {4}, height = {5}, {6}".format(i, parent[i], sibling[i], degree[i], depth[i], height[i], v_type[i])) | File "/tmp/tmpeaiq36qx/tmpnl2ojr25.py", line 10
if c[1] >= 0
^
SyntaxError: expected ':'
| |
s212561522 | p02280 | u798803522 | 1509864026 | Python | Python3 | py | Runtime Error | 0 | 0 | 1617 | from collections import defaultdict
def dfs(here, p, d, conn, parent, depth, height):
parent[here] = p
depth[here] = d
l_d, r_d = 0, 0
for c in conn[here]:
if c[0] >= 0:
l_d = dfs(c[0], here, d + 1, conn, parent, depth, height)
if c[1] >= 0:
r_d = dfs(c[1], here, d + 1, conn, parent, depth, height)
if len(conn[here]):
height[here] = max(max(l_d, r_d) - d, 0)
return max(l_d, r_d, d)
v_num = int(input())
conn = defaultdict(list)
in_v = [0 for n in range(v_num + 1)]
for _ in range(v_num):
v, left, right = (int(n) for n in input().split(" "))
conn[v].append([left, right])
in_v[left] += 1
in_v[right] += 1
degree[v] += int(left >= 0) + int(right >= 0)
for i, n in enumerate(in_v):
if not n:
root = i
break
parent = [-1 for n in range(v_num)]
depth = [0 for n in range(v_num)]
height = [0 for n in range(v_num)]
dfs(root, -1, 0, conn, parent, depth, height)
sibling = [-1 for n in range(v_num)]
degree = [0 for n in range(v_num)]
for i in range(v_num):
for j in range(v_num):
if i == j:
continue
elif parent[i] == parent[j]:
sibling[i] = j
break
v_type = ["" for n in range(v_num)]
for i in range(v_num):
if i == root:
v_type[i] = "root"
elif len(conn[i]) != 0:
v_type[i] = "internal node"
else:
v_type[i] = "leaf"
for i in range(v_num):
print("node {0}: parent = {1}, sibling = {2}, degree = {3}, depth = {4}, height = {5}, {6}".format(i, parent[i], sibling[i], degree[i], depth[i], height[i], v_type[i])) | Traceback (most recent call last):
File "/tmp/tmpigclg6dc/tmpaw_l7wf9.py", line 16, in <module>
v_num = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s867666026 | p02280 | u949517845 | 1512299483 | Python | Python3 | py | Runtime Error | 0 | 0 | 2056 | # -*- coding:utf-8 -*-
import sys
def bin_tree(lst, n):
"""
tree[0]: node
tree[1]: parent
tree[2]: sibling
tree[3]: degree
tree[4]: depth
tree[5]: height
"""
info = [[-1, -1, -1, -1, -1, -1] for n in range(0, n)]
degree = 0
for node, left, right in lst:
if left > 0:
info[left][1] = node
info[left][2] = right
degree += 1
if right > 0:
info[right][1] = node
info[right][2] = left
degree += 1
info[node][0] = node
info[node][3] = degree
degree = 0
root = -1
for node in info:
root = node[0]
if node[1] == -1:
break
get_depth(lst, root, info)
get_height(lst, root, info)
print_info(info)
def get_height(tree, root, info):
def _get_height(node, depth):
if node == -1:
return 0
left_height = _get_height(tree[node][1], depth + 1)
right_height = _get_height(tree[node][2], depth + 1)
if left_height > right_height:
info[node][5] = left_height
else:
info[node][5] = right_height
return info[node][5] + 1
_get_height(root, 0)
def get_depth(tree, root, info):
def _get_depth(node, depth):
if node == -1:
return
info[node][4] = depth
_get_depth(tree[node][1], depth + 1)
_get_depth(tree[node][2], depth + 1)
_get_depth(root, 0)
def print_info(info):
for node, parent, sibling, degree, depth, height in info:
if parent == -1:
_type = "root"
elif degree != 0:
_type = "internal node"
else:
_type = "leaf"
print("node {0}: parent = {1}, sibling = {2}, degree = {3}, depth = {4}, height = {5}, {6}".format(node, parent, sibling, degree, depth, height, _type))
if __name__ == "__main__":
n = int(input())
lst = [[int(n) for n in val.split()] for val in sys.stdin.readlines()]
bin_tree(lst, n) | Traceback (most recent call last):
File "/tmp/tmphqnmkmq6/tmpgf3mhqa2.py", line 86, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s426465825 | p02280 | u426534722 | 1517581981 | Python | Python3 | py | Runtime Error | 0 | 0 | 1550 | import sys
readline = sys.stdin.readline
class Tree:
__slots__ = ['id', 'p', 'sibling', 'degree', 'depth', 'height', 'type', 'c']
def __init__(self, id):
self.id = id
self.p = -1
self.sibling = -1
self.degree = 0
self.depth = 0
self.height = 0
self.type = "leaf"
self.c = []
def __str__(self):
return f"node {self.id}: parent = {self.p}, sibling = {self.sibling}, degree = {self.degree}, depth = {self.depth}, height = {self.height}, {self.type}"
def solve():
n = int(input())
tree = tuple(Tree(i) for i in range(n))
for _ in range(n):
id, *c = map(int, readline().replace("-1", "").split())
if c != []:
tree[id].type = "internal node"
tree[id].c = c
for j in c:
tree[j].p = id
len_c = len(c)
tree[id].degree = len_c
if len_c == 2:
tree[c[0]].sibling = c[1]
tree[c[1]].sibling = c[0]
rootget = lambda x: rootget(x) if tree[x].p != -1 else x
root = rootget(0)
tree[root].type = "root"
def depth_height_check(id, d):
d += 1
height = 0
for i in tree[id].c:
tree[i].depth = d
if tree[i].type != "leaf":
tree[i].height = depth_height_check(i, d) + 1
height = max(height, tree[i].height)
return height
tree[root].height = depth_height_check(root, 0) + 1 if n > 1 else 0
print("\n".join(map(str, tree)))
solve()
| Traceback (most recent call last):
File "/tmp/tmpzgo9tm1p/tmpe44qjs_t.py", line 45, in <module>
solve()
File "/tmp/tmpzgo9tm1p/tmpe44qjs_t.py", line 17, in solve
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s058011997 | p02280 | u150984829 | 1519061517 | Python | Python3 | py | Runtime Error | 0 | 0 | 535 | def q(a,k):
d[a]=k
for b in t[a]:
if'-1'!=b:q(b,k+1)
def n(a,k):
h[a]=max(h[a],k)if a in h else k
if'-1'!=p[a]:n(p[a],k+1)
t,p,s,d,h={},{},{},{},{}
for _ in[0]*int(input()):
x,y,z=input().split()
t[x]=set([y,z])-{-1}
p[y]=p[z]=x
s[y],s[z]=z,y
r=(set(t)-set(p)).pop()
p[r]=s[r]='-1'
q(r,0)
for i in t:
if not t[i]:n(i,0)
for i in map(str,sorted(map(int,t))):
print(f"node {i}: parent = {p[i]}, sibling = {s[i]}, degree = {len(t[i])}, depth = {d[i]}, height = {h[i]}, {'root'if d[i]==0else'internal node'if t[i]else'leaf'}")
| /tmp/tmp89l01ml3/tmpyt3lzcjg.py:20: SyntaxWarning: invalid decimal literal
print(f"node {i}: parent = {p[i]}, sibling = {s[i]}, degree = {len(t[i])}, depth = {d[i]}, height = {h[i]}, {'root'if d[i]==0else'internal node'if t[i]else'leaf'}")
Traceback (most recent call last):
File "/tmp/tmp89l01ml3/tmpyt3lzcjg.py", line 9, in <module>
for _ in[0]*int(input()):
^^^^^^^
EOFError: EOF when reading a line
| |
s647724659 | p02280 | u150984829 | 1519062269 | Python | Python3 | py | Runtime Error | 0 | 0 | 531 | def q(a,k):
d[a]=k
for b in t[a]:
if'-1'!=b:q(b,k+1)
def n(a,k):
h[a]=max(h[a],k)if a in h else k
if'-1'!=p[a]:n(p[a],k+1)
t,p,s,d,h={},{},{},{},{}
n=int(input())
for _ in[0]*n:
x,y,z=input().split()
t[x]=set([y,z])-{'-1'}
p[y]=p[z]=x
s[y],s[z]=z,y
r=(set(t)-set(p)).pop()
p[r]=s[r]='-1'
q(r,0)
for i in t:
if not t[i]:n(i,0)
for i in map(str,range(n)):
print(f"node {i}: parent = {p[i]}, sibling = {s[i]}, degree = {len(t[i])}, depth = {d[i]}, height = {h[i]}, {'root'if d[i]==0else'internal node'if t[i]else'leaf'}")
| /tmp/tmpg1e37gf4/tmp6djcur1x.py:21: SyntaxWarning: invalid decimal literal
print(f"node {i}: parent = {p[i]}, sibling = {s[i]}, degree = {len(t[i])}, depth = {d[i]}, height = {h[i]}, {'root'if d[i]==0else'internal node'if t[i]else'leaf'}")
Traceback (most recent call last):
File "/tmp/tmpg1e37gf4/tmp6djcur1x.py", line 9, in <module>
n=int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s995418475 | p02280 | u150984829 | 1519062319 | Python | Python3 | py | Runtime Error | 0 | 0 | 531 | def q(a,k):
d[a]=k
for b in t[a]:
if'-1'!=b:q(b,k+1)
def n(a,k):
h[a]=max(h[a],k)if a in h else k
if'-1'!=p[a]:n(p[a],k+1)
t,p,s,d,h={},{},{},{},{}
N=int(input())
for _ in[0]*n:
x,y,z=input().split()
t[x]=set([y,z])-{'-1'}
p[y]=p[z]=x
s[y],s[z]=z,y
r=(set(t)-set(p)).pop()
p[r]=s[r]='-1'
q(r,0)
for i in t:
if not t[i]:n(i,0)
for i in map(str,range(N)):
print(f"node {i}: parent = {p[i]}, sibling = {s[i]}, degree = {len(t[i])}, depth = {d[i]}, height = {h[i]}, {'root'if d[i]==0else'internal node'if t[i]else'leaf'}")
| /tmp/tmp46pchu88/tmpqw5bfviw.py:21: SyntaxWarning: invalid decimal literal
print(f"node {i}: parent = {p[i]}, sibling = {s[i]}, degree = {len(t[i])}, depth = {d[i]}, height = {h[i]}, {'root'if d[i]==0else'internal node'if t[i]else'leaf'}")
Traceback (most recent call last):
File "/tmp/tmp46pchu88/tmpqw5bfviw.py", line 9, in <module>
N=int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s705409489 | p02280 | u150984829 | 1519064027 | Python | Python3 | py | Runtime Error | 0 | 0 | 472 | N=int(input())
t=[0]*N
r=set(range(N))
for _ in[0]*N:
x,y,z=list(map(int,input().split()))
t[x]=[y,z]+[0]*2+[(l!=-1)+(r!=-1)]+[0]*2
r-={y,z}
def f(i,p,s,d):
if i<0:return-1
l,r=t[i][:2]
t[i][2:]=[p,s,t[i][4],d,max(f(l,i,r,d+1),f(r,i,l,d+1))+1]
return t[i][-1]
f(r.pop(),-1,-1,0)
for i in range(N):
print('node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, {}'.format(i,*t[i][2:],'root'if not t[i][5]else'internal node'if t[i][4]else'leaf'))
| Traceback (most recent call last):
File "/tmp/tmpit9yiglt/tmppdi8zum_.py", line 1, in <module>
N=int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s682394814 | p02280 | u150984829 | 1519064257 | Python | Python3 | py | Runtime Error | 0 | 0 | 452 | N=int(input())
t=[0]*N
r=set(range(N))
for _ in[0]*N:
x,y,z=list(map(int,input().split()))
t[x]=[y,z,0,0,(y!=-1)+(z!=-1),0,0]
r-={y,z}
def f(i,p,s,d):
if i<0:return-1
t[i][2:]=[p,s,t[i][4],d,max(f(l,i,r,d+1),f(r,i,l,d+1))+1]
return t[i][-1]
f(r.pop(),-1,-1,0)
for i in range(N):
print('node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, {}'.format(i,*t[i][2:],'root'if not t[i][5]else'internal node'if t[i][4]else'leaf'))
| Traceback (most recent call last):
File "/tmp/tmpbrq70l3y/tmp18xtowx5.py", line 1, in <module>
N=int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s059142648 | p02280 | u150984829 | 1519064414 | Python | Python3 | py | Runtime Error | 0 | 0 | 457 | N=int(input())
t=[0]*N
r=set(range(N))
for _ in[0]*N:
e=list(map(int,input().split()))
t[e[0]]=e[1:]+[0]*5
r-={e[1:]}
def f(i,p,s,d):
if i<0:return-1
l,r=t[i][:2]
t[i][2:]=[p,s,(l!=-1)+(r!=-1),d,max(f(l,i,r,d+1),f(r,i,l,d+1))+1]
return t[i][-1]
f(r.pop(),-1,-1,0)
for i in range(N):
print('node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, {}'.format(i,*t[i][2:],'root'if not t[i][5]else'internal node'if t[i][4]else'leaf'))
| Traceback (most recent call last):
File "/tmp/tmp1v4teasr/tmpyq2kbp1w.py", line 1, in <module>
N=int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s562251809 | p02280 | u150984829 | 1519067019 | Python | Python3 | py | Runtime Error | 0 | 0 | 450 | N=int(input())
t=[0]*N
r=set(range(N))
for _ in[0]*N:
x,y,z=map(int,input().split())
t[x]=[y,z]+[0]*5
r-={y,z}
def f(i,p,s,d):
if i<0:return-1
l,r=t[i][:2]
t[i][2:5]=[p,s,(l!=-1)+(r!=-1),d]
return t[i][6]=max(f(l,i,r,d+1),f(r,i,l,d+1))+1
f(r.pop(),-1,-1,0)
for i in range(N):
print('node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, {}'.format(i,*t[i][2:],'root'if not t[i][5]else'internal node'if t[i][4]else'leaf'))
| File "/tmp/tmp3j5jitjt/tmpfp4mtmbq.py", line 12
return t[i][6]=max(f(l,i,r,d+1),f(r,i,l,d+1))+1
^
SyntaxError: invalid syntax
| |
s585807404 | p02280 | u912143677 | 1521809280 | Python | Python3 | py | Runtime Error | 0 | 0 | 957 | NIL = -1
class Node:
parent = NIL
left = NIL
right = NIL
n = int(input())
t = [Node() for i in range(n)]
for i in range(n):
a = list(map(int, input().split()))
if a[1] != -1:
t[i].left = a[1]
t[a[1]].parent = a[i]
if a[2] != -1:
t[i].right = a[2]
t[a[2]].parent = a[i]
def getdepth(u):
d = 0
while t[u].parent != NIL:
u = t[u].parent
d += 1
return d
d = []
for i in range(i):
b = getdepth(i)
d.append(b)
for i in range(n):
c = 0
if t[i].left != NIL:
c += 1
if t[i].right != NIL:
c += 1
if t[i].parent == NIL:
cond = "root"
elif t[i].left == NIL and t[i].right == NIL:
cond = "leaf"
else:
cond = "internal node"
print("node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, {}"
.format(i, t[i].parent, d.count(d[i]), c, d[i], max(d)-d[i], cond))
| Traceback (most recent call last):
File "/tmp/tmpt4qfe9tu/tmpmjxiu8pm.py", line 8, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s783534430 | p02280 | u938045879 | 1527781479 | Python | Python3 | py | Runtime Error | 0 | 0 | 1290 | n = int(input())
root = set(range(n))
binary_tree = []
out = [0 for i in range(n)]
for i in range(n):
l = list(map(int, input().split()))
binary_tree.append(l)
s = set(l[1:3])
root -= s
def binary(id, parent, depth, sibling, height):
output = {}
hline = []
hline.append(height)
output['node'] = id
output['parent'] = parent
output['sibling'] = sibling
output['depth'] = depth
left = binary_tree[id][1]
right = binary_tree[id][2]
degree = 0
if(parent == -1):
output['type'] = 'root'
elif(left == -1 and right == -1):
output['type'] = 'leaf'
else:
output['type'] = 'internal node'
if(left != -1):
degree += 1
hline.append(binary(left, id, depth+1, right, height))
if(right != -1):
degree += 1
hline.append(binary(right, id, depth+1, left, height))
max_height = max(hline)
output['degree'] = degree
output['height'] = max_height
out[output['node']] = output
return max_height + 1
binary(list(root)[0], -1, 0, -1, 0)
for line in out:
print('node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, {}'.format(line['node'], line['parent'], line['sibling'], line['degree'], line['depth'], line['height'], line['type']))
| Traceback (most recent call last):
File "/tmp/tmpyta90ttg/tmpl0_la7mh.py", line 1, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s636709470 | p02280 | u464859367 | 1528538118 | Python | Python3 | py | Runtime Error | 0 | 0 | 3949 | using System;
using System.Collections.Generic;
using System.Text;
namespace ALDS1_7_B
{
class Node
{
public int Parent { get; set; }
public int Sibling { get; set; }
public int Degree { get; set; }
public int Depth { get; set; }
public int Hight { get; set; }
public string Type { get; set; }
public List<int> Children { get; set; }
public Node()
{
Parent = -1;
Sibling = -1;
Degree = 0;
Depth = -1;
Hight = -1;
Type = "";
Children = new List<int>();
}
}
class Program
{
static void Main(string[] args)
{
int i;
var n = int.Parse(Console.ReadLine());
var Nodes = new Node[n];
for (i = 0; i < n; i++) Nodes[i] = new Node();
for (i = 0; i < n; i++)
{
int[] input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
int parent = input[0], left = input[1], right = input[2];
if (left == -1 && right == -1)
{
Nodes[parent].Type = "leaf";
Nodes[parent].Hight = 0;
}
else if (left == -1 || right == -1)
{
var child = (left > -1) ? left : right;
Nodes[parent].Children.Add(child);
Nodes[child].Parent = parent;
Nodes[parent].Degree = 1;
}
else
{
Nodes[parent].Children.Add(right);
Nodes[parent].Children.Add(left);
Nodes[left].Parent = parent;
Nodes[left].Sibling = right;
Nodes[right].Parent = parent;
Nodes[right].Sibling = left;
Nodes[parent].Degree = 2;
}
}
int rootIndex = 0;
for (i = 0; i < n; i++)
{
if (Nodes[i].Parent == -1)
{
Nodes[i].Type = "root";
Nodes[i].Depth = 0;
rootIndex = i;
}
else if (Nodes[i].Type != "leaf")
{
Nodes[i].Type = "internal node";
}
}
var stk = new Stack<Node>();
var MaxDepth = 0;
stk.Push(Nodes[rootIndex]);
while (stk.Count > 0)
{
Node temp = stk.Pop();
foreach (var child in temp.Children)
{
Nodes[child].Depth = temp.Depth + 1;
MaxDepth = (Nodes[child].Depth > MaxDepth) ? Nodes[child].Depth : MaxDepth;
if (Nodes[child].Children.Count > 0)
{
stk.Push(Nodes[child]);
}
}
}
for (i = 0; i < n; i++)
{
if (Nodes[i].Type == "leaf")
{
continue;
}
foreach (var child in Nodes[i].Children)
{
if (Nodes[child].Type == "leaf")
{
continue;
}
}
Nodes[i].Hight = MaxDepth - Nodes[i].Depth;
}
StringBuilder sb = new StringBuilder();
for (i = 0; i < n; i++)
{
sb.AppendLine("node " + i + ": parent = " + Nodes[i].Parent + ", sibling = " + Nodes[i].Sibling + ", degree = " +
Nodes[i].Degree + ", depth = " + Nodes[i].Depth + ", height = " + Nodes[i].Hight + ", " + Nodes[i].Type);
}
Console.Write(sb);
Console.ReadLine();
}
}
}
| File "/tmp/tmpkt0ktqyp/tmpihutbacb.py", line 1
using System;
^^^^^^
SyntaxError: invalid syntax
| |
s492157254 | p02280 | u464859367 | 1528590849 | Python | Python3 | py | Runtime Error | 0 | 0 | 4352 | using System;
using System.Collections.Generic;
using System.Text;
namespace ALDS1_7_B
{
class Node
{
public int Parent { get; set; }
public int Sibling { get; set; }
public int Degree { get; set; }
public int Depth { get; set; }
public int Hight { get; set; }
public string Type { get; set; }
public List<int> Children { get; set; }
public Node()
{
Parent = -1;
Sibling = -1;
Degree = 0;
Depth = -1;
Hight = -1;
Type = "";
Children = new List<int>();
}
}
class Program
{
static void Main(string[] args)
{
int i;
var n = int.Parse(Console.ReadLine());
var Nodes = new Node[n];
for (i = 0; i < n; i++) Nodes[i] = new Node();
for (i = 0; i < n; i++)
{
int[] input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
int parent = input[0], left = input[1], right = input[2];
if (left == -1 && right == -1)
{
Nodes[parent].Type = "leaf";
Nodes[parent].Hight = 0;
}
else if (left == -1 || right == -1)
{
var child = (left > -1) ? left : right;
Nodes[parent].Children.Add(child);
Nodes[child].Parent = parent;
Nodes[parent].Degree = 1;
}
else
{
Nodes[parent].Children.Add(right);
Nodes[parent].Children.Add(left);
Nodes[left].Parent = parent;
Nodes[left].Sibling = right;
Nodes[right].Parent = parent;
Nodes[right].Sibling = left;
Nodes[parent].Degree = 2;
}
}
int rootIndex = 0;
for (i = 0; i < n; i++)
{
if (Nodes[i].Parent == -1)
{
Nodes[i].Type = "root";
Nodes[i].Depth = 0;
rootIndex = i;
}
else if (Nodes[i].Type != "leaf")
{
Nodes[i].Type = "internal node";
}
}
var stk = new Stack<Node>();
var MaxDepth = 0;
stk.Push(Nodes[rootIndex]);
while (stk.Count > 0)
{
Node temp = stk.Pop();
foreach (var child in temp.Children)
{
Nodes[child].Depth = temp.Depth + 1;
MaxDepth = (Nodes[child].Depth > MaxDepth) ? Nodes[child].Depth : MaxDepth;
if (Nodes[child].Children.Count > 0)
{
stk.Push(Nodes[child]);
}
}
}
var ParentPushflag = true;
stk.Push(Nodes[rootIndex]);
while (stk.Count > 0)
{
var node = stk.Pop();
foreach (var child in node.Children)
{
if (Nodes[child].Hight == -1)
{
if (ParentPushflag)
{
stk.Push(node);
ParentPushflag = false;
}
stk.Push(Nodes[child]);
}
else
{
node.Hight = (Nodes[child].Hight + 1 > node.Hight) ? Nodes[child].Hight + 1 : node.Hight;
}
}
ParentPushflag = true;
}
StringBuilder sb = new StringBuilder();
for (i = 0; i < n; i++)
{
sb.AppendLine("node " + i + ": parent = " + Nodes[i].Parent + ", sibling = " + Nodes[i].Sibling + ", degree = " +
Nodes[i].Degree + ", depth = " + Nodes[i].Depth + ", height = " + Nodes[i].Hight + ", " + Nodes[i].Type);
}
Console.Write(sb);
Console.ReadLine();
}
}
}
| File "/tmp/tmpoovejqw7/tmp4zc40sz2.py", line 1
using System;
^^^^^^
SyntaxError: invalid syntax
| |
s366517107 | p02280 | u782850731 | 1380149075 | Python | Python | py | Runtime Error | 0 | 0 | 1559 | #!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin
FORMAT = ('node {0.id}: parent = {0.parent}, sibling = {0.sibling}, '
'degree = {0.degree}, depth = {0.depth}, height = {0.height}, '
'{0.type}').format
class Node(object):
def __init__(self, data):
self.id = data[0]
self.parent = -1
self.sibling = -1
self.degree = int(data[1] > 0) + int(data[2] > 0)
self.childs = [i for i in data[1:] if i > 0]
self.depth = 0
self.height = -1
self.type = 'internal node' if self.degree else 'leaf'
def __str__(self):
return FORMAT(self)
def calc_depth(tree, i):
h = 0
for k, j in enumerate(tree[i].childs):
tree[j].depth += 1
tree[j].sibling = tree[i].childs[1-k]
h = max(h, calc_depth(tree, j))
if tree[i].type == 'leaf':
tree[i].height = 0
else:
tree[i].height = h
return tree[i].height + 1
n = int(stdin.readline())
tree = [-1] * n
for _ in range(n):
node = Node([int(s) for s in stdin.readline().split()])
parent = tree[node.id]
tree[node.id] = node
if isinstance(parent, int):
tree[node.id].parent = parent
else:
tree[node.id].parent = parent.id
for i in node.childs:
if isinstance(tree[i], int):
tree[i] = node.id
else:
tree[i].parent = node.id
for i in range(n):
calc_depth(tree, i)
for node in tree:
if node.parent == -1:
node.type = 'root'
print(node) | Traceback (most recent call last):
File "/tmp/tmp18p454eq/tmprzekx8xu.py", line 38, in <module>
n = int(stdin.readline())
^^^^^^^^^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: ''
| |
s816485146 | p02281 | u805716376 | 1556796546 | Python | Python3 | py | Runtime Error | 0 | 0 | 409 | def s(x):
print('',x,end='')
def p(u):
if u+1:s(u);p(R[u]);p(L(u))
def i(u):
if u+1:i(R(u));s(u);p(L(u))
def o(u):
if u+1:o(R(u));o(L(u));s(u)
n = int(inpout())
R, L = [0]*n, [0]*n
for _ in range(n):
a, b, c = map(int, input().split())
R[a] = b
L[a] = c
root = (set(range(n))-set(R)-set(L)).pop()
print(f'Preorder{p(r)}')
print(f'Inorder{i(r)}')
print(f'Postorder{o(r)}')
| Traceback (most recent call last):
File "/tmp/tmpz288q20_/tmpi_4cts5j.py", line 10, in <module>
n = int(inpout())
^^^^^^
NameError: name 'inpout' is not defined. Did you mean: 'input'?
| |
s411609854 | p02281 | u805716376 | 1556796577 | Python | Python3 | py | Runtime Error | 0 | 0 | 408 | def s(x):
print('',x,end='')
def p(u):
if u+1:s(u);p(R[u]);p(L(u))
def i(u):
if u+1:i(R(u));s(u);p(L(u))
def o(u):
if u+1:o(R(u));o(L(u));s(u)
n = int(input())
R, L = [0]*n, [0]*n
for _ in range(n):
a, b, c = map(int, input().split())
R[a] = b
L[a] = c
root = (set(range(n))-set(R)-set(L)).pop()
print(f'Preorder{p(r)}')
print(f'Inorder{i(r)}')
print(f'Postorder{o(r)}')
| Traceback (most recent call last):
File "/tmp/tmp_l573z5b/tmpu8iuzot8.py", line 10, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s751876698 | p02281 | u805716376 | 1556796659 | Python | Python3 | py | Runtime Error | 0 | 0 | 417 | def s(x):
print('',x,end='')
def p(u):
if u+1:s(u);p(R[u]);p(L(u))
def i(u):
if u+1:i(R(u));s(u);p(L(u))
def o(u):
if u+1:o(R(u));o(L(u));s(u)
n = int(input())
R, L = [0]*n, [0]*n
for _ in range(n):
a, b, c = map(int, input().split())
R[a] = b
L[a] = c
root = (set(range(n))-set(R)-set(L)).pop()
print(f'Preorder{p(root)}')
print(f'Inorder{i(root)}')
print(f'Postorder{o(root)}')
| Traceback (most recent call last):
File "/tmp/tmpbqlsbop4/tmp7gl99xyl.py", line 10, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s418716751 | p02281 | u805716376 | 1556797454 | Python | Python3 | py | Runtime Error | 0 | 0 | 462 | def s(x):
print('',x,end='')
def p(u):
if u+1:s(u);p(R[u]);p(R(u+n))
def i(u):
if u+1:i(R(u));s(u);p(L(u))
def o(u):
if u+1:o(R(u));o(L(u));s(u)
n = int(input())
R, L = [0]*2*n, [0]*n
for _ in range(n):
a, b, c = map(int, input().split())
R[a], R[a+n] = b,c
L[a] = c
root = (set(range(n))-set(R)-set(L)).pop()
print('Preorder',end = '');p(root)
print('\nInorder',end = '');i(root)
print('\nPostorder',end='');o(root)
print()
| Traceback (most recent call last):
File "/tmp/tmpbq_r0ps0/tmptv0u5vom.py", line 10, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s435365940 | p02281 | u805716376 | 1556797797 | Python | Python3 | py | Runtime Error | 0 | 0 | 417 | def s(x):
print('',x,end='')
def p(u):
if u+1:s(u);p(R[u]);p(L(u))
def i(u):
if u+1:i(R(u));s(u);p(L(u))
def o(u):
if u+1:o(R(u));o(L(u));s(u)
n = int(input())
R, L = [0]*n, [0]*n
for _ in range(n):
a, b, c = map(int, input().split())
R[a] = b
L[a] = c
root = (set(range(n))-set(R)-set(L)).pop()
print(f'Preorder{p(root)}')
print(f'Inorder{i(root)}')
print(f'Postorder{o(root)}')
| Traceback (most recent call last):
File "/tmp/tmpuvw0rzmh/tmpg7zpjvtc.py", line 10, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s962112263 | p02281 | u882992966 | 1442182266 | Python | Python3 | py | Runtime Error | 30 | 7788 | 1913 | import sys
class Node(object):
def __init__(self, parent=-1, left=-1, right=-1):
self.parent = parent
self.left = left
self.right = right
def __str__(self):
return "parent = %s, left = %s, right = %s" % (self.parent, self.left, self.right)
def __repr__(self):
return self.__str__()
def preorder_traverse(node_list, current):
print(" %d" % current, end="")
node = node_list[current]
if node.left != -1:
preorder_traverse(node_list, node.left)
if node.right != -1:
preorder_traverse(node_list, node.right)
def inorder_traverse(node_list, current):
node = node_list[current]
if node.left != -1:
inorder_traverse(node_list, node.left)
print(" %d" % current, end="")
if node.right != -1:
inorder_traverse(node_list, node.right)
def postorder_traverse(node_list, current):
node = node_list[current]
if node.left != -1:
postorder_traverse(node_list, node.left)
if node.right != -1:
postorder_traverse(node_list, node.right)
print(" %d" % current, end="")
def main():
lines = sys.stdin.readlines()
num_of_nodes = int(lines[0])
node_list = [Node() for _ in range(num_of_nodes)]
# Set filiation
for line in lines[1:]:
node_id, left, right = [int(x) for x in line.strip().split(" ")]
node_list[node_id].left = left
node_list[node_id].right = right
node_list[left].parent = node_id
node_list[right].parent = node_id
# Find root node id
for node_id, node in enumerate(node_list):
if node.parent == -1:
root = node_id
break
print("Preorder")
preorder_traverse(node_list, root)
print("\nInorder")
inorder_traverse(node_list, root)
print("\nPostorder")
postorder_traverse(node_list, root)
print("")
if __name__ == "__main__":
main() | Traceback (most recent call last):
File "/tmp/tmpdd1pjmdb/tmpkj4tl2sq.py", line 73, in <module>
main()
File "/tmp/tmpdd1pjmdb/tmpkj4tl2sq.py", line 46, in main
num_of_nodes = int(lines[0])
~~~~~^^^
IndexError: list index out of range
| |
s747129873 | p02281 | u519227872 | 1502565327 | Python | Python3 | py | Runtime Error | 0 | 0 | 1033 | n = int(input())
class Node(object):
def __init__(self, p, l, r):
self.p = p
self.l = l
self.r = r
nodes = [Node(None, None, None) for i in range(n)]
for i in range(n):
id, left, right = map(int, input().split())
nodes[i].l = left
nodes[i].r = right
if left != -1:
nodes[left].p = i
if right != -1:
nodes[right].p = i
def pre(node):
print(node, end=" ")
if nodes[node].l != -1:
pre(nodes[node].l)
if nodes[node].r != -1:
pre(nodes[node].r)
def ino(node):
if nodes[node].l != -1:
ino(nodes[node].l)
print(node, end=" ")
if nodes[node].r != -1:
ino(nodes[node].r)
def post(node):
if nodes[node].l != -1:
post(nodes[node].l)
if nodes[node].r != -1:
post(nodes[node].r)
print(node, end=" ")
for ind, n in enumerate(nodes):
if n.p != -1:
root = ind
break
print('Preorder')
pre(root)
print('\n')
print('InOrder')
ino(root)
print('\n')
print('Postorder')
post(root) | Traceback (most recent call last):
File "/tmp/tmp_wwu030x/tmprsqhdjtg.py", line 1, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s204182064 | p02281 | u798803522 | 1509811714 | Python | Python3 | py | Runtime Error | 0 | 0 | 1190 | from collections import defaultdict
def preorder(here, conn, chain):
if here == -1:
return
chain.append(here)
if conn[here]:
preorder(conn[here][0], conn, chain)
preorder(conn[here][1], conn, chain)
def inorder(here, conn, chain):
if here == -1:
return
if conn[here]:
inorder(conn[here][0], conn, chain)
chain.append(here)
inorder(conn[here][1], conn, chain)
def postorder(here, conn, chain):
if here == -1:
return
if conn[here]:
postorder(conn[here][0], conn, chain)
postorder(conn[here][1], conn, chain)
chain.append(here)
query = int(input())
connect = defaultdict(list)
in_v = [0 for n in range(query + 1)]
for _ in range(query):
here, left, right = (int(n) for n in input().split(" "))
connect[here] = [left, right]
in_v[left] += 1
in_v[right] += 1
for i in range(query):
if not in_v[i]:
root = i
break
preo = []
ino = []
posto = []
preorder(root, connect, preo)
inorder(root, connect, ino)
postorder(root, connect, posto)
print("Preorder")
print(" " + *preo)
print("Inorder")
print(" " + *ino)
print("Postorder")
print(" " + *posto) | File "/tmp/tmp1rfg0hrp/tmp7neg_5z6.py", line 46
print(" " + *preo)
^
SyntaxError: invalid syntax
| |
s763315419 | p02281 | u845643816 | 1511260330 | Python | Python3 | py | Runtime Error | 0 | 0 | 746 | N = int(input())
left = [-1 for i in range(N)]
right = [-1 for i in range(N)]
root = sum(range(N))
for i in range(N):
i, l, r = map(int, input().split())
left[i] = l
right[i] = r
root -= (l + r)
def preorder(i):
if i == -1:
return
print(' ', i, sep = '')
preorder(left[i])
preorder(right[i])
def inorder(i):
if i == -1:
return
inorder(left[i])
print(' ', i, sep = '')
inorder(right[i])
def postorder(i):
if i == -1:
return
(l, r) = tree[i]
postorder(left[i])
postorder(right[i])
print(' ', i, sep = '')
print('Preorder')
preorder(root)
print()
print('Inorder')
inorder(root)
print()
print('Postorder')
postorder(root)
print() | Traceback (most recent call last):
File "/tmp/tmpfnnhxqof/tmpkw5e0wgw.py", line 1, in <module>
N = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s557881967 | p02281 | u845643816 | 1511260741 | Python | Python3 | py | Runtime Error | 0 | 0 | 754 | N = int(input())
left = [-1 for i in range(N)]
right = [-1 for i in range(N)]
root = sum(range(N)) - N - 1
for i in range(N):
i, l, r = map(int, input().split())
left[i] = l
right[i] = r
root -= (l + r)
def preorder(i):
if i == -1:
return
print(' ', i, sep = '')
preorder(left[i])
preorder(right[i])
def inorder(i):
if i == -1:
return
inorder(left[i])
print(' ', i, sep = '')
inorder(right[i])
def postorder(i):
if i == -1:
return
(l, r) = tree[i]
postorder(left[i])
postorder(right[i])
print(' ', i, sep = '')
print('Preorder')
preorder(root)
print()
print('Inorder')
inorder(root)
print()
print('Postorder')
postorder(root)
print() | Traceback (most recent call last):
File "/tmp/tmpm12hgoq4/tmp8zfznq56.py", line 1, in <module>
N = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s303939197 | p02281 | u845643816 | 1511260854 | Python | Python3 | py | Runtime Error | 0 | 0 | 784 | N = int(input())
left = [-1 for i in range(N)]
right = [-1 for i in range(N)]
root = sum(range(N)) - N - 1
for i in range(N):
i, l, r = map(int, input().split())
left[i] = l
right[i] = r
root -= (l + r)
def preorder(i):
if i == -1:
return
print(' ', i, sep = '', end = '')
preorder(left[i])
preorder(right[i])
def inorder(i):
if i == -1:
return
inorder(left[i])
print(' ', i, sep = '', end = '')
inorder(right[i])
def postorder(i):
if i == -1:
return
(l, r) = tree[i]
postorder(left[i])
postorder(right[i])
print(' ', i, sep = '', end = '')
print('Preorder')
preorder(root)
print()
print('Inorder')
inorder(root)
print()
print('Postorder')
postorder(root)
print() | Traceback (most recent call last):
File "/tmp/tmpud2rabhz/tmp7ti9sh0_.py", line 1, in <module>
N = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s671952712 | p02281 | u626266743 | 1511267747 | Python | Python3 | py | Runtime Error | 0 | 0 | 689 | n = int(input())
tree = [None for i in range(n)]
root = set(range(n))
for i in range(n):
i, l, r = map(int, input().split())
left[i] = l
right[i] = r
root -= {1, r}
def preorder(i):
if i == -1:
return
print(' ', i, sep = '')
preorder(left[i])
preorder(right[i])
def inorder(i):
if i == -1:
return
inorder(left[i])
print(' ', i, sep = '')
inorder(right[i])
def postorder(i):
if i == -1:
return
postorder(left[i])
postorder(right[i])
print(' ', i, sep = '')
print('Preorder')
preorder(root)
print()
print('Inorder')
inorder(root)
print()
print('Postorder')
postorder(root)
print() | Traceback (most recent call last):
File "/tmp/tmp2bq4rome/tmp1bx9sql4.py", line 1, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
| |
s632311170 | p02281 | u626266743 | 1511267779 | Python | Python3 | py | Runtime Error | 0 | 0 | 719 | n = int(input())
tree = [None for i in range(n)]
root = set(range(n))
for i in range(n):
i, l, r = map(int, input().split())
left[i] = l
right[i] = r
root -= {1, r}
def preorder(i):
if i == -1:
return
print(' ', i, sep = '', end = '')
preorder(left[i])
preorder(right[i])
def inorder(i):
if i == -1:
return
inorder(left[i])
print(' ', i, sep = '', end = '')
inorder(right[i])
def postorder(i):
if i == -1:
return
postorder(left[i])
postorder(right[i])
print(' ', i, sep = '', end = '')
print('Preorder')
preorder(root)
print()
print('Inorder')
inorder(root)
print()
print('Postorder')
postorder(root)
print() | Traceback (most recent call last):
File "/tmp/tmpjdg6opow/tmp4w5l7ul1.py", line 1, in <module>
n = int(input())
^^^^^^^
EOFError: EOF when reading a line
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.