Buggy python submission (verdict: Runtime Error):
class UnionFind:
    Parents = None

    def __init__(self, N):
        self.Parents = [-1 for i in range(N + 1)]

    # Aがどのグループにいるのか調べる
    def root(self, A):
        if self.Parents[A] < 0:
            return A
        else:
            self.Parents[A] = self.root(self.Parents[A])
            return self.Parents[A]

    # Aが所属しているグループのサイズを返す
    def size(self, A):
        return -self.Parents[self.root(A)]

    # AとBをくっつける
    def Connect(self, A, B):
        if self.root(A) == self.root(B):
            return False
        if self.size(A) < self.size(B):
            self.Parents[self.root(B)] += self.Parents[A]
            self.Parents[self.root(A)] = B
        else:
            self.Parents[self.root(A)] += self.Parents[self.root(B)]
            self.Parents[self.root(B)] = A
        return True


n, m = map(int, input().split())
a = [input().split() for i in range(m)]
a = [(int(i[0]), int(i[1])) for i in a]
ans = [int(n * (n - 1) / 2) for i in range(m)]
uni = UnionFind(n)

for i in range(m):
    if i == 0:
        continue
    A = a[-i][0]
    B = a[-i][1]

    if uni.root(A) != uni.root(B):
        ans[i] = ans[i-1] - uni.size(A) * uni.size(B)
        uni.Connect(A, B)
    else:
        ans[i] = ans[i-1]


for i in range(1, m+1):
    print(ans[-i])


Fixed submission:
class UnionFind:
    Parents = None

    def __init__(self, N):
        self.Parents = [-1 for i in range(N + 1)]

    # Aがどのグループにいるのか調べる
    def root(self, A):
        if self.Parents[A] < 0:
            return A
        else:
            self.Parents[A] = self.root(self.Parents[A])
            return self.Parents[A]

    # Aが所属しているグループのサイズを返す
    def size(self, A):
        return -self.Parents[self.root(A)]

    # AとBをくっつける
    def Connect(self, A, B):
        if self.root(A) == self.root(B):
            return False
        if self.size(A) < self.size(B):
            self.Parents[self.root(B)] += self.Parents[self.root(A)]
            self.Parents[self.root(A)] = B
        else:
            self.Parents[self.root(A)] += self.Parents[self.root(B)]
            self.Parents[self.root(B)] = A
        return True


n, m = map(int, input().split())
a = [input().split() for i in range(m)]
a = [(int(i[0]), int(i[1])) for i in a]
ans = [int(n * (n - 1) / 2) for i in range(m)]
uni = UnionFind(n)

for i in range(m):
    if i == 0:
        continue
    A = a[-i][0]
    B = a[-i][1]

    if uni.root(A) != uni.root(B):
        ans[i] = ans[i-1] - uni.size(A) * uni.size(B)
        uni.Connect(A, B)
    else:
        ans[i] = ans[i-1]


for i in range(1, m+1):
    print(ans[-i])

Buggy python submission (verdict: Runtime Error):
class UnionFind:
    Parents = None

    def __init__(self, N):
        self.Parents = [-1 for i in range(N + 1)]

    # Aがどのグループにいるのか調べる
    def root(self, A):
        if self.Parents[A] < 0:
            return A
        else:
            self.Parents[A] = self.root(self.Parents[A])
            return self.Parents[A]

    # Aが所属しているグループのサイズを返す
    def size(self, A):
        return -self.Parents[self.root(A)]

    # AとBをくっつける
    def Connect(self, A, B):
        if self.root(A) == self.root(B):
            return False
        if self.size(A) < self.size(B):
            self.Parents[B] += self.Parents[A]
            self.Parents[A] = B
        else:
            self.Parents[A] += self.Parents[B]
            self.Parents[B] = A
        return True


n, m = map(int, input().split())
a = [input().split() for i in range(m)]
a = [(int(i[0]), int(i[1])) for i in a]
ans = [int(n * (n - 1) / 2) for i in range(m)]
uni = UnionFind(n)

for i in range(1, m):
    A = a[-i][0]
    B = a[-i][1]

    if uni.root(A) != uni.root(B):
        ans[i] = ans[i-1] - uni.size(A) * uni.size(B)
        uni.Connect(A, B)
    else:
        ans[i] = ans[i-1]


for i in range(1, m+1):
    print(ans[-i])


