Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
AB = list( list(map(int, input().split())) for _ in range(M) )

nC2 = [ i * (i - 1) // 2 for i in range(N+1) ]

class UnionFind():
    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 = 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 nC2_group_members(self):
      return sum([ nC2[len(self.members(r))] for r in self.roots() ])
      
    def count_group_members(self):
      return [ len(self.members(r)) for r in self.roots() ]

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
  
  
'''def nC2(n):
  if n < 2:
    return 0
  else:
    return n * (n - 1) // 2'''
  
ans_lst = [nC2[N]] * (M+1)
uf = UnionFind(N)
cnt = 1
for a,b in AB[::-1]:
  uf.union(a-1,b-1)
  ans_lst[cnt] -= uf.nC2_group_members()
  cnt += 1

for m in range(1,M+1):
  print(ans_lst[M-m])

Fixed submission:
N, M = map(int, input().split())
AB = list( list(map(int, input().split())) for _ in range(M) )

class UnionFind():
    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 = 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())

  
ans_lst = [0] * (M+1)
ans_lst[0] = N * (N - 1) // 2

uf = UnionFind(N)
cnt = 1
for a,b in AB[::-1]:
  if uf.same(a-1, b-1):
    ans_lst[cnt] = ans_lst[cnt-1]
    #print(uf.all_group_members())
  else:
    #print(uf.size(a-1), uf.size(b-1))
    ans_lst[cnt] = ans_lst[cnt-1] - uf.size(a-1) * uf.size(b-1)
    uf.union(a-1,b-1)
    #print(uf.all_group_members())
  cnt += 1

for m in range(1,M+1):
  print(ans_lst[M-m])

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
AB = list( list(map(int, input().split())) for _ in range(M) )

nC2 = [ i * (i - 1) // 2 for i in range(N+1) ]

class UnionFind():
    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 = 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 nC2_group_members(self):
      return sum([ nC2[len(self.members(r))] for r in self.roots() ])
      
    def count_group_members(self):
      return [ len(self.members(r)) for r in self.roots() ]

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
  
  
'''def nC2(n):
  if n < 2:
    return 0
  else:
    return n * (n - 1) // 2'''
  
ans_lst = [nC2[N]] * (M+1)
uf = UnionFind(N)
cnt = 1
for a,b in AB[::-1]:
  uf.union(a-1,b-1)
  ans_lst[cnt] -= uf.nC2_group_members()
  cnt += 1

for m in range(1,M+1):
  print(ans_lst[M-m])

Fixed submission:
N, M = map(int, input().split())
AB = list( list(map(int, input().split())) for _ in range(M) )

class UnionFind():
    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 = 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())

  
ans_lst = [0] * (M+1)
ans_lst[0] = N * (N - 1) // 2

uf = UnionFind(N)
cnt = 1
for a,b in AB[::-1]:
  if uf.same(a-1, b-1):
    ans_lst[cnt] = ans_lst[cnt-1]
    #print(uf.all_group_members())
  else:
    #print(uf.size(a-1), uf.size(b-1))
    ans_lst[cnt] = ans_lst[cnt-1] - uf.size(a-1) * uf.size(b-1)
    uf.union(a-1,b-1)
    #print(uf.all_group_members())
  cnt += 1

for m in range(1,M+1):
  print(ans_lst[M-m])

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
AB = list( list(map(int, input().split())) for _ in range(M) )

class UnionFind():
    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 = 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 count_group_members(self):
      return [ len(self.members(r)) for r in self.roots() ]

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
  
  
def nC2(n):
  if n < 2:
    return 0
  else:
    return n * (n - 1) // 2
  
  
ans_lst = [nC2(N)] * (M+1)
uf = UnionFind(N)
cnt = 1
for a,b in AB[::-1]:
  uf.union(a-1,b-1)
  ans_lst[cnt] -= sum([ nC2(i) for i in uf.count_group_members() ])
  cnt += 1

for m in range(1,M+1):
  print(ans_lst[M-m])

Fixed submission:
N, M = map(int, input().split())
AB = list( list(map(int, input().split())) for _ in range(M) )

class UnionFind():
    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 = 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())

  
ans_lst = [0] * (M+1)
ans_lst[0] = N * (N - 1) // 2

uf = UnionFind(N)
cnt = 1
for a,b in AB[::-1]:
  if uf.same(a-1, b-1):
    ans_lst[cnt] = ans_lst[cnt-1]
    #print(uf.all_group_members())
  else:
    #print(uf.size(a-1), uf.size(b-1))
    ans_lst[cnt] = ans_lst[cnt-1] - uf.size(a-1) * uf.size(b-1)
    uf.union(a-1,b-1)
    #print(uf.all_group_members())
  cnt += 1

for m in range(1,M+1):
  print(ans_lst[M-m])