Problem p03044 (python)

Fixed submission:
import sys
if sys.version_info[0] == 2:
    input = raw_input
sys.setrecursionlimit(2 ** 31 -1)

res = None
G = None

def dfs(jibun_v, p, jibun_c):
    global res
    global G

    res[jibun_v] = jibun_c
    #print(jibun_v, p, jibun_c)

    tonari_nodes = G[jibun_v]
    for tonari_v, w in tonari_nodes:
        #if tonari_v == p: # only no loop
        #if tonari_v == p or res[tonari_v] != -1:
        if res[tonari_v] != -1:
            continue

        else:
            if w % 2 == 0:
                tonari_c = jibun_c
            else:
                tonari_c = 1 - jibun_c

            dfs(tonari_v, jibun_v, tonari_c)
    

def main():
    N= int(input())

    global G
    G = [[] for _ in range(N)]

    for i in range(N-1):
        u, v, w = map(int, input().split(" "))
        u -= 1
        v -= 1
        w = w % 2
        G[u].append([v, w])
        G[v].append([u, w])

    global res
    res = [-1 for _ in range(N)]
    
    jibun_v = 0
    p = -1
    jibun_c = 0
    dfs(jibun_v, p, jibun_c)

    for i in range(N):
        print(res[i])

main()

Buggy submission (verdict: Runtime Error):
import sys
if sys.version_info[0] == 2:
    input = raw_input
#sys.setrecursionlimit(2 ** 31 -1)

res = None
G = None

def dfs(jibun_v, p, jibun_c):
    global res
    global G

    res[jibun_v] = jibun_c
    #print(jibun_v, p, jibun_c)

    tonari_nodes = G[jibun_v]
    for tonari_v, w in tonari_nodes:
        #if tonari_v == p: # only no loop
        #if tonari_v == p or res[tonari_v] != -1:
        if res[tonari_v] != -1:
            continue

        else:
            if w % 2 == 0:
                tonari_c = jibun_c
            else:
                tonari_c = 1 - jibun_c

            dfs(tonari_v, jibun_v, tonari_c)
    

def main():
    N= int(input())

    global G
    G = [[] for _ in range(N)]

    for i in range(N-1):
        u, v, w = map(int, input().split(" "))
        u -= 1
        v -= 1
        w = w % 2
        G[u].append([v, w])
        G[v].append([u, w])

    global res
    res = [-1 for _ in range(N)]
    
    jibun_v = 0
    p = -1
    jibun_c = 0
    dfs(jibun_v, p, jibun_c)

    for i in range(N):
        print(res[i])

main()

Buggy submission (verdict: Runtime Error):
import sys
if sys.version_info[0] == 2:
    input = raw_input
sys.setrecursionlimit(2 ** 31 -1)

res = none
g = none

def dfs(jibun_v, p, jibun_c):
    global res
    global g

    res[jibun_v] = jibun_c
    #print(jibun_v, p, jibun_c)

    tonari_nodes = g[jibun_v]
    for tonari_v, w in tonari_nodes:
        #if tonari_v == p: # only no loop
        #if tonari_v == p or res[tonari_v] != -1:
        if res[tonari_v] != -1:
            continue

        else:
            if w % 2 == 0:
                tonari_c = jibun_c
            else:
                tonari_c = 1 - jibun_c

            dfs(tonari_v, jibun_v, tonari_c)
    

def main():
    n= int(input())

    global g
    g = [[] for _ in range(n)]

    for i in range(n-1):
        u, v, w = map(int, input().split(" "))
        u -= 1
        v -= 1
        w = w % 2
        g[u].append([v, w])
        g[v].append([u, w])

    global res
    res = [-1 for _ in range(n)]
    
    jibun_v = 0
    p = -1
    jibun_c = 0
    dfs(jibun_v, p, jibun_c)

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

main()

Buggy submission (verdict: Runtime Error):
3
1 2 2
2 3 1

Buggy submission (verdict: Runtime Error):
import sys
if sys.version_info[0] == 2:
    input = raw_input

res = None
G = None

def dfs(jibun_v, p, jibun_c):
    global res
    global G

    res[jibun_v] = jibun_c
    #print(jibun_v, p, jibun_c)

    tonari_nodes = G[jibun_v]
    for tonari_v, w in tonari_nodes:
        if tonari_v == p:
        #if tonari_v == p or res[tonari_v] != -1:
        #if res[tonari_v] != -1:
            continue

        else:
            if w % 2 == 0:
                tonari_c = jibun_c
            else:
                tonari_c = 1 - jibun_c

            dfs(tonari_v, jibun_v, tonari_c)
    