Fixed submission:
class UnionFind:
    Parents = None

    def __init__(self, N):
        self.Parents = [-1 for i in range(N + 1)]

    # Aがどのグループにいるのか調べる
    def root(self, A):
        if self.Parents[A] < 0:
            return A
        else:
            self.Parents[A] = self.root(self.Parents[A])
            return self.Parents[A]

    # Aが所属しているグループのサイズを返す
    def size(self, A):
        return -self.Parents[self.root(A)]

    # AとBをくっつける
    def Connect(self, A, B):
        if self.root(A) == self.root(B):
            return False
        if self.size(A) < self.size(B):
            self.Parents[self.root(B)] += self.Parents[self.root(A)]
            self.Parents[self.root(A)] = B
        else:
            self.Parents[self.root(A)] += self.Parents[self.root(B)]
            self.Parents[self.root(B)] = A
        return True


n, m = map(int, input().split())
a = [input().split() for i in range(m)]
a = [(int(i[0]), int(i[1])) for i in a]
ans = [int(n * (n - 1) / 2) for i in range(m)]
uni = UnionFind(n)

for i in range(m):
    if i == 0:
        continue
    A = a[-i][0]
    B = a[-i][1]

    if uni.root(A) != uni.root(B):
        ans[i] = ans[i-1] - uni.size(A) * uni.size(B)
        uni.Connect(A, B)
    else:
        ans[i] = ans[i-1]


for i in range(1, m+1):
    print(ans[-i])

Buggy python submission (verdict: Runtime Error):
class UnionFind:
    Parents = None

    def __init__(self, N):
        self.Parents = [-1 for i in range(N + 1)]

    # Aがどのグループにいるのか調べる
    def root(self, A):
        if self.Parents[A] < 0:
            return A
        else:
            self.Parents[A] = self.root(self.Parents[A])
            return self.Parents[A]

    # Aが所属しているグループのサイズを返す
    def size(self, A):
        return -self.Parents[self.root(A)]

    # AとBをくっつける
    def Connect(self, A, B):
        if self.root(A) == self.root(B):
            return False
        if self.size(A) < self.size(B):
            self.Parents[B] += self.Parents[A]
            self.Parents[A] = B
        else:
            self.Parents[A] += self.Parents[B]
            self.Parents[B] = A
        return True


n, m = map(int, input().split())
a = [input().split() for i in range(m)]
a = [(int(i[0]), int(i[1])) for i in a]
ans = [int(n * (n - 1) / 2) for i in range(m)]
uni = UnionFind(n)

for i in range(1, m):
    A = a[-i][0]
    B = a[-i][1]

    if uni.root(A) != uni.root(B):
        ans[-i-1] = ans[-i] - uni.size(A) * uni.size(B)
        uni.Connect(A, B)
    else:
        ans[-i - 1] = ans[-i]


for i in range(m):
    print(ans[i])


Fixed submission:
class UnionFind:
    Parents = None

    def __init__(self, N):
        self.Parents = [-1 for i in range(N + 1)]

    # Aがどのグループにいるのか調べる
    def root(self, A):
        if self.Parents[A] < 0:
            return A
        else:
            self.Parents[A] = self.root(self.Parents[A])
            return self.Parents[A]

    # Aが所属しているグループのサイズを返す
    def size(self, A):
        return -self.Parents[self.root(A)]

    # AとBをくっつける
    def Connect(self, A, B):
        if self.root(A) == self.root(B):
            return False
        if self.size(A) < self.size(B):
            self.Parents[self.root(B)] += self.Parents[self.root(A)]
            self.Parents[self.root(A)] = B
        else:
            self.Parents[self.root(A)] += self.Parents[self.root(B)]
            self.Parents[self.root(B)] = A
        return True


n, m = map(int, input().split())
a = [input().split() for i in range(m)]
a = [(int(i[0]), int(i[1])) for i in a]
ans = [int(n * (n - 1) / 2) for i in range(m)]
uni = UnionFind(n)

for i in range(m):
    if i == 0:
        continue
    A = a[-i][0]
    B = a[-i][1]

    if uni.root(A) != uni.root(B):
        ans[i] = ans[i-1] - uni.size(A) * uni.size(B)
        uni.Connect(A, B)
    else:
        ans[i] = ans[i-1]


for i in range(1, m+1):
    print(ans[-i])

Buggy python submission (verdict: Time Limit Exceeded):

