Problem p03045 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10000000)

def visit_tree(now, num):
    visited[now] = num
    for i in range(len(tree[now])):
        if not visited[tree[now][i]]:
            visit_tree(tree[now][i], num)

n, m = map(int, input().split())
tree = [[] for _ in range(n)]
for i in range(m):
    x, y, z = map(int, input().split())
    tree[x - 1].append(y - 1)
    tree[y - 1].append(x - 1)

visited = [0 for _ in range(n)]
num = 0
for i in range(n):
    if not visited[i]:
        num += 1
        visit_tree(i, num)

print(num)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000)

def visit_tree(now, num):
    visited[now] = num
    for i in range(len(tree[now])):
        if not visited[tree[now][i]]:
            visit_tree(tree[now][i], num)

n, m = map(int, input().split())
tree = [[] for _ in range(n)]
for i in range(m):
    x, y, z = map(int, input().split())
    tree[x - 1].append(y - 1)
    tree[y - 1].append(x - 1)

visited = [0 for _ in range(n)]
num = 0
for i in range(n):
    if not visited[i]:
        num += 1
        visit_tree(i, num)

print(num)

Buggy submission (verdict: Runtime Error):
def visit_tree(now, num):
    if not visited[now]:
        visited[now] = num
        if len(tree[now]) == 0:
            return 0
        for i in range(len(tree[now])):
            visit_tree(tree[now][i], num)

n, m = map(int, input().split())
tree = [[] for _ in range(n)]
for i in range(m):
    x, y, z = map(int, input().split())
    tree[x - 1].append(y - 1)
    tree[y - 1].append(x - 1)

visited = [0 for _ in range(n)]
num = 0
for i in range(n):
    if not visited[i]:
        num += 1
        visit_tree(i, num)

print(num)

Buggy submission (verdict: Runtime Error):
def visit_tree(now, num):
    if now >= len(visited):
        return 0
    if not visited[now]:
        visited[now] = num
        for i in range(len(tree[now])):
            visit_tree(tree[now][i], num)

n, m = map(int, input().split())
tree = [[] for _ in range(n)]
for i in range(m):
    x, y, z = map(int, input().split())
    tree[x - 1].append(y - 1)
    tree[y - 1].append(x - 1)

visited = [0 for _ in range(n)]
num = 0
for i in range(n):
    if not visited[i]:
        num += 1
        visit_tree(i, num)

print(num)

Buggy submission (verdict: Runtime Error):
def visit_tree(now, num):
    if not visited[now]:
        visited[now] = num
        for i in range(len(tree[now])):
            visit_tree(tree[now][i], num)

n, m = map(int, input().split())
tree = [[] for _ in range(n)]
for i in range(m):
    x, y, z = map(int, input().split())
    tree[x - 1].append(y - 1)
    tree[y - 1].append(x - 1)

visited = [0 for _ in range(n)]
num = 0
for i in range(n):
    if not visited[i]:
        num += 1
        visit_tree(i, num)

print(num)

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
datalist = [0 for i in range(n)]
ans = 0
root = [[] for i in range(n)]
for i in range(m):
    x, y, z = map(int, input().split())
    root[x - 1].append(y - 1)
    root[y - 1].append(x - 1)

def solve(now):
    global ans, root
    for j in range(len(root[now])):
        if not datalist[root[now][j]]:
            datalist[root[now][j]] = 1
            solve(root[now][j])

for i in range(n):
    if not datalist[i]:
        solve(i)
        ans += 1

for i in datalist:
    if i == 0:
        ans += 1
        datalist[i] = 1
        solve(i)

print(ans)

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
datalist = [0 for i in range(n)]
ans = 0
root = [[] for i in range(n)]
for i in range(m):
    x, y, z = map(int, input().split())
    root[x - 1].append(y - 1)
    root[y - 1].append(x - 1)

def solve(now):
    global ans, root
    for j in range(len(root[now])):
        if not datalist[root[now][j]]:
            datalist[root[now][j]] = 1
            solve(root[now][j])

for i in range(n):
    if not datalist[i]:
        solve(i)
        ans += 1

for i in datalist:
    if i == 0:
        ans += 1
        datalist[i] = 1
        solve(i)

print(ans)

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
datalist = [0 for i in range(n)]
ans = 0
root = [[] for i in range(n)]
for i in range(m):
    x, y, z = map(int, input().split())
    root[x - 1].append(y - 1)
    root[y - 1].append(x - 1)

def solve(now):
    global ans, root
    if datalist[now] == 0:
        ans += 1
    datalist[now] = 1
    for j in range(len(root[now])):
        if not datalist[root[now][j]]:
            datalist[root[now][j]] = 1
            solve(root[now][j])

for i in range(n):
    if not datalist[i]:
        #print(i,i,i)
        solve(i)

for i in datalist:
    if i == 0:
        ans += 1

#print(datalist)
print(ans)

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
datalist = [0 for i in range(n)]
ans = 0
root = [[] for i in range(n)]
for i in range(m):
    x, y, z = map(int, input().split())
    root[x - 1].append(y - 1)
    root[y - 1].append(x - 1)

def solve(now, root):
    global ans
    #print(now, ans)
    if datalist[now] == 0:
        ans += 1
    datalist[now] = 1
    for j in range(len(root[now])):
        if datalist[root[now][j]] == 0:
            datalist[root[now][j]] = 1
            solve(root[now][j], root)

for i in range(n):
    if datalist[i] == 0:
        #print(i,i,i)
        solve(i, root)

for i in datalist:
    if i == 0:
        ans += 1

#print(datalist)
print(ans )