Problem p03575 (python)

Fixed submission:
import collections
import queue
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

N, M = map(int, input().split())
X, Y = [0] * M, [0] * M
g = [[] for _ in range(N)]

for i in range(M):
    X[i], Y[i] = map(int, input().split())

    X[i] -= 1
    Y[i] -= 1
    g[X[i]].append(Y[i])
    g[Y[i]].append(X[i])


def bfs(x, y):
    visited = [False] * N
    q = queue.Queue()
    for nx in g[x]:
        if nx == y:
            continue
        q.put(nx)
        visited[nx] = True

    visited[x] = True

    while not q.empty():
        cur = q.get()
        if cur == y:
            return True

        for nx in g[cur]:
            if visited[nx]:
                continue
            q.put(nx)
            visited[nx] = True

    return False


cnt = 0
for i in range(M):
    if not bfs(X[i], Y[i]):
        cnt += 1

print(cnt)

Buggy submission (verdict: Runtime Error):
import collections
import queue
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

N, M = map(int, input().split())
X, Y = [0] * N, [0] * N
g = [[] for _ in range(N)]

for i in range(M):
    X[i], Y[i] = map(int, input().split())

    X[i] -= 1
    Y[i] -= 1
    g[X[i]].append(Y[i])
    g[Y[i]].append(X[i])


def bfs(x, y):
    visited = [False] * N
    q = collections.deque()
    l = 0
    for nx in g[x]:
        if nx == y:
            continue
        q.append(nx)
        visited[nx] = True
        l += 1

    visited[x] = True

    while l > 0:
        cur = q.pop()
        l -= 1
        if cur == y:
            return True

        for nx in g[cur]:
            if visited[nx]:
                continue
            q.append(nx)
            visited[nx] = True
            l += 1

    return False


cnt = 0
for i in range(M):
    if not bfs(X[i], Y[i]):
        cnt += 1

print(cnt)

Buggy submission (verdict: Runtime Error):
import collections
import queue
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

N, M = map(int, input().split())
X, Y = [0] * N, [0] * N
g = [[] for _ in range(N)]

for i in range(M):
    X[i], Y[i] = map(int, input().split())

    X[i] -= 1
    Y[i] -= 1
    g[X[i]].append(Y[i])
    g[Y[i]].append(X[i])


def bfs(x, y):
    visited = [False] * N
    q = collections.deque()
    l = 0
    for nx in g[x]:
        if nx == y:
            continue
        q.append(nx)
        l += 1

    visited[x] = True

    while l > 0:
        cur = q.pop()
        l -= 1
        if cur == y:
            return True

        visited[cur] = True
        for nx in g[cur]:
            if visited[nx]:
                continue
            q.append(nx)
            l += 1

    return False


cnt = 0
for i in range(M):
    if not bfs(X[i], Y[i]):
        cnt += 1

print(cnt)

Buggy submission (verdict: Runtime Error):
/Users/a13937/Documents/workplace/atcoder-practice/venv/bin/python /Users/a13937/Documents/workplace/atcoder-practice/src/main/scala/abc075/C.py
6 5
1 2
2 3
3 4
4 5
5 6
5

Process finished with exit code 0

Buggy submission (verdict: Runtime Error):
import queue
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

N, M = map(int, input().split())
X, Y = [0] * N, [0] * N
g = [[] for _ in range(N)]

for i in range(M):
    X[i], Y[i] = map(int, input().split())

    X[i] -= 1
    Y[i] -= 1
    g[X[i]].append(Y[i])
    g[Y[i]].append(X[i])


def bfs(x, y):
    visited = [False] * N
    q = queue.LifoQueue()
    for nx in g[x]:
        if nx == y:
            continue
        q.put(nx)

    visited[x] = True

    while not q.empty():
        cur = q.get()
        if cur == y:
            return True

        visited[cur] = True
        for nx in g[cur]:
            if visited[nx]:
                continue
            q.put(nx)

    return False

cnt = 0
for i in range(M):
    if not bfs(X[i], Y[i]):
        cnt += 1

print(cnt)

Buggy submission (verdict: Runtime Error):
import queue
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

N, M = map(int, input().split())
X, Y = [0] * N, [0] * N
g = [[] for _ in range(N)]

for i in range(M):
    X[i], Y[i] = map(int, input().split())

    X[i] -= 1
    Y[i] -= 1
    g[X[i]].append(Y[i])
    g[Y[i]].append(X[i])


def bfs(x, y):
    visited = [False] * N
    q = queue.Queue()
    for nx in g[x]:
        if nx == y:
            continue
        q.put(nx)

    visited[x] = True

    while not q.empty():
        cur = q.get()
        if cur == y:
            return True

        visited[cur] = True
        for nx in g[cur]:
            if visited[nx]:
                continue
            q.put(nx)

    return False

cnt = 0
for i in range(M):
    if not bfs(X[i], Y[i]):
        cnt += 1

print(cnt)