def main():
    N= int(input())

    global G
    G = [[] for _ in range(N)]

    for i in range(N-1):
        u, v, w = map(int, input().split(" "))
        u -= 1
        v -= 1
        w = w % 2
        G[u].append([v, w])
        G[v].append([u, w])

    global res
    res = [-1 for _ in range(N)]
    
    jibun_v = 0
    p = -1
    jibun_c = 0
    dfs(jibun_v, p, jibun_c)

    for i in range(N):
        print(res[i])

main()

Buggy submission (verdict: Runtime Error):
import sys
if sys.version_info[0] == 2:
    input = raw_input

res = None
G = None

def dfs(jibun_v, p, jibun_c):
    global res
    global G

    res[jibun_v] = jibun_c
    #print(jibun_v, p, jibun_c)

    tonari_nodes = G[jibun_v]
    for tonari_v, w in tonari_nodes:
        #if tonari_v == p or res[tonari_v] != -1:
        if res[tonari_v] != -1:
            continue

        if w % 2 == 0:
            tonari_c = jibun_c
        else:
            tonari_c = 1 - jibun_c

        dfs(tonari_v, jibun_v, tonari_c)
    

def main():
    N= int(input())

    global G
    G = [[] for _ in range(N)]

    for i in range(N-1):
        u, v, w = map(int, input().split(" "))
        u -= 1
        v -= 1
        w = w % 2
        G[u].append([v, w])
        G[v].append([u, w])

    global res
    res = [-1 for _ in range(N)]
    
    jibun_v = 0
    p = -1
    jibun_c = 0
    dfs(jibun_v, p, jibun_c)

    for i in range(N):
        print(res[i])

main()

Buggy submission (verdict: Runtime Error):
import sys
if sys.version_info[0] == 2:
    input = raw_input

res = None
G = None

def dfs(jibun_v, p, jibun_c):
    global res
    global G

    res[jibun_v] = jibun_c
    #print(jibun_v, p, jibun_c)

    tonari_nodes = G[jibun_v]
    for tonari_v, w in tonari_nodes:
        #if tonari_v == p or res[tonari_v] != -1:
        if res[tonari_v] != -1:
            continue

        if w % 2 == 0:
            tonari_c = jibun_c
        else:
            tonari_c = 1 - jibun_c

        dfs(tonari_v, jibun_v, tonari_c)
    

def main():
    N= int(input())

    global G
    G = [[] for _ in range(N)]

    for i in range(N-1):
        u, v, w = map(int, input().split(" "))
        u -= 1
        v -= 1
        w = w % 2
        G[u].append([v, w])
        G[v].append([u, w])

    global res
    res = [-1 for _ in range(N)]
    
    jibun_v = 0
    p = -1
    jibun_c = 0
    dfs(jibun_v, p, jibun_c)

    for i in range(N):
        print(res[i])

main()

Buggy submission (verdict: Runtime Error):
#input = raw_input

res = None
G = None

def dfs(jibun_v, p, jibun_c):
    global res
    global G

    res[jibun_v] = jibun_c
    #print(jibun_v, p, jibun_c)

    tonari_nodes = G[jibun_v]
    for tonari_v, w in tonari_nodes:
        #if tonari_v == p or res[tonari_v] != -1:
        if tonari_v == p or res[tonari_v] != -1:
            continue

        if w % 2 == 0:
            tonari_c = jibun_c
        else:
            tonari_c = 1 - jibun_c

        dfs(tonari_v, jibun_v, tonari_c)
    

def main():
    N= int(input())

    global G
    G = [[] for _ in range(N)]

    for i in range(N-1):
        u, v, w = map(int, input().split(" "))
        u -= 1
        v -= 1
        w = w % 2
        G[u].append([v, w])
        G[v].append([u, w])

    global res
    res = [-1 for _ in range(N)]
    
    jibun_v = 0
    p = -1
    jibun_c = 0
    dfs(jibun_v, p, jibun_c)

    for i in range(N):
        print(res[i])

main()

Buggy submission (verdict: Runtime Error):
#input = raw_input

res = None
G = None

def dfs(jibun_v, p, jibun_c):
    global res
    global G

    res[jibun_v] = jibun_c
    #print(jibun_v, p, jibun_c)

    tonari_nodes = G[jibun_v]
    for tonari_v, w in tonari_nodes:
        #if tonari_v == p or res[tonari_v] != -1:
        if res[tonari_v] != -1:
            continue

        if w % 2 == 0:
            tonari_c = jibun_c
        else:
            tonari_c = 1 - jibun_c

        dfs(tonari_v, jibun_v, tonari_c)
    

