Problem p03108 (python)

Fixed submission:
import sys
sys.setrecursionlimit(100000)
input = sys.stdin.readline
n,m=map(int,input().split())
z=n*(n-1)//2
l=[-1]*n
a=[1]*n
b=[input().split() for _ in range(m)]
c=[0]*m+[z]
def r(s):
  t=l[s]
  if t<0:
    return s
  else:
    l[s]=r(t)
    return l[s]
def f(s):
  return r(int(s)-1)
for i in range(m-1,-1,-1):
  x,y=map(f,b[i])
  if x!=y:
    l[x]=y
    z-=a[x]*a[y]
    a[y]+=a[x]
  c[i]=z
for x in c[1:]:
  print(x)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000)
input=sys.stdin.readline
n,m=map(int,input().split())
l=[1]*n
z=n*(n-1)//2
input()
m-=1
b=[input().split() for _ in range(m)]
c=[0]*m+[z]
def r(s):
  t=l[s]
  if t>0:
    return s
  else:
    l[s]=r(t)
    return l[s]
def f(s):
  return r(int(s)-1)
for i in range(m-1,-1,-1):
  x,y=map(f,b[i])
  if x!=y:
    s,t=l[x],l[y]
    z-=s*t
    if s<t:
      l[x]=-y
      l[y]+=s
    else:
      l[y]=-x
      l[x]+=t
  c[i]=z
for x in c:
  print(x)

Buggy submission (verdict: Runtime Error):
n,m=map(int,input().split())
ans=(n*(n-1))//2
l=[-1]*n
a=[1]*n
b=[input().split() for _ in range(m)]
c=[0]*m+[ans]
def r(s):
  t=l[s]
  return s if t<0 else r(t)
def f(s):
  return r(int(s)-1)
for i in range(m-1,-1,-1):
  x,y=map(f,b[i])
  if x!=y:
    l[x]=y
    ans-=a[x]*a[y]
    a[y]+=a[x]
  c[i]=ans
for x in c[1:]:
  print(x)

Buggy submission (verdict: Runtime Error):
n,m=map(int,input().split())
ans=n*(n-1)/2
l=[-1]*n
a=[1]*n
b=[input().split() for _ in range(m)]
c=[0]*m+[ans]
def r(s):
  t=l[s]
  return s if t<0 else r(t)
def f(s):
  return r(int(s)-1)
for i in range(m-1,-1,-1):
  x,y=map(f,b[i])
  if x!=y:
    l[x]=y
    ans-=a[x]*a[y]
    a[y]+=a[x]
  c[i]=ans
for x in c[1:]:
  print(x)

Buggy submission (verdict: Runtime Error):
def root(s):
  t=l[s]
  if t<0:
    return s
  return root(t)
def f(s):
  return root(int(s)-1)
n,m=map(int,input().split())
ans=n*(n-1)//2
l=[-1]*n
a=[1]*n
b=[input().split() for _ in range(m)]
c=[0]*m+[ans]
for i in range(m-1,-1,-1):
  x,y=map(f,b[i])
  if x!=y:
    l[x]=y
    ans-=a[x]*a[y]
    a[y]+=a[x]
  c[i]=ans
for x in c[1:]:
  print(x)

Buggy submission (verdict: Runtime Error):
def root(s):
  t=l[s]
  if t<0:
    return s
  return root(t)
def f(s):
  return root(int(s)-1)
n,m=map(int,input().split())
ans=n*(n-1)//2
l=[-1]*n
a=[1]*n
b=[input().split() for _ in range(m)]
c=[0]*m+[ans]
for i in range(m-1,-1,-1):
  x,y=map(f,b[i+1])
  if x!=y:
    l[x]=y
    ans-=a[x]*a[y]
    a[y]+=a[x]
  c[i]=ans
for x in c[1:]:
  print(x)

Buggy submission (verdict: Runtime Error):
def root(s):
  t=l[s]
  if t<0:
    return s
  return root(t)
n,m=map(int,input().split())
ans=n*(n-1)//2
l=[-1]*n
a=[1]*n
b=[input().split() for _ in range(m)]
c=[ans]*m
for i in range(m-2,-1,-1):
  x,y=map(int,b[i+1])
  x=root(x-1)
  y=root(y-1)
  if x!=y:
    l[x]=y
    ans-=a[x]*a[y]
    a[y]+=a[x]
  c[i]=ans
for x in c:
  print(x)

Buggy submission (verdict: Runtime Error):
def root(s):
  t=l[s]
  if not t:
    return s
  return root(t)
n,m=map(int,input().split())
ans=n*(n-1)//2
l=[0]*n
a=[1]*n
b=[input().split() for _ in range(m)]
c=[ans]*m
for i in range(m-2,-1,-1):
  x,y=map(int,b[i+1])
  x=root(x-1)
  y=root(y-1)
  if x!=y:
    l[x]=y
    ans-=a[x]*a[y]
    a[y]+=a[x]
  c[i]=ans
for x in c:
  print(x)