Problem p02850 (python)

Fixed submission:
def f(u, i, d, t, vis):
    if vis[u]:
        return
    vis[u] = True
    z = t[u]

    c = 1
    for v in z:
        if vis[v]:
            continue
        if c == i:
            c += 1
        d[u].update({v: c})
        f(v, c, d, t, vis)
        c += 1


def main():
    from collections import defaultdict
    import sys
    sys.setrecursionlimit(100000)
    n = int(input())
    t = [[] for _ in range(n + 1)]
    m = []

    for _ in range(n - 1):
        i, j = map(int, input().split())
        t[i].append(j)
        t[j].append(i)
        m.append([i, j])
    d = defaultdict(dict)
    vis = [False for _ in range(n + 1)]
    f(1, len(t[1]) + 1, d, t, vis)

    ans = ''
    if t.count([]) == n:
        ans += str(n - 1) + '\n'
    else:
        ans += str(max(len(i) for i in t)) + '\n'

    for (u, v) in m:
        ans += str(d[u][v]) + '\n'

    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def f(u, i, d, t, vis):
    if vis[u]:
        return
    vis[u] = True
    z = t[u]

    c = 1
    for v in z:
        if vis[v]:
            continue
        if c == i:
            c += 1
        d[u].update({v: c})
        f(v, c, d, t, vis)
        c += 1


def main():
    from collections import defaultdict
    n = int(input())
    t = [[] for _ in range(n + 1)]
    m = []

    for _ in range(n - 1):
        i, j = map(int, input().split())
        t[i].append(j)
        t[j].append(i)
        m.append([i, j])
    d = defaultdict(dict)
    vis = [False for _ in range(n + 1)]
    f(1, len(t[1]) + 1, d, t, vis)

    ans = ''
    if t.count([]) == n:
        ans += str(n - 1) + '\n'
    else:
        ans += str(max(len(i) for i in t)) + '\n'

    for (u, v) in m:
        ans += str(d[u][v]) + '\n'

    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def f(u, i, d, t):
    z = t[u]
    c = 1
    for v in z:
        if c == i:
            c += 1
        d[u].update({v: c})
        f(v, c, d, t)
        c += 1


def main():
    from collections import deque, defaultdict
    n = int(input())
    t = [[] for _ in range(n + 1)]
    m = []

    for _ in range(n - 1):
        i, j = map(int, input().split())
        i, j = min(i, j), max(i, j)
        t[i].append(j)
        m.append([i, j])
    d = defaultdict(dict)

    f(1, len(t[1]) + 1, d, t)

    ans = ''
    if t.count([]) == n:
        ans += str(n - 1) + '\n'
    else:
        ans += str(max(len(i) for i in t) + 1) + '\n'

    for (u, v) in m:
        ans += str(d[u][v]) + '\n'

    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    from collections import deque, defaultdict
    n = int(input())
    t = [[] for _ in range(n + 1)]
    m = []

    for _ in range(n - 1):
        i, j = map(int, input().split())
        i, j = min(i, j), max(i, j)
        t[i].append(j)
        m.append([i, j])
    d = defaultdict(dict)

    def f(u, i):
        z = t[u]
        c = 1
        for v in z:
            if c == i:
                c += 1
            d[u].update({v: c})
            f(v, c)
            c += 1

    f(1, len(t[1]) + 1)

    ans = ''
    if t.count([]) == n:
        ans += str(n - 1) + '\n'
    else:
        ans += str(max(len(i) for i in t) + 1) + '\n'

    for (u, v) in m:
        ans += str(d[u][v]) + '\n'

    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    from collections import deque, defaultdict
    n = int(input())
    t = [[] for _ in range(n + 1)]
    m = []

    for _ in range(n - 1):
        i, j = map(int, input().split())
        t[i].append(j)
        m.append([i, j])
    d = defaultdict(dict)

    def f(u, i):
        z = t[u]
        c = 1
        for v in z:
            if c == i:
                c += 1
            d[u].update({v: c})
            f(v, c)
            c += 1

    f(m[0][0], len(t[m[0][0]]) + 1)

    ans = ''
    if t.count([]) == n:
        ans += str(n - 1) + '\n'
    else:
        ans += str(max(len(i) for i in t) + 1) + '\n'

    for (u, v) in m:
        ans += str(d[u][v]) + '\n'

    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    from collections import deque, defaultdict
    n = int(input())
    t = [[] for _ in range(n + 1)]
    m = []

    for _ in range(n - 1):
        i, j = map(int, input().split())
        t[i].append(j)
        m.append([i, j])
    d = defaultdict(dict)

    def f(u, i):
        z = t[u]
        l = len(z)
        r = list(range(1, i)) + list(range(i + 1, l + 2))
        for v, j in zip(z, r):
            d[u].update({v: j})
            f(v, j)

    f(m[0][0], len(t[m[0][0]]) + 1)

    ans = ''
    if t.count([]) == n:
        ans += str(n - 1) + '\n'
    else:
        ans += str(max(len(i) for i in t) + 1) + '\n'

    for (u, v) in m:
        ans += str(d[u][v]) + '\n'

    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    from collections import deque, defaultdict
    n = int(input())
    t = [[] for _ in range(n + 1)]
    m = []

    for _ in range(n - 1):
        i, j = map(int, input().split())
        t[i].append(j)
        m.append([i, j])
    d = defaultdict(dict)

    def f(u, i):
        z = t[u]
        l = len(z)
        s = set(range(1, l + 2)) - {i}
        r = list(s)
        r.sort()
        for v, j in zip(z, r):
            d[u].update({v: j})
            f(v, j)

    f(m[0][0], len(t[m[0][0]]) + 1)

    if t.count([]) == n:
        print(n - 1)
    else:
        print(max(len(i) for i in t) + 1)

    for (u, v) in m:
        print(d[u][v])


if __name__ == '__main__':
    main()