def main():
    N= int(input())

    global G
    G = [[] for _ in range(N)]

    for i in range(N-1):
        u, v, w = map(int, input().split(" "))
        u -= 1
        v -= 1
        w = w % 2
        G[u].append([v, w])
        G[v].append([u, w])

    global res
    res = [-1 for _ in range(N)]
    
    jibun_v = 0
    p = -1
    jibun_c = 0
    dfs(jibun_v, p, jibun_c)

    for i in range(N):
        print(res[i])

main()

Buggy submission (verdict: Runtime Error):
input = raw_input

res = None
G = None

def dfs(jibun_v, p, jibun_c):
    global res
    global G

    res[jibun_v] = jibun_c
    #print(jibun_v, p, jibun_c)

    tonari_nodes = G[jibun_v]
    for tonari_v, w in tonari_nodes:
        #if tonari_v == p or res[tonari_v] != -1:
        if res[tonari_v] != -1:
            continue

        if w % 2 == 0:
            tonari_c = jibun_c
        else:
            tonari_c = 1 - jibun_c

        dfs(tonari_v, jibun_v, tonari_c)
    

def main():
    N= int(input())

    global G
    G = [[] for _ in range(N)]

    for i in range(N-1):
        u, v, w = map(int, input().split(" "))
        u -= 1
        v -= 1
        w = w % 2
        G[u].append([v, w])
        G[v].append([u, w])

    global res
    res = [-1 for _ in range(N)]
    
    jibun_v = 0
    p = -1
    jibun_c = 0
    dfs(jibun_v, p, jibun_c)

    for i in range(N):
        print(res[i])

main()

Buggy submission (verdict: Runtime Error):
#input = raw_input

res = None
G = None

def dfs(jibun_v, p, jibun_c):
    global res
    global G

    res[jibun_v] = jibun_c
    #print(jibun_v, p, jibun_c)

    tonari_nodes = G[jibun_v]
    for tonari_v, w in tonari_nodes:
        if tonari_v == p:
            continue

        if w % 2 == 0:
            tonari_c = jibun_c
        else:
            tonari_c = 1 - jibun_c

        dfs(tonari_v, jibun_v, tonari_c)
    

def main():
    N= int(input())

    global G
    G = [[] for _ in range(N)]

    for i in range(N-1):
        u, v, w = map(int, input().split(" "))
        u -= 1
        v -= 1
        w = w % 2
        G[u].append([v, w])
        G[v].append([u, w])

    global res
    res = [-1 for _ in range(N)]
    
    jibun_v = 0
    p = -1
    jibun_c = 0
    dfs(jibun_v, p, jibun_c)

    for i in range(N):
        print(res[i])

main()

Buggy submission (verdict: Runtime Error):
class G:
    def __init__(self, i):
        self.graph = [i]
        self.id = i
        self.gid = -1


def concat(u, v):
    g1 = data[u].graph
    g2 = data[v].graph
    g = g1 + g2
    all_g.append(g)
    id = min(data[u].id, data[v].id)
    
    for elm in g:
        data[elm].graph = g
        data[elm].id = id

def make_revid(id):
    revid = id + 1 if id %2 == 0 else id - 1
    return revid


def sep_g(u, v):
    global gid_
    global all_gid

    gid0 = data[u].gid
    gid1 = data[v].gid
    cond1 = data[u].gid >= 0
    cond2 = data[v].gid >= 0

    if cond1 or cond2:
        if cond1 and cond2:
            if gid0 <= gid1:
                old_gid = data[v].gid
                new_gid = make_revid(gid0)
                data[v].gid = new_gid
            else:
                old_gid = data[u].gid
                new_gid = make_revid(gid1)
                data[u].gid = new_gid

        elif cond1 and not cond2:
            old_gid = data[v].gid
            new_gid = make_revid(gid0)
            data[v].gid = new_gid
        elif not cond1 and not cond2:
            old_gid = data[u].gid
            new_gid =  make_revid(gid1)
            data[u].gid = new_gid

        all_gid.remove(old_gid)
        all_gid.append(new_gid)

    else:
        data[u].gid = gid_
        data[v].gid = gid_ + 1
        all_gid += [gid_, gid_+1]
        gid_ += 2


gid_ = 0
N = int(input())

odd_pairs = []
all_g = []
all_gid = []

data = {}

for i in range(N):
    g = G(i)
    data[i] = g
    all_g.append(g)

for i in range(N-1):
    u, v, w = [int(elm) for elm in input().split(" ")]
    u -= 1
    v -= 1

    if w %2 == 1:
        odd_pairs.append([u, v, w])
        continue
    
    if v not in data[u].graph:
        concat(u, v)

