Problem p02762 (python)

Fixed submission:
from sys import stdin, setrecursionlimit


class UnionFindTree:
    def __init__(self, n):
        self.parent = [-1] * n

    def find(self, x):
        p = self.parent
        while p[x] >= 0:
            x, p[x] = p[x], p[p[x]]
        return x

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        p = self.parent
        if x != y:
            x, y = sorted([x, y])
            p[x], p[y] = p[x] + p[y], x

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def size(self, x):
        return -self.parent[self.find(x)]


def main():
    n, m, k = map(int, stdin.readline().split())

    f = UnionFindTree(n)

    f_or_b = [0] * n

    for _ in range(m):
        a, b = map(int, stdin.readline().split())
        f.union(a - 1, b - 1)
        f_or_b[a - 1] += 1
        f_or_b[b - 1] += 1

    for _ in range(k):
        c, d = map(int, stdin.readline().split())
        if f.same(c - 1, d - 1):
            f_or_b[c - 1] += 1
            f_or_b[d - 1] += 1

    print(*[f.size(i) - f_or_b[i] - 1 for i in range(n)])


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Buggy submission (verdict: Runtime Error):

from sys import stdin, setrecursionlimit


def dfs(f, v, seen):
    seen[v] = True

    for nv in f[v]:
        if seen[nv]:
            continue
        dfs(f, nv, seen)


def main():
    n, m, k = map(int, stdin.readline().split())

    f = [[] for _ in range(n)]

    for _ in range(m):
        a, b = map(int, stdin.readline().split())
        f[a - 1].append(b - 1)
        f[b - 1].append(a - 1)

    b = [[] for _ in range(n)]

    for _ in range(k):
        c, d = map(int, stdin.readline().split())
        b[c - 1].append(d - 1)
        b[d - 1].append(c - 1)

    ans = [0] * n

    u = [-1] * n
    uid = 0
    for i in range(n):
        if u[i] == -1:
            seen = [False] * n
            dfs(f, i, seen)
            for j in range(n):
                if seen[j]:
                    u[j] = uid
        uid += 1

    for i in range(n):
        uid = u[i]
        arr = [i for i in range(n) if u[i] == uid]
        tmp = len((set(arr) - set(f[i])) - set(b[i]))
        tmp -= 1
        ans[i] = tmp

    print(*ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Buggy submission (verdict: Runtime Error):
from sys import stdin, setrecursionlimit


def dfs(f, v, seen):
    seen[v] = True

    for nv in f[v]:
        if seen[nv]:
            continue
        dfs(f, nv, seen)


def main():
    n, m, k = map(int, stdin.readline().split())

    f = [[] for _ in range(n)]

    for _ in range(m):
        a, b = map(int, stdin.readline().split())
        f[a - 1].append(b - 1)
        f[b - 1].append(a - 1)

    b = [[] for _ in range(n)]

    for _ in range(k):
        c, d = map(int, stdin.readline().split())
        b[c - 1].append(d - 1)
        b[d - 1].append(c - 1)

    ans = [0] * n

    u = [-1] * n
    uid = 0
    for i in range(n):
        if u[i] == -1:
            seen = [False] * n
            dfs(f, i, seen)
            for j in range(n):
                if seen[j]:
                    u[j] = uid
        uid += 1

    for i in range(n):
        uid = u[i]
        arr = [i for i in range(n) if u[i] == uid]
        tmp = len(set(arr) - set(f[i]) - set(b[i]))
        tmp -= 1
        ans[i] = tmp

    print(*ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Buggy submission (verdict: Runtime Error):
from sys import stdin, setrecursionlimit


def dfs(f, v, seen):
    seen[v] = True

    for nv in f[v]:
        if seen[nv]:
            continue
        dfs(f, nv, seen)


def main():
    n, m, k = map(int, stdin.readline().split())

    f = [[] for _ in range(n)]

    for _ in range(m):
        a, b = map(int, stdin.readline().split())
        f[a - 1].append(b - 1)
        f[b - 1].append(a - 1)

    b = [[] for _ in range(n)]

    for _ in range(k):
        c, d = map(int, stdin.readline().split())
        b[c - 1].append(d - 1)
        b[d - 1].append(c - 1)

    ans = [0] * n
    for i in range(n):
        seen = [False] * n
        dfs(f, i, seen)
        arr = []
        for j in range(n):
            if seen[j]:
                arr.append(j)
        tmp = len(set(arr) - set(f[i]) - set(b[i]))
        tmp -= 1
        ans[i] = tmp

    print(*ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Buggy submission (verdict: Runtime Error):
from sys import stdin, setrecursionlimit


def dfs(f, v, seen):
    seen[v] = True

    for nv in f[v]:
        if seen[nv]:
            continue
        dfs(f, nv, seen)


def main():
    n, m, k = map(int, stdin.readline().split())

    f = [[] for _ in range(n)]

    for _ in range(m):
        a, b = map(int, stdin.readline().split())
        f[a - 1].append(b - 1)
        f[b - 1].append(a - 1)

    b = [[] for _ in range(n)]

    for _ in range(k):
        c, d = map(int, stdin.readline().split())
        b[c - 1].append(d - 1)
        b[d - 1].append(c - 1)

    ans = [0] * n
    for i in range(n):
        seen = [False] * n
        dfs(f, i, seen)
        arr = []
        for j in range(n):
            if seen[j]:
                arr.append(j)
        tmp = len(set(arr) - set(f[i]) - set(b[i]))
        tmp -= 1
        ans[i] = tmp

    print(*ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Buggy submission (verdict: Runtime Error):
from sys import stdin, setrecursionlimit


def main():
    n, m, k = map(int, stdin.readline().split())

    f = [[] for _ in range(n)]

    for _ in range(m):
        a, b = map(int, stdin.readline().split())
        f[a - 1].append(b - 1)
        f[b - 1].append(a - 1)

    b = [[] for _ in range(n)]

    for _ in range(k):
        c, d = map(int, stdin.readline().split())
        b[c - 1].append(d - 1)
        b[d - 1].append(c - 1)

    def dfs(v, seen):
        seen[v] = True

        for nv in f[v]:
            if seen[nv]:
                continue
            dfs(nv, seen)

    ans = [0] * n
    for i in range(n):
        seen = [False] * n
        dfs(i, seen)
        arr = []
        for j in range(n):
            if seen[j]:
                arr.append(j)
        tmp = len(set(arr) - set(f[i]) - set(b[i]))
        tmp -= 1
        ans[i] = tmp

    print(*ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Buggy submission (verdict: Runtime Error):
from sys import stdin, setrecursionlimit


def main():
    n, m, k = map(int, stdin.readline().split())

    f = [[] for _ in range(n)]

    for _ in range(m):
        a, b = map(int, stdin.readline().split())
        f[a - 1].append(b - 1)
        f[b - 1].append(a - 1)

    b = [[] for _ in range(n)]

    for _ in range(k):
        c, d = map(int, stdin.readline().split())
        b[c - 1].append(d - 1)
        b[d - 1].append(c - 1)

    def dfs(v, seen):
        seen[v] = True

        for nv in f[v]:
            if seen[nv]:
                continue
            dfs(nv, seen)

    ans = [0] * n
    for i in range(n):
        seen = [False] * n
        dfs(i, seen)
        arr = []
        for j in range(n):
            if seen[j]:
                arr.append(j)
        tmp = len(set(arr) - set(f[i]) - set(b[i]))
        tmp -= 1
        ans[i] = tmp

    print(*ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()