Problem p02850 (python)

Fixed submission:
from collections import deque
n = int(raw_input())
edges = []
for i in range(n - 1):
    edges.append(map(int, raw_input().split()))
edge_num = {}
G = [[] for i in range(n)]
ans = [0] * (n - 1)
for i in range(n - 1):
    s, e = edges[i]
    s = s - 1
    e = e - 1
    edge_num[(s, e)] = i
    edge_num[(e, s)] = i
    G[s].append(e)
    G[e].append(s)
vis = set([])
q = deque([0])
vis.add(0)
while q:
    u = q.popleft()
    not_colored = []
    colors = set([])
    for v in G[u]:
        i = edge_num[(u, v)]
        if ans[i] == 0:
            not_colored.append(i)
        else:
            colors.add(ans[i])
        if not v in vis:
            vis.add(v)
            q.append(v)
    color = 1
    for i in not_colored:
        while color in colors:
            color += 1
        ans[i] = color
        color += 1
print len(set(ans))
for i in ans:
    print i

Buggy submission (verdict: Runtime Error):
import queue
n = int(raw_input())
edges = []
for i in range(n - 1):
    edges.append(map(int, raw_input().split()))
edge_num = {}
G = [[] for i in range(n)]
ans = [0] * (n - 1)
for i in range(n - 1):
    s, e = edges[i]
    s = s - 1
    e = e - 1
    edge_num[(s, e)] = i
    edge_num[(e, s)] = i
    G[s].append(e)
    G[e].append(s)
vis = set([])
q = queue.Queue(n)
q.put(0)
vis.add(0)
while q.qsize() > 0:
    u = q.get()
    not_colored = []
    colors = set([])
    for v in G[u]:
        i = edge_num[(u, v)]
        if ans[i] == 0:
            not_colored.append(i)
        else:
            colors.add(ans[i])
        if not v in vis:
            vis.add(v)
            q.put(v)
    color = 1
    for i in not_colored:
        while color in colors:
            color += 1
        ans[i] = color
        color += 1
print len(set(ans))
for i in ans:
    print i

Buggy submission (verdict: Runtime Error):
from collections import deque
n = int(raw_input())
edges = []
for i in range(n - 1):
    edges.append(map(int, raw_input().split()))
edge_num = {}
G = [[] for i in range(n)]
ans = [0] * (n - 1)
for i in range(n - 1):
    s, e = edges[i]
    s = s - 1
    e = e - 1
    edge_num[(s, e)] = i
    edge_num[(e, s)] = i
    G[s].append(e)
    G[e].append(s)
vis = set([])
q = deque([0])
vis.add(u)
while q:
    u = q.popleft()
    not_colored = []
    colors = set([])
    for v in G[u]:
        i = edge_num[(u, v)]
        if ans[i] == 0:
            not_colored.append(i)
        else:
            colors.add(ans[i])
        if not v in vis:
            vis.add(v)
            q.append(v)
    color = 1
    for i in not_colored:
        while color in colors:
            color += 1
        ans[i] = color
        color += 1
print len(set(ans))
for i in ans:
    print i

Buggy submission (verdict: Runtime Error):
import queue
n = int(raw_input())
edges = []
for i in range(n - 1):
    edges.append(map(int, raw_input().split()))
edge_num = {}
G = [[] for i in range(n + 1)]
ans = [0] * (n - 1)
for i in range(n - 1):
    s, e = edges[i]
    s = s - 1
    e = e - 1
    edge_num[(s, e)] = i
    edge_num[(e, s)] = i
    G[s].append(e)
    G[e].append(s)
vis = set([])
q = queue.Queue(n)
vis.add(u)
q.put(0)
while q.qsize() > 0:
    u = q.get()
    not_colored = []
    colors = set([])
    for v in G[u]:
        i = edge_num[(u, v)]
        if ans[i] == 0:
            not_colored.append(i)
        else:
            colors.add(ans[i])
        if not v in vis:
            vis.add(v)
            q.put(v)
    color = 1
    for i in not_colored:
        while color in colors:
            color += 1
        ans[i] = color
        color += 1
print len(set(ans))
for i in ans:
    print i

Buggy submission (verdict: Runtime Error):
import queue
n = int(raw_input())
edges = []
for i in range(n - 1):
    edges.append(map(int, raw_input().split()))
edge_num = {}
G = [[] for i in range(n + 1)]
ans = [0] * (n - 1)
for i in range(n - 1):
    s, e = edges[i]
    s = s - 1
    e = e - 1
    edge_num[(s, e)] = i
    edge_num[(e, s)] = i
    G[s].append(e)
    G[e].append(s)
vis = set([])
q = queue.Queue(n)
vis.add(u)
q.put(0)
while q.qsize() > 0:
    u = q.get()
    not_colored = []
    colors = set([])
    for v in G[u]:
        i = edge_num[(u, v)]
        if ans[i] == 0:
            not_colored.append(i)
        else:
            colors.add(ans[i])
        if not v in vis:
            vis.add(v)
            q.put(v)
    color = 1
    for i in not_colored:
        while color in colors:
            color += 1
        ans[i] = color
        color += 1
print len(set(ans))
for i in ans:
    print i

Buggy submission (verdict: Runtime Error):
import queue
n = int(raw_input())
edges = []
for i in range(n - 1):
    edges.append(map(int, raw_input().split()))
edge_num = {}
G = [[] for i in range(n + 1)]
ans = [0] * (n - 1)
for i in range(n - 1):
    s, e = edges[i]
    s = s - 1
    e = e - 1
    edge_num[(s, e)] = i
    edge_num[(e, s)] = i
    G[s].append(e)
    G[e].append(s)
vis = set([])
q = queue.Queue(n)
q.put(0)
while q.qsize() > 0:
    u = q.get()
    vis.add(u)
    not_colored = []
    colors = set([])
    for v in G[u]:
        i = edge_num[(u, v)]
        if ans[i] == 0:
            not_colored.append(i)
        else:
            colors.add(ans[i])
        if not v in vis:
            q.put(v)
    color = 1
    for i in not_colored:
        while color in colors:
            color += 1
        ans[i] = color
        color += 1
print ans
print len(set(ans))
for i in ans:
    print i