def Calculate_inconveniece(group):
    if len(group) == 1:
        return 0
    else:
        Mysum = 0
        group_count = len(group)
        for i in range(group_count):
            num_group1 = len(group[i])
            for j in range(i + 1, group_count):
                Mysum += num_group1 * len(group[j])
        return Mysum


def Conect_island(index, group):
    right = index[0]
    left = index[1]
    right_index = [i for i in range(len(group)) if right in group[i]][0]
    left_index = [i for i in range(len(group)) if left in group[i]][0]
    if right_index == left_index:
        return group
    else:
        temp = group[right_index]
        group[left_index] += temp
        del(group[right_index])
        return group



n, m = map(int, input().split())
a = [input().split() for i in range(m)]
a = [(int(i[0]), int(i[1])) for i in a]
group = [[i]for i in range(1, n+1)]
ans =[]
for idnex in range(1, len(a) + 1):
    # print(Calculate_inconveniece(group))
    ans.append(Calculate_inconveniece(group))
    x = a[-idnex]
    group = Conect_island(x, group)
    # print(x)

for i in range(1, len(a) + 1):
    print(ans[-i])

Fixed submission:
class UnionFind:
    Parents = None

    def __init__(self, N):
        self.Parents = [-1 for i in range(N + 1)]

    # Aがどのグループにいるのか調べる
    def root(self, A):
        if self.Parents[A] < 0:
            return A
        else:
            self.Parents[A] = self.root(self.Parents[A])
            return self.Parents[A]

    # Aが所属しているグループのサイズを返す
    def size(self, A):
        return -self.Parents[self.root(A)]

    # AとBをくっつける
    def Connect(self, A, B):
        if self.root(A) == self.root(B):
            return False
        if self.size(A) < self.size(B):
            self.Parents[self.root(B)] += self.Parents[self.root(A)]
            self.Parents[self.root(A)] = B
        else:
            self.Parents[self.root(A)] += self.Parents[self.root(B)]
            self.Parents[self.root(B)] = A
        return True


n, m = map(int, input().split())
a = [input().split() for i in range(m)]
a = [(int(i[0]), int(i[1])) for i in a]
ans = [int(n * (n - 1) / 2) for i in range(m)]
uni = UnionFind(n)

for i in range(m):
    if i == 0:
        continue
    A = a[-i][0]
    B = a[-i][1]

    if uni.root(A) != uni.root(B):
        ans[i] = ans[i-1] - uni.size(A) * uni.size(B)
        uni.Connect(A, B)
    else:
        ans[i] = ans[i-1]


for i in range(1, m+1):
    print(ans[-i])

Buggy python submission (verdict: Time Limit Exceeded):
from queue import Queue


def disintegration_Bridge(index):
    ConnectMatrix[index[0]][index[1]] = '0'
    ConnectMatrix[index[1]][index[0]] = '0'


def Calculate_inconveniece(group):
    if len(group) == 1:
        return 0
    else:
        Mysum = 0
        group_count = len(group)
        for i in range(group_count):
            num_group1 = len(group[i])
            for j in range(i + 1, group_count):
                Mysum += num_group1 * len(group[j])
        return Mysum


n, m = map(int, input().split())
a = [input().split() for i in range(m)]
a = [(int(i[0]), int(i[1])) for i in a]
ConnectMatrix = [['0' for i in range(n + 1)] for j in range(n + 1)]

for bridge in a:
    ConnectMatrix[bridge[1]][bridge[0]] = '1'
    ConnectMatrix[bridge[0]][bridge[1]] = '1'

for i in a:
    # 橋の崩落
    disintegration_Bridge(i)
    mydic = ['0' for i in range(n + 1)]
    root = [[i for i in range(n + 1) if ConnectMatrix[j][i] == '1'] for j in range(n + 1)]
    group = []

    q = Queue()
    counter = 0
    for index in range(1, n + 1):
        if mydic[index] == '1':
            continue
        q.put(index)
        mydic[index] = '1'
        group.append([index])
        while not q.empty():
            focus = q.get()
            for j in root[focus]:
                if type(j) == int:
                    if mydic[j] == '0':
                        mydic[j] = '1'
                        group[counter].append(j)
                        q.put(j)
                else:
                    for node in j:
                        if mydic[node] == '0':
                            mydic[node] = '1'
                            q.put(node)
                            group[counter].append(j)
        counter += 1
    # print(group)
    print(Calculate_inconveniece(group))


