Buggy python submission (verdict: Runtime Error):
N,M = [int(x) for x in input().split()]
map = [[0 for j in range(N+1)] for i in range(N+1)]
li_e = []
dp = [0]*(M+1)

def dfs(v,li_gr,flg):
    if(not flg[v]):
        flg[v] = True
        li_gr.append(v)
        for u,exist in enumerate(map[v]):
            if(exist == 1 and not flg[u]):
                dfs(u,li_gr,flg)

for i in range(M):
    A,B = [int(x) for x in input().split()]
    map[A][B] = 1
    map[B][A] = 1
    li_e.append((A,B))
dp[0] = [x for x in range(1,N+1)]

for i in range(M):
    flg = [True]*(N+1)
    dp[i+1] = dp[i]
    A = li_e[i][0]
    B = li_e[i][1]
    map[A][B] = 0
    map[B][A] = 0
    for n,p in enumerate(li_gr):
        if(A in p):
            for q in p:
                flg[q] = False
            dp[i+1].pop(n)
    li = []
    li.append([])
    dfs(A,li[0],flg)
    if(not flg[B]):
        li.append([])
        dfs(B,li[1],flg)
    dp[i+1].append(li)
    ans = 0
    grps = len(dp[i+1])
    for p in range(grps):
        for q in range(p+1,grps):
            ans += len(dp[i+1][p])*len(dp[i+1][q])
    print(ans)


Fixed submission:
import sys
#sys.setrecursionlimit(1000000)

class UnionFind():
    def __init__(self,n):
        self.parent = [-1 for _ in range(n)]

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

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

    def unite(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if(x == y):
            return False
        if(self.size(x)<self.size(y)):
            self.parent[y]+=self.parent[x]
            self.parent[x]=y
        else:
            self.parent[x]+=self.parent[y]
            self.parent[y]=x
        return True

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

N,M = [int(x) for x in input().split()]
A = [0]*M
B = [0]*M
ans = [0]*M
for i in range(M):
    A[i],B[i] = [int(x) for x in input().split()]
ans[M-1] = int(N*(N-1)/2)
uni = UnionFind(N)
for i in range(M-1,0,-1):
    ans[i-1]=ans[i]
    if(not uni.same(A[i]-1,B[i]-1)):
        ans[i-1]-=uni.size(A[i]-1)*uni.size(B[i]-1)
        uni.unite(A[i]-1,B[i]-1)
for i in ans:
    print(i)

Buggy python submission (verdict: Time Limit Exceeded):
N,M = [int(x) for x in input().split()]
map = [[0 for j in range(N+1)] for i in range(N+1)]
li_e = []

def dfs(v,li_gr,flg):
    if(not flg[v]):
        flg[v] = True
        li_gr.append(v)
        for u,exist in enumerate(map[v]):
            if(exist == 1 and not flg[u]):
                dfs(u,li_gr,flg)

for i in range(M):
    A,B = [int(x) for x in input().split()]
    map[A][B] = 1
    map[B][A] = 1
    li_e.append((A,B))

for i in range(M):
    flg = [False]*(N+1)
    li_gr = []
    A = li_e[i][0]
    B = li_e[i][1]
    map[A][B] = 0
    map[B][A] = 0
    for j in range(1,N+1):
        if(not flg[j]):
            li_gr.append([])
            dfs(j,li_gr[-1],flg)
    ans = 0
    grps = len(li_gr)
    for p in range(grps):
        for q in range(p+1,grps):
            ans += len(li_gr[p])*len(li_gr[q])
    print(ans)


Fixed submission:
import sys
#sys.setrecursionlimit(1000000)

class UnionFind():
    def __init__(self,n):
        self.parent = [-1 for _ in range(n)]

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

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

    def unite(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if(x == y):
            return False
        if(self.size(x)<self.size(y)):
            self.parent[y]+=self.parent[x]
            self.parent[x]=y
        else:
            self.parent[x]+=self.parent[y]
            self.parent[y]=x
        return True

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

N,M = [int(x) for x in input().split()]
A = [0]*M
B = [0]*M
ans = [0]*M
for i in range(M):
    A[i],B[i] = [int(x) for x in input().split()]
ans[M-1] = int(N*(N-1)/2)
uni = UnionFind(N)
for i in range(M-1,0,-1):
    ans[i-1]=ans[i]
    if(not uni.same(A[i]-1,B[i]-1)):
        ans[i-1]-=uni.size(A[i]-1)*uni.size(B[i]-1)
        uni.unite(A[i]-1,B[i]-1)
for i in ans:
    print(i)

Buggy python submission (verdict: Time Limit Exceeded):
N,M = [int(x) for x in input().split()]
map = [[0 for j in range(N+1)] for i in range(N+1)]
li_e = []

def dfs(v,li_gr,flg):
    if(not flg[v]):
        flg[v] = True
        li_gr.append(v)
        for u,exist in enumerate(map[v]):
            if(exist == 1 and not flg[u]):
                dfs(u,li_gr,flg)

for i in range(M):
    A,B = [int(x) for x in input().split()]
    map[A][B] = 1
    map[B][A] = 1
    li_e.append((A,B))

for i in range(M):
    flg = [False]*(N+1)
    li_gr = []
    A = li_e[i][0]
    B = li_e[i][1]
    map[A][B] = 0
    map[B][A] = 0
    for j in range(1,N+1):
        if(not flg[j]):
            li_gr.append([])
            dfs(j,li_gr[-1],flg)
    ans = 0
    grps = len(li_gr)
    for p in range(grps):
        for q in range(p+1,grps):
            ans += len(li_gr[p])*len(li_gr[q])
    print(ans)


Fixed submission:
import sys
#sys.setrecursionlimit(1000000)

class UnionFind():
    def __init__(self,n):
        self.parent = [-1 for _ in range(n)]

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

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

    def unite(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if(x == y):
            return False
        if(self.size(x)<self.size(y)):
            self.parent[y]+=self.parent[x]
            self.parent[x]=y
        else:
            self.parent[x]+=self.parent[y]
            self.parent[y]=x
        return True

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

N,M = [int(x) for x in input().split()]
A = [0]*M
B = [0]*M
ans = [0]*M
for i in range(M):
    A[i],B[i] = [int(x) for x in input().split()]
ans[M-1] = int(N*(N-1)/2)
uni = UnionFind(N)
for i in range(M-1,0,-1):
    ans[i-1]=ans[i]
    if(not uni.same(A[i]-1,B[i]-1)):
        ans[i-1]-=uni.size(A[i]-1)*uni.size(B[i]-1)
        uni.unite(A[i]-1,B[i]-1)
for i in ans:
    print(i)