Problem p03044 (python)

Fixed submission:
import sys
 
sys.setrecursionlimit(10**5)

n = int(input())
p = [list() for _ in range(n)]
for i in range(n-1):
    u, v, w = [int(s) for s in input().split()]
    u -= 1
    v -= 1
    w %= 2
    p[u].append((v, w))
    p[v].append((u, w))

costs = [-1 for _ in range(n)]

def dfs(cur, dist):
    if costs[cur] != -1:
        return
    costs[cur] = dist
    for v, d in p[cur]:
        dfs(v, dist^d)
dfs(0, 0)

for i in range(n):
    print(costs[i])

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3.4
# -*- coding: utf-8 -*-

# abc126_d

from collections import namedtuple

DistFrom = namedtuple('DistFrom', ['f', 'dist'])

def main():
    # Input
    n, = [int(s) for s in input().split()]
    fs = [[int(s) for s in input().split()] for _ in range(n-1)]
    # Get ans
    gs = [None] * n
    for i in range(n):
        gs[i] = list()
    for x, y, d in fs:
        x -= 1
        y -= 1
        d = d%2
        gs[x].append(DistFrom(y, d))
        gs[y].append(DistFrom(x, d))
    distlist = [None] * n
    def dfs(cur, dist):
        if distlist[cur] != None:
            return
        distlist[cur] = dist
        for i, d in gs[cur]:
            dfs(i, dist^d)
    dfs(0, 0)
    # Output
    for i in range(n):
        print(distlist[i])

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):


n = int(input())
p = [list() for _ in range(n)]
for i in range(n-1):
    u, v, w = [int(s) for s in input().split()]
    u -= 1
    v -= 1
    w %= 2
    p[u].append((v, w))
    p[v].append((u, w))

costs = [-1 for _ in range(n)]

def dfs(cur, dist):
    if costs[cur] != -1:
        return
    costs[cur] = dist
    for v, d in p[cur]:
        dfs(v, dist^d)
dfs(0, 0)

for c in costs:
    print(c)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3.4
# -*- coding: utf-8 -*-

# abc126_d

from collections import namedtuple

DistFrom = namedtuple('DistFrom', ['f', 'dist'])

def main():
    # Input
    n, = [int(s) for s in input().split()]
    #if n==1: return
    fs = [[int(s) for s in input().split()] for _ in range(n-1)]
    # Get ans
    gs = [None] * n
    for i in range(n):
        gs[i] = list()
    for x, y, d in fs:
        x -= 1
        y -= 1
        d = d%2
        gs[x].append(DistFrom(y, d))
        gs[y].append(DistFrom(x, d))
    distlist = [None] * n
    def dfs(cur, dist):
        if distlist[cur] != None:
            return
        distlist[cur] = dist
        for i, d in gs[cur]:
            dfs(i, dist^d)
    dfs(0, 0)
    ans = distlist
    # Output
    for a in ans:
        print(a)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3.4
# -*- coding: utf-8 -*-

# abc126_d

from collections import namedtuple

DistFrom = namedtuple('DistFrom', ['f', 'dist'])

def main():
    # input
    n, = [int(s) for s in input().split()]
    #if n==1: return
    fs = [[int(s) for s in input().split()] for _ in range(n-1)]
    # getans
    gs = [None] * n
    for i in range(n):
        gs[i] = list()
    for x, y, d in fs:
        x -= 1
        y -= 1
        d = d%2
        gs[x].append(DistFrom(y, d))
        gs[y].append(DistFrom(x, d))
    distlist = [None] * n
    def search(cur, prev, dist):
        #distlist[cur] != None: return
        distlist[cur] = dist
        for i, d in gs[cur]:
            if i != prev:
                search(i, cur, dist^d)
    distlist[0] = 0
    for i, d in gs[0]:
        search(i, 0, d)
    ans = distlist
    # output
    for a in ans:
        print(a)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3.4
# -*- coding: utf-8 -*-

# abc126_d

from collections import namedtuple

DistFrom = namedtuple('DistFrom', ['f', 'dist'])

def main():
    # input
    n, = [int(s) for s in input().split()]
    #if n==1: return
    fs = [[int(s) for s in input().split()] for _ in range(n-1)]
    # getans
    gs = [None] * n
    for i in range(n):
        gs[i] = list()
    for x, y, d in fs:
        x -= 1
        y -= 1
        gs[x].append(DistFrom(y, d))
        gs[y].append(DistFrom(x, d))
    distlist = [None] * n
    def search(cur, prev, dist):
        #distlist[cur] != None: return
        distlist[cur] = dist
        for i, d in gs[cur]:
            if i != prev:
                search(i, cur, dist+d)
    distlist[0] = 0
    for i, d in gs[0]:
        search(i, 0, d)
    ans = [d%2 for d in distlist]
    # output
    for a in ans:
        print(a)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3.4
# -*- coding: utf-8 -*-

# abc126_d

from collections import namedtuple

DistFrom = namedtuple('DistFrom', ['f', 'dist'])

def main():
    # input
    n, = [int(s) for s in input().split()]
    fs = [[int(s) for s in input().split()] for _ in range(n-1)]
    # getans
    getters = [None] * n
    for i in range(n):
        getters[i] = list()
    for x, y, d in fs:
        x -= 1
        y -= 1
        getters[x].append(DistFrom(y, d))
        getters[y].append(DistFrom(x, d))
    distlist = [None] * n
    def search(cur_i, prev, dist):
        if distlist[cur_i] != None: return  # TODO delete
        distlist[cur_i] = dist
        for i, d in getters[cur_i]:
            if i != prev:
                search(i, cur_i, dist+d)
    distlist[0] = 0
    for i, d in getters[0]:
        search(i, 0, d)
    ans = [d%2 for d in distlist]
    # output
    for a in ans:
        print(a)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3.4
# -*- coding: utf-8 -*-

# abc126_d

from collections import namedtuple

DistFrom = namedtuple('DistFrom', ['f', 'dist'])

def main():
    # input
    n, = [int(s) for s in input().split()]
    fs = [[int(s) for s in input().split()] for _ in range(n-1)]
    # getans
    getters = [None] * n
    for i in range(n):
        getters[i] = list()
    for x, y, d in fs:
        x -= 1
        y -= 1
        getters[x].append((y, d))
        getters[y].append((x, d))
    distlist = [None] * n
    def search(cur_i, prev, dist):
        distlist[cur_i] = dist
        for i, d in getters[cur_i]:
            if i != prev:
                search(i, cur_i, dist+d)
    distlist[0] = 0
    for i, d in getters[0]:
        search(i, 0, d)
    ans = [d%2 for d in distlist]
    # output
    for a in ans:
        print(a)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3.4
# -*- coding: utf-8 -*-

# abc126_d

from collections import namedtuple

DistFrom = namedtuple('DistFrom', ['f', 'dist'])

def main():
    # input
    n, = [int(s) for s in input().split()]
    fs = [[int(s) for s in input().split()] for _ in range(n-1)]
    # getans
    getters = [None] * n
    for i in range(n):
        getters[i] = list()
    for x, y, d in fs:
        x -= 1
        y -= 1
        getters[x].append(DistFrom(y, d))
        getters[y].append(DistFrom(x, d))
    dists = [-1] * n
    def search(cur_i, prev, dist):
        dists[cur_i] = dist
        for i, d in getters[cur_i]:
            if i == prev: continue
            search(i, cur_i, dist+d)
    dists[0] = 0
    for i, d in getters[0]:
        search(i, 0, d)
    ans = [d%2 for d in dists]
    # output
    for a in ans:
        print(a)

if __name__ == '__main__':
    main()