Fixed submission:
class UnionFind:
    Parents = None

    def __init__(self, N):
        self.Parents = [-1 for i in range(N + 1)]

    # Aがどのグループにいるのか調べる
    def root(self, A):
        if self.Parents[A] < 0:
            return A
        else:
            self.Parents[A] = self.root(self.Parents[A])
            return self.Parents[A]

    # Aが所属しているグループのサイズを返す
    def size(self, A):
        return -self.Parents[self.root(A)]

    # AとBをくっつける
    def Connect(self, A, B):
        if self.root(A) == self.root(B):
            return False
        if self.size(A) < self.size(B):
            self.Parents[self.root(B)] += self.Parents[self.root(A)]
            self.Parents[self.root(A)] = B
        else:
            self.Parents[self.root(A)] += self.Parents[self.root(B)]
            self.Parents[self.root(B)] = A
        return True


n, m = map(int, input().split())
a = [input().split() for i in range(m)]
a = [(int(i[0]), int(i[1])) for i in a]
ans = [int(n * (n - 1) / 2) for i in range(m)]
uni = UnionFind(n)

for i in range(m):
    if i == 0:
        continue
    A = a[-i][0]
    B = a[-i][1]

    if uni.root(A) != uni.root(B):
        ans[i] = ans[i-1] - uni.size(A) * uni.size(B)
        uni.Connect(A, B)
    else:
        ans[i] = ans[i-1]


for i in range(1, m+1):
    print(ans[-i])

Buggy python submission (verdict: Time Limit Exceeded):
from queue import Queue
def  disintegration_Bridge(index):
    ConectMatrix[index[0]][index[1]] = '0'
    ConectMatrix[index[1]][index[0]] = '0'

def is_island(index):
    # indexから直接たどり着ける島リスト
    temp = [[i for i in range(n + 1) if ConectMatrix[j][i] == '1'] for j in range(n + 1)]
    mydic = ['0' for i in range(n+1)]
    q = Queue()
    q.put(index[0])
    while not q.empty():
        v = q.get()
        for j in temp[v]:
            if type(j) == int:
                if mydic[j] == '0':
                    mydic[j] = '1'
                    q.put(j)
            else:
                for node in j:
                    if mydic[node] == '0':
                        mydic[node] = '1'
                        q.put(node)

    if mydic[index[1]] == '1':
        return False
    else:
        return True

n, m = map(int, input().split())
a = [input().split() for i in range(m)]
a = [(int(i[0]), int(i[1])) for i in a]
ConectMatrix = [['0' for i in range(n + 1)] for j in range(n + 1)]

for brige in a:
    ConectMatrix[brige[1]][brige[0]] = '1'
    ConectMatrix[brige[0]][brige[1]] = '1'

for i in a:
    # print("-----{}----".format(i))
    # 橋の崩落
    disintegration_Bridge(i)
    # 直接繋がっていない島がある島を検索
    not_connect = [(i, j) for i in range(1, n + 1) for j in range(1, n + 1) if ConectMatrix[i][j] == '0' and i != j]
    [not_connect.remove((i[1], i[0])) for i in not_connect]
    counter = 0
    # print(not_connect)
    # 直接繋がっていないが迂回すればいけるか検索
    for index in not_connect:
        # AとBの島が迂回して繋がっているか判定
        # temp = [[i for i in range(n + 1) if ConectMatrix[j][i] == '1'] for j in range(n + 1)]
        # print(temp)
        if is_island(index):
            # print(index)
            counter = counter + 1
    print(counter)

Fixed submission:
class UnionFind:
    Parents = None

    def __init__(self, N):
        self.Parents = [-1 for i in range(N + 1)]

    # Aがどのグループにいるのか調べる
    def root(self, A):
        if self.Parents[A] < 0:
            return A
        else:
            self.Parents[A] = self.root(self.Parents[A])
            return self.Parents[A]

    # Aが所属しているグループのサイズを返す
    def size(self, A):
        return -self.Parents[self.root(A)]

    # AとBをくっつける
    def Connect(self, A, B):
        if self.root(A) == self.root(B):
            return False
        if self.size(A) < self.size(B):
            self.Parents[self.root(B)] += self.Parents[self.root(A)]
            self.Parents[self.root(A)] = B
        else:
            self.Parents[self.root(A)] += self.Parents[self.root(B)]
            self.Parents[self.root(B)] = A
        return True