for u, v, w in odd_pairs:
    sep_g(u, v)


for i in range(N):
    gid = data[i].gid
    if gid < 0:
        out = 0
    else:
        out = gid % 2
    print(out)

Buggy submission (verdict: Runtime Error):
class G:
    def __init__(self, i):
        self.graph = [i]
        self.id = i
        self.gid = -1


def concat(u, v):
    g1 = data[u].graph
    g2 = data[v].graph
    g = g1 + g2
    all_g.append(g)
    id = min(data[u].id, data[v].id)
    
    for elm in g:
        data[elm].graph = g
        data[elm].id = id

def make_revid(id):
    revid = id + 1 if id %2 == 0 else id - 1
    return revid


def sep_g(u, v):
    global gid_
    global all_gid

    gid0 = data[u].gid
    gid1 = data[v].gid
    cond1 = data[u].gid >= 0
    cond2 = data[v].gid >= 0

    if cond1 or cond2:
        if cond1 and cond2:
            if gid0 <= gid1:
                old_gid = data[v].gid
                new_gid = make_revid(gid0)
                data[v].gid = new_gid
            else:
                old_gid = data[u].gid
                new_gid = make_revid(gid1)
                data[u].gid = new_gid

        elif cond1 and not cond2:
            old_gid = data[v].gid
            new_gid = make_revid(gid0)
            data[v].gid = new_gid
        elif not cond1 and not cond2:
            old_gid = data[u].gid
            new_gid =  make_revid(gid1)
            data[u].gid = new_gid

        all_gid.remove(old_gid)
        all_gid.append(new_gid)

    else:
        data[u].gid = gid_
        data[v].gid = gid_ + 1
        all_gid += [gid_, gid_+1]
        gid_ += 2


gid_ = 0
N = int(input())

odd_pairs = []
all_g = []
all_gid = []

data = {}

for i in range(N):
    g = G(i)
    data[i] = g
    all_g.append(g)

for i in range(N-1):
    u, v, w = [int(elm) for elm in input().split(" ")]
    u -= 1
    v -= 1

    if w %2 == 1:
        odd_pairs.append([u, v, w])
        continue
    
    if v not in data[u].graph:
        concat(u, v)

for u, v, w in odd_pairs:
    sep_g(u, v)


for i in range(N):
    gid = data[i].gid
    if gid < 0:
        out = 0
    else:
        out = gid % 2
    print(out)

Buggy submission (verdict: Runtime Error):
class G:
    def __init__(self, i):
        self.graph = [i]
        self.id = i
        self.gid = -1


def concat(u, v):
    g1 = data[u].graph
    g2 = data[v].graph
    g = g1 + g2
    all_g.append(g)
    id = min(data[u].id, data[v].id)
    
    for elm in g:
        data[elm].graph = g
        data[elm].id = id

def make_revid(id):
    revid = id + 1 if id %2 == 0 else id - 1
    return revid


def sep_g(u, v):
    global gid_
    global all_gid

    gid0 = data[u].gid
    gid1 = data[v].gid
    cond1 = data[u].gid >= 0
    cond2 = data[v].gid >= 0

    if cond1 or cond2:
        if cond1 and cond2:
            if gid0 <= gid1:
                old_gid = data[v].gid
                new_gid = make_revid(gid0)
                data[v].gid = new_gid
            else:
                old_gid = data[u].gid
                new_gid = make_revid(gid1)
                data[u].gid = new_gid

        elif cond1 and not cond2:
            old_gid = data[v].gid
            new_gid = make_revid(gid0)
            data[v].gid = new_gid
        elif not cond1 and not cond2:
            old_gid = data[u].gid
            new_gid =  make_revid(gid1)
            data[u].gid = new_gid

        all_gid.remove(old_gid)
        all_gid.append(new_gid)

    else:
        data[u].gid = gid_
        data[v].gid = gid_ + 1
        all_gid += [gid_, gid_+1]
        gid_ += 2


gid_ = 0
N = int(input())

odd_pairs = []
all_g = []
all_gid = []

data = {}

for i in range(N):
    g = G(i)
    data[i] = g
    all_g.append(g)

for i in range(N-1):
    u, v, w = [int(elm) for elm in input().split(" ")]
    u -= 1
    v -= 1

    if w %2 == 1:
        odd_pairs.append([u, v, w])
        continue
    
    if v not in data[u].graph:
        concat(u, v)

for u, v, w in odd_pairs:
    sep_g(u, v)


for i in range(N):
    gid = data[i].gid
    if gid < 0:
        out = 0
    else:
        out = gid % 2
    print(out)