Buggy python submission (verdict: Time Limit Exceeded):
# /////[UnionFind準備]//////////////////////////////////////////////////////
class UnionFind: #引数は頂点の数、1インデックス
    def __init__(self, n):
        self.parents = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.parents[x] == x:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.parents[x] = y
        else:
            self.parents[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

# /////[main]]//////////////////////////////////////////////////////
if __name__ == "__main__":
    N, M = map(int, input().split())
    bridges = [tuple(map(int, input().split())) for _ in range(M)]

    answer_list = []
    initial = N * (N-1) // 2
    answer_list.append(initial)
    # すべて崩落したところから逆に調べていく
    union_find = UnionFind(N)
    for i in range(M-1, 0, -1):
        A, B = bridges[i]
        union_find.union(A, B)
    
        # それぞれの島の集合にいくつの島が所属しているか辞書型に格納
        count_dict = {}
        for j in range(1, N+1): # unionfindは1インデックスなので
            parent = union_find.find(j)
            if parent in count_dict:
                count_dict[parent] += 1
            else:
                count_dict[parent] = 1
        # 存在する集合の数をもとに不便さを計算する 3:2:2:1の場合は3*(8-3)+2*(8-5)+2*(8-7)+1*(8-8)となる
        value_count = 0
        inconvenient = 0
        for k, v in count_dict.items():
            value_count += v
            inconvenient += v * (N - value_count)

        answer_list.append(inconvenient)

    len_answer = len(answer_list)
    for i in range(len_answer-1, -1, -1):
        print(answer_list[i])




Fixed submission:
# /////[UnionFind準備]//////////////////////////////////////////////////////
class UnionFind(): # 0インデックス
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        '''
        xとyは-=1して使う
        '''
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

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

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# /////[main]]//////////////////////////////////////////////////////
if __name__ == "__main__":
    N, M = map(int, input().split())
    bridges = [tuple(map(int, input().split())) for _ in range(M)]

    answer_list = []
    total = N * (N-1) // 2
    answer_list.append(total)
    # すべて崩落したところから逆に調べていく
    union_find = UnionFind(N)
    for i in range(M-1, 0, -1):
        A, B = bridges[i]
        A -= 1
        B -= 1
        if not union_find.same(A, B):
            total -= union_find.size(A) * union_find.size(B)
            union_find.union(A, B)

        answer_list.append(total)

    len_answer = len(answer_list)
    for i in range(len_answer-1, -1, -1):
        print(answer_list[i])

Buggy python submission (verdict: Time Limit Exceeded):
# /////[UnionFind準備]//////////////////////////////////////////////////////
class UnionFind: #引数は頂点の数、1インデックス
    def __init__(self, n):
        self.parents = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.parents[x] == x:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.parents[x] = y
        else:
            self.parents[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

# /////[main]]//////////////////////////////////////////////////////
if __name__ == "__main__":
    N, M = map(int, input().split())
    bridges = [tuple(map(int, input().split())) for _ in range(M)]

    answer_list = []
    initial = N * (N-1) // 2
    answer_list.append(initial)
    # すべて崩落したところから逆に調べていく
    union_find = UnionFind(N)
    for i in range(M-1, 0, -1):
        A, B = bridges[i]
        union_find.union(A, B)
    
        # それぞれの島の集合にいくつの島が所属しているか辞書型に格納
        count_dict = {}
        for j in range(1, N+1): # unionfindは1インデックスなので
            parent = union_find.find(j)
            if parent in count_dict:
                count_dict[parent] += 1
            else:
                count_dict[parent] = 1
        # 存在する集合の数をもとに不便さを計算する 3:2:2:1の場合は3*(8-3)+2*(8-5)+2*(8-7)+1*(8-8)となる
        value_count = 0
        inconvenient = 0
        for k, v in count_dict.items():
            value_count += v
            inconvenient += v * (N - value_count)

        answer_list.append(inconvenient)

    len_answer = len(answer_list)
    for i in range(len_answer-1, -1, -1):
        print(answer_list[i])




Fixed submission:
# /////[UnionFind準備]//////////////////////////////////////////////////////
class UnionFind(): # 0インデックス
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        '''
        xとyは-=1して使う
        '''
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

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

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# /////[main]]//////////////////////////////////////////////////////
if __name__ == "__main__":
    N, M = map(int, input().split())
    bridges = [tuple(map(int, input().split())) for _ in range(M)]

    answer_list = []
    total = N * (N-1) // 2
    answer_list.append(total)
    # すべて崩落したところから逆に調べていく
    union_find = UnionFind(N)
    for i in range(M-1, 0, -1):
        A, B = bridges[i]
        A -= 1
        B -= 1
        if not union_find.same(A, B):
            total -= union_find.size(A) * union_find.size(B)
            union_find.union(A, B)

        answer_list.append(total)

    len_answer = len(answer_list)
    for i in range(len_answer-1, -1, -1):
        print(answer_list[i])

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin

# /////[UnionFind準備]//////////////////////////////////////////////////////
class UnionFind: #引数は頂点の数、1インデックス
    def __init__(self, n):
        self.parents = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.parents[x] == x:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.parents[x] = y
        else:
            self.parents[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

def main(N, M, bridges):
    answer_list = []
    # 崩落する橋の数をiとして増やしていく
    for i in range(M):
        # 崩落後の島についてunionfindする
        union_find = UnionFind(N)
        for j in range(i+1, M):
            A, B = bridges[j]
            union_find.union(A, B)
        
        # それぞれの島の集合にいくつの島が所属しているか辞書型に格納
        count_dict = {}
        for j in range(1, N+1): # unionfindは1インデックスなので
            parent = union_find.find(j)
            if parent in count_dict:
                count_dict[parent] += 1
            else:
                count_dict[parent] = 1
        # 存在する集合の数をもとに不便さを計算する 3:2:2:1の場合は3*(8-3)+2*(8-5)+2*(8-7)+1*(8-8)となる
        value_count = 0
        inconvenient = 0
        for v in count_dict.values():
            value_count += v
            inconvenient += v * (N - value_count)
 
        answer_list.append(inconvenient)
    return answer_list


# /////[main]]//////////////////////////////////////////////////////
if __name__ == "__main__":
    N, M = map(int, input().split())
    bridges = [tuple(map(int, stdin.readline().split())) for _ in range(M)]
    answer_list = main(N, M, bridges)
    
    for answer in answer_list:
        print(answer)

Fixed submission:
# /////[UnionFind準備]//////////////////////////////////////////////////////
class UnionFind(): # 0インデックス
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        '''
        xとyは-=1して使う
        '''
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

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

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# /////[main]]//////////////////////////////////////////////////////
if __name__ == "__main__":
    N, M = map(int, input().split())
    bridges = [tuple(map(int, input().split())) for _ in range(M)]

    answer_list = []
    total = N * (N-1) // 2
    answer_list.append(total)
    # すべて崩落したところから逆に調べていく
    union_find = UnionFind(N)
    for i in range(M-1, 0, -1):
        A, B = bridges[i]
        A -= 1
        B -= 1
        if not union_find.same(A, B):
            total -= union_find.size(A) * union_find.size(B)
            union_find.union(A, B)

        answer_list.append(total)

    len_answer = len(answer_list)
    for i in range(len_answer-1, -1, -1):
        print(answer_list[i])

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin

# /////[UnionFind準備]//////////////////////////////////////////////////////
class UnionFind: #引数は頂点の数、1インデックス
    def __init__(self, n):
        self.parents = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.parents[x] == x:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.parents[x] = y
        else:
            self.parents[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

def main(N, M, bridges):
    answer_list = []
    # 崩落する橋の数をiとして増やしていく
    for i in range(M):
        # 崩落後の島についてunionfindする
        union_find = UnionFind(N)
        for j in range(i+1, M):
            A, B = bridges[j]
            union_find.union(A, B)
        
        # それぞれの島の集合にいくつの島が所属しているか辞書型に格納
        count_dict = {}
        for j in range(1, N+1): # unionfindは1インデックスなので
            parent = union_find.find(j)
            if parent in count_dict:
                count_dict[parent] += 1
            else:
                count_dict[parent] = 1
        # 存在する集合の数をもとに不便さを計算する 3:2:2:1の場合は3*(8-3)+2*(8-5)+2*(8-7)+1*(8-8)となる
        value_count = 0
        inconvenient = 0
        for v in count_dict.values():
            value_count += v
            inconvenient += v * (N - value_count)
 
        answer_list.append(inconvenient)
    return answer_list


# /////[main]]//////////////////////////////////////////////////////
if __name__ == "__main__":
    N, M = map(int, input().split())
    bridges = [tuple(map(int, stdin.readline().split())) for _ in range(M)]
    answer_list = main(N, M, bridges)
    
    for answer in answer_list:
        print(answer)

Fixed submission:
# /////[UnionFind準備]//////////////////////////////////////////////////////
class UnionFind(): # 0インデックス
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        '''
        xとyは-=1して使う
        '''
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

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

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# /////[main]]//////////////////////////////////////////////////////
if __name__ == "__main__":
    N, M = map(int, input().split())
    bridges = [tuple(map(int, input().split())) for _ in range(M)]

    answer_list = []
    total = N * (N-1) // 2
    answer_list.append(total)
    # すべて崩落したところから逆に調べていく
    union_find = UnionFind(N)
    for i in range(M-1, 0, -1):
        A, B = bridges[i]
        A -= 1
        B -= 1
        if not union_find.same(A, B):
            total -= union_find.size(A) * union_find.size(B)
            union_find.union(A, B)

        answer_list.append(total)

    len_answer = len(answer_list)
    for i in range(len_answer-1, -1, -1):
        print(answer_list[i])

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin

# /////[UnionFind準備]//////////////////////////////////////////////////////
class UnionFind: #引数は頂点の数、1インデックス
    def __init__(self, n):
        self.parents = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.parents[x] == x:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.parents[x] = y
        else:
            self.parents[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

def main(N, M, bridges):
    answer_list = []
    # 崩落する橋の数をiとして増やしていく
    for i in range(M):
        # 崩落後の島についてunionfindする
        union_find = UnionFind(N)
        for j in range(i+1, M):
            A, B = bridges[j]
            union_find.union(A, B)
        
        # それぞれの島の集合にいくつの島が所属しているか辞書型に格納
        count_dict = {}
        for j in range(1, N+1): # unionfindは1インデックスなので
            parent = union_find.find(j)
            if parent in count_dict:
                count_dict[parent] += 1
            else:
                count_dict[parent] = 1
        # 存在する集合の数をもとに不便さを計算する 3:2:2:1の場合は3*(8-3)+2*(8-5)+2*(8-7)+1*(8-8)となる
        value_count = 0
        inconvenient = 0
        for k, v in count_dict.items():
            value_count += v
            inconvenient += v * (N - value_count)
 
        answer_list.append(inconvenient)
    return answer_list


# /////[main]]//////////////////////////////////////////////////////
if __name__ == "__main__":
    N, M = map(int, input().split())
    bridges = [tuple(map(int, stdin.readline().split())) for _ in range(M)]
    answer_list = main(N, M, bridges)
    
    for answer in answer_list:
        print(answer)

Fixed submission:
# /////[UnionFind準備]//////////////////////////////////////////////////////
class UnionFind(): # 0インデックス
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        '''
        xとyは-=1して使う
        '''
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

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

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# /////[main]]//////////////////////////////////////////////////////
if __name__ == "__main__":
    N, M = map(int, input().split())
    bridges = [tuple(map(int, input().split())) for _ in range(M)]

    answer_list = []
    total = N * (N-1) // 2
    answer_list.append(total)
    # すべて崩落したところから逆に調べていく
    union_find = UnionFind(N)
    for i in range(M-1, 0, -1):
        A, B = bridges[i]
        A -= 1
        B -= 1
        if not union_find.same(A, B):
            total -= union_find.size(A) * union_find.size(B)
            union_find.union(A, B)

        answer_list.append(total)

    len_answer = len(answer_list)
    for i in range(len_answer-1, -1, -1):
        print(answer_list[i])

Buggy python submission (verdict: Time Limit Exceeded):
# /////[UnionFind準備]//////////////////////////////////////////////////////
class UnionFind: #引数は頂点の数、1インデックス
    def __init__(self, n):
        self.parents = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.parents[x] == x:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.parents[x] = y
        else:
            self.parents[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

def main(N, M, bridges):
    answer_list = []
    # 崩落する橋の数をiとして増やしていく
    for i in range(M):
        # 崩落後の島についてunionfindする
        union_find = UnionFind(N)
        for j in range(i+1, M):
            A, B = bridges[j]
            union_find.union(A, B)
        
        # それぞれの島の集合にいくつの島が所属しているか辞書型に格納
        count_dict = {}
        for j in range(1, N+1): # unionfindは1インデックスなので
            parent = union_find.find(j)
            if parent in count_dict:
                count_dict[parent] += 1
            else:
                count_dict[parent] = 1
        # 存在する集合の数をもとに不便さを計算する 3:2:2:1の場合は3*(8-3)+2*(8-5)+2*(8-7)+1*(8-8)となる
        value_count = 0
        inconvenient = 0
        for k, v in count_dict.items():
            value_count += v
            inconvenient += v * (N - value_count)
 
        answer_list.append(inconvenient)
    return answer_list


# /////[main]]//////////////////////////////////////////////////////
if __name__ == "__main__":
    N, M = map(int, input().split())
    bridges = [tuple(map(int, input().split())) for _ in range(M)]
    answer_list = main(N, M, bridges)
    
    for answer in answer_list:
        print(answer)

Fixed submission:
# /////[UnionFind準備]//////////////////////////////////////////////////////
class UnionFind(): # 0インデックス
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        '''
        xとyは-=1して使う
        '''
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

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

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# /////[main]]//////////////////////////////////////////////////////
if __name__ == "__main__":
    N, M = map(int, input().split())
    bridges = [tuple(map(int, input().split())) for _ in range(M)]

    answer_list = []
    total = N * (N-1) // 2
    answer_list.append(total)
    # すべて崩落したところから逆に調べていく
    union_find = UnionFind(N)
    for i in range(M-1, 0, -1):
        A, B = bridges[i]
        A -= 1
        B -= 1
        if not union_find.same(A, B):
            total -= union_find.size(A) * union_find.size(B)
            union_find.union(A, B)

        answer_list.append(total)

    len_answer = len(answer_list)
    for i in range(len_answer-1, -1, -1):
        print(answer_list[i])

Buggy python submission (verdict: Time Limit Exceeded):
# /////[UnionFind準備]//////////////////////////////////////////////////////
class UnionFind: #引数は頂点の数、1インデックス
    def __init__(self, n):
        self.parents = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.parents[x] == x:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.parents[x] = y
        else:
            self.parents[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

# /////[main]]//////////////////////////////////////////////////////
if __name__ == "__main__":
    N, M = map(int, input().split())
    bridges = [tuple(map(int, input().split())) for _ in range(M)]

    answer_list = []
    # 崩落する橋の数をiとして増やしていく
    for i in range(M):
        # 崩落後の島についてunionfindする
        union_find = UnionFind(N)
        for j in range(i+1, M):
            A, B = bridges[j]
            union_find.union(A, B)
        
        # それぞれの島の集合にいくつの島が所属しているか辞書型に格納
        count_dict = {}
        for j in range(1, N+1): # unionfindは1インデックスなので
            parent = union_find.find(j)
            if parent in count_dict:
                count_dict[parent] += 1
            else:
                count_dict[parent] = 1
        # 存在する集合の数をもとに不便さを計算する 3:2:2:1の場合は3*(8-3)+2*(8-5)+2*(8-7)+1*(8-8)となる
        value_count = 0
        inconvenient = 0
        for k, v in count_dict.items():
            value_count += v
            inconvenient += v * (N - value_count)
 
        answer_list.append(inconvenient)

    for answer in answer_list:
        print(answer)






Fixed submission:
# /////[UnionFind準備]//////////////////////////////////////////////////////
class UnionFind(): # 0インデックス
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        '''
        xとyは-=1して使う
        '''
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

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

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# /////[main]]//////////////////////////////////////////////////////
if __name__ == "__main__":
    N, M = map(int, input().split())
    bridges = [tuple(map(int, input().split())) for _ in range(M)]

    answer_list = []
    total = N * (N-1) // 2
    answer_list.append(total)
    # すべて崩落したところから逆に調べていく
    union_find = UnionFind(N)
    for i in range(M-1, 0, -1):
        A, B = bridges[i]
        A -= 1
        B -= 1
        if not union_find.same(A, B):
            total -= union_find.size(A) * union_find.size(B)
            union_find.union(A, B)

        answer_list.append(total)

    len_answer = len(answer_list)
    for i in range(len_answer-1, -1, -1):
        print(answer_list[i])

Buggy python submission (verdict: Time Limit Exceeded):
# /////[UnionFind準備]//////////////////////////////////////////////////////
class UnionFind: #引数は頂点の数、1インデックス
    def __init__(self, n):
        self.parents = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.parents[x] == x:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.parents[x] = y
        else:
            self.parents[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

# /////[main]]//////////////////////////////////////////////////////
if __name__ == "__main__":
    N, M = map(int, input().split())
    bridges = [tuple(map(int, input().split())) for _ in range(M)]

    answer_list = []
    # 崩落する橋の数をiとして増やしていく
    for i in range(M):
        # 崩落後の島についてunionfindする
        union_find = UnionFind(N)
        for j in range(i+1, M):
            A, B = bridges[j]
            union_find.union(A, B)
        
        # それぞれの島の集合にいくつの島が所属しているか辞書型に格納
        count_dict = {}
        for j in range(1, N+1): # unionfindは1インデックスなので
            parent = union_find.find(j)
            if parent in count_dict:
                count_dict[parent] += 1
            else:
                count_dict[parent] = 1
        # 存在する集合の数をもとに不便さを計算する 3:2:2:1の場合は3*(8-3)+2*(8-5)+2*(8-7)+1*(8-8)となる
        value_count = 0
        inconvenient = 0
        for k, v in count_dict.items():
            value_count += v
            inconvenient += v * (N - value_count)
 
        answer_list.append(inconvenient)

    for answer in answer_list:
        print(answer)






Fixed submission:
# /////[UnionFind準備]//////////////////////////////////////////////////////
class UnionFind(): # 0インデックス
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        '''
        xとyは-=1して使う
        '''
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

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

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# /////[main]]//////////////////////////////////////////////////////
if __name__ == "__main__":
    N, M = map(int, input().split())
    bridges = [tuple(map(int, input().split())) for _ in range(M)]

    answer_list = []
    total = N * (N-1) // 2
    answer_list.append(total)
    # すべて崩落したところから逆に調べていく
    union_find = UnionFind(N)
    for i in range(M-1, 0, -1):
        A, B = bridges[i]
        A -= 1
        B -= 1
        if not union_find.same(A, B):
            total -= union_find.size(A) * union_find.size(B)
            union_find.union(A, B)

        answer_list.append(total)

    len_answer = len(answer_list)
    for i in range(len_answer-1, -1, -1):
        print(answer_list[i])