n, m = map(int, input().split())
a = [input().split() for i in range(m)]
a = [(int(i[0]), int(i[1])) for i in a]
ans = [int(n * (n - 1) / 2) for i in range(m)]
uni = UnionFind(n)

for i in range(m):
    if i == 0:
        continue
    A = a[-i][0]
    B = a[-i][1]

    if uni.root(A) != uni.root(B):
        ans[i] = ans[i-1] - uni.size(A) * uni.size(B)
        uni.Connect(A, B)
    else:
        ans[i] = ans[i-1]


for i in range(1, m+1):
    print(ans[-i])

Buggy python submission (verdict: Time Limit Exceeded):
from queue import Queue
def  disintegration_Bridge(index):
    ConectMatrix[index[0]][index[1]] = '0'
    ConectMatrix[index[1]][index[0]] = '0'

def is_island(index):
    # indexから直接たどり着ける島リスト
    temp = [[i for i in range(n + 1) if ConectMatrix[j][i] == '1'] for j in range(n + 1)]
    mydic = ['0' for i in range(n+1)]
    q = Queue()
    q.put(index[0])
    while not q.empty():
        v = q.get()
        for j in temp[v]:
            if type(j) == int:
                if mydic[j] == '0':
                    mydic[j] = '1'
                    q.put(j)
            else:
                for node in j:
                    if mydic[node] == '0':
                        mydic[node] = '1'
                        q.put(node)

    if mydic[index[1]] == '1':
        return False
    else:
        return True

def find_root(index, mylist, mydic):
    mydic[index] = '1'
    candidate = mylist[index]
    temp = [j for j in range(1, n+1) if ConectMatrix[index][j] == '1']
    temp.append(mylist).set()
    for t in temp:
        if mydic[t] == '0':
            return find_root(temp)
    return temp

n, m = map(int, input().split())
a = [input().split() for i in range(m)]
a = [(int(i[0]), int(i[1])) for i in a]
ConectMatrix = [['0' for i in range(n + 1)] for j in range(n + 1)]

for brige in a:
    ConectMatrix[brige[1]][brige[0]] = '1'
    ConectMatrix[brige[0]][brige[1]] = '1'

for i in a:
    # print("-----{}----".format(i))
    # 橋の崩落
    disintegration_Bridge(i)
    # 直接繋がっていない島がある島を検索
    not_connect = [(i, j) for i in range(1, n + 1) for j in range(1, n + 1) if ConectMatrix[i][j] == '0' and i != j]
    [not_connect.remove((i[1], i[0])) for i in not_connect]
    counter = 0
    # print(not_connect)
    # 直接繋がっていないが迂回すればいけるか検索
    for index in not_connect:
        # AとBの島が迂回して繋がっているか判定
        temp = [[i for i in range(n + 1) if ConectMatrix[j][i] == '1'] for j in range(n + 1)]
        # print(temp)
        if is_island(index):
            # print(index)
            counter = counter + 1        


    print(counter)

Fixed submission:
class UnionFind:
    Parents = None

    def __init__(self, N):
        self.Parents = [-1 for i in range(N + 1)]

    # Aがどのグループにいるのか調べる
    def root(self, A):
        if self.Parents[A] < 0:
            return A
        else:
            self.Parents[A] = self.root(self.Parents[A])
            return self.Parents[A]

    # Aが所属しているグループのサイズを返す
    def size(self, A):
        return -self.Parents[self.root(A)]

    # AとBをくっつける
    def Connect(self, A, B):
        if self.root(A) == self.root(B):
            return False
        if self.size(A) < self.size(B):
            self.Parents[self.root(B)] += self.Parents[self.root(A)]
            self.Parents[self.root(A)] = B
        else:
            self.Parents[self.root(A)] += self.Parents[self.root(B)]
            self.Parents[self.root(B)] = A
        return True


n, m = map(int, input().split())
a = [input().split() for i in range(m)]
a = [(int(i[0]), int(i[1])) for i in a]
ans = [int(n * (n - 1) / 2) for i in range(m)]
uni = UnionFind(n)

for i in range(m):
    if i == 0:
        continue
    A = a[-i][0]
    B = a[-i][1]

    if uni.root(A) != uni.root(B):
        ans[i] = ans[i-1] - uni.size(A) * uni.size(B)
        uni.Connect(A, B)
    else:
        ans[i] = ans[i-1]


for i in range(1, m+1):
    print(ans[-i])