Problem p03855 (python)

Fixed submission:
import sys
import collections as c
sys.setrecursionlimit(10**6)

import sys
sys.setrecursionlimit(10**7)

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.root = [-1]*(n+1)
        self.rnk = [0]*(n+1)

    def Find_Root(self, x):
        if(self.root[x] < 0):
            return x
        else:
            self.root[x] = self.Find_Root(self.root[x])
            return self.root[x]
    def Unite(self, x, y):
        x = self.Find_Root(x)
        y = self.Find_Root(y)
        if(x == y):
            return 
        elif(self.rnk[x] > self.rnk[y]):
            self.root[x] += self.root[y]
            self.root[y] = x

        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if(self.rnk[x] == self.rnk[y]):
                self.rnk[y] += 1
    def isSameGroup(self, x, y):
        return self.Find_Root(x) == self.Find_Root(y)

    def Count(self, x):
        return -self.root[self.Find_Root(x)]

N, K, L = map(int,input().split())
p = []
q = []
l = []
r = []

for _ in range(K):
	p_tmp, q_tmp = map(int ,input().split())
	p.append(p_tmp)
	q.append(q_tmp)

for _ in range(L):
	l_tmp, r_tmp = map(int, input().split())
	l.append(l_tmp)
	r.append(r_tmp)

road = UnionFind(N)
train = UnionFind(N)

for i in range(K):
	road.Unite(p[i], q[i])

for i in range(L):
	train.Unite(l[i], r[i])

ar = []

for i in range(1,N+1):
	a = road.Find_Root(i)
	b = train.Find_Root(i)
	ar.append(str([a,b]))

Car = c.Counter(ar)

for i in range(N):
	print(Car[ar[i]], end = " ")

Buggy submission (verdict: Runtime Error):
import sys
import collections as c
sys.setrecursionlimit(10**6)

import sys
sys.setrecursionlimit(10**7)

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.root = [-1]*(n+1)
        self.rnk = [0]*(n+1)

    def Find_Root(self, x):
        if(self.root[x] < 0):
            return x
        else:
            self.root[x] = self.Find_Root(self.root[x])
            return self.root[x]
    def Unite(self, x, y):
        x = self.Find_Root(x)
        y = self.Find_Root(y)
        if(x == y):
            return 
        elif(self.rnk[x] > self.rnk[y]):
            self.root[x] += self.root[y]
            self.root[y] = x

        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if(self.rnk[x] == self.rnk[y]):
                self.rnk[y] += 1
    def isSameGroup(self, x, y):
        return self.Find_Root(x) == self.Find_Root(y)

    def Count(self, x):
        return -self.root[self.Find_Root(x)]

N, K, L = map(int,input().split())
p = []
q = []
l = []
r = []

for _ in range(K):
	p_tmp, q_tmp = map(int ,input().split())
	p.append(p_tmp)
	q.append(q_tmp)

for _ in range(L):
	l_tmp, r_tmp = map(int, input().split())
	l.append(l_tmp)
	r.append(r_tmp)

road = UnionFind(N)
train = UnionFind(N)

for i in range(K):
	road.uniteTree(p[i], q[i])

for i in range(L):
	train.uniteTree(l[i], r[i])

ar = []

for i in range(1,N+1):
	a = road.findRoot(i)
	b = train.findRoot(i)
	ar.append(str([a,b]))

Car = c.Counter(ar)

for i in range(N):
	print(Car[ar[i]], end = " ")

Buggy submission (verdict: Runtime Error):
import sys
import collections as c
sys.setrecursionlimit(10**6)

class UnionFind():
	def __init__(self, n):
		self.size = n
		self.root = [-1 for _ in range(n+1)]
		self.rnk = [0 for _ in range(n+1)]

	def findRoot(self, a):
		if self.root[a] < 0:
			return a
		else:
			return self.findRoot(self.root[a])

	def uniteTree(self, a, b):
		a = self.findRoot(a)
		b = self.findRoot(b)

		if a == b:
			return

		elif self.rnk[a] > self.rnk[b]:
			self.root[a] += self.root[b]
			self.root[b] = a

		elif self.rnk[b] > self.rnk[a]:
			self.root[b] += self.root[a]
			self.root[a] = b

		elif self.rnk[a] == self.rnk[b]:
			self.root[b] += self.rnk[a]
			self.root[a] = b
			self.rnk[b] += 1

	def judgeTree(self, a, b):
		return self.findRoot(a) == self.findRoot(b)

	def count(self, a):
		return self.rnk[self.findRoot(a)] * (-1)

N, K, L = map(int,input().split())
p = []
q = []
l = []
r = []

for _ in range(K):
	p_tmp, q_tmp = map(int ,input().split())
	p.append(p_tmp)
	q.append(q_tmp)

for _ in range(L):
	l_tmp, r_tmp = map(int, input().split())
	l.append(l_tmp)
	r.append(r_tmp)

road = UnionFind(N)
train = UnionFind(N)

for i in range(K):
	road.uniteTree(p[i], q[i])

for i in range(L):
	train.uniteTree(l[i], r[i])

ar = []

for i in range(1,N+1):
	a = road.findRoot(i)
	b = train.findRoot(i)
	ar.append(str([a,b]))

Car = c.Counter(ar)

for i in range(N):
	print(Car[ar[i]], end = " ")

Buggy submission (verdict: Runtime Error):
import sys
import collections as c
sys.setrecursionlimit(10**6)

class UnionFind():
	def __init__(self, n):
		self.size = n
		self.root = [-1 for _ in range(n+1)]
		self.rnk = [0 for _ in range(n+1)]

	def findRoot(self, a):
		if self.root[a] < 0:
			return a
		else:
			return self.findRoot(self.root[a])

	def uniteTree(self, a, b):
		a = self.findRoot(a)
		b = self.findRoot(b)

		if a == b:
			return

		elif self.rnk[a] > self.rnk[b]:
			self.root[a] += self.root[b]
			self.root[b] = a

		elif self.rnk[b] > self.rnk[a]:
			self.root[b] += self.root[a]
			self.root[a] = b

		elif self.rnk[a] == self.rnk[b]:
			self.root[b] += self.rnk[a]
			self.root[a] = b
			self.rnk[b] += 1

	def judgeTree(self, a, b):
		return self.findRoot(a) == self.findRoot(b)

	def count(self, a):
		return self.rnk[self.findRoot(a)] * (-1)

N, K, L = map(int,input().split())
p = []
q = []
l = []
r = []

for _ in range(K):
	p_tmp, q_tmp = map(int ,input().split())
	p.append(p_tmp)
	q.append(q_tmp)

for _ in range(L):
	l_tmp, r_tmp = map(int, input().split())
	l.append(l_tmp)
	r.append(r_tmp)

road = UnionFind(N)
train = UnionFind(N)

for i in range(K):
	road.uniteTree(p[i], q[i])

for i in range(L):
	train.uniteTree(l[i], r[i])

ar = ["[-1,-1]"]

for i in range(1,N+1):
	a = road.findRoot(i)
	b = train.findRoot(i)
	ar.append(str([a,b]))

Car = c.Counter(ar)

for i in range(1,N+1):
	print(Car[ar[i]], end = " ")

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)


class UnionFind():
	def __init__(self, n):
		self.size = n
		self.root = [-1 for _ in range(n+1)]
		self.rnk = [0 for _ in range(n+1)]

	def findRoot(self, a):
		if self.root[a] < 0:
			return a
		else:
			return self.findRoot(self.root[a])

	def uniteTree(self, a, b):
		a = self.findRoot(a)
		b = self.findRoot(b)

		if a == b:
			return

		elif self.rnk[a] > self.rnk[b]:
			self.root[a] += self.root[b]
			self.root[b] = a

		elif self.rnk[b] > self.rnk[a]:
			self.root[b] += self.root[a]
			self.root[a] = b

		elif self.rnk[a] == self.rnk[b]:
			self.root[b] += self.rnk[a]
			self.root[a] = b
			self.rnk[b] += 1

	def judgeTree(self, a, b):
		return self.findRoot(a) == self.findRoot(b)

	def count(self, a):
		return self.rnk[self.findRoot(a)] * (-1)

N, K, S = map(int, input().split())
P = []
Q = []
R = []
L = []

for _ in range(K):
	p_tmp, q_tmp = map(int, input().split())
	P.append(p_tmp)
	Q.append(q_tmp)

for _ in range(S):
	r_tmp, l_tmp = map(int, input().split())
	R.append(r_tmp)
	L.append(l_tmp)
	
road = UnionFind(N)
train = UnionFind(N)

for i in range(K):
	road.uniteTree(P[i], Q[i])

for i in range(S):
	train.uniteTree(R[i], L[i])

ans = [1 for _ in range(N)]

for i in range(1, N+1):
	for j in range(1, N+1):
		if i == j:
			continue
		else:
			if road.judgeTree(i,j) and train.judgeTree(i,j):
				ans[i-1] += 1

for i in ans:
	print(i, end = " ")

Buggy submission (verdict: Runtime Error):
class UnionFind():
	def __init__(self, n):
		self.size = n
		self.root = [-1 for _ in range(n+1)]
		self.rnk = [0 for _ in range(n+1)]

	def findRoot(self, a):
		if self.root[a] < 0:
			return a
		else:
			return self.findRoot(self.root[a])

	def uniteTree(self, a, b):
		a = self.findRoot(a)
		b = self.findRoot(b)

		if a == b:
			return

		elif self.rnk[a] > self.rnk[b]:
			self.root[a] += self.root[b]
			self.root[b] = a

		elif self.rnk[b] > self.rnk[a]:
			self.root[b] += self.root[a]
			self.root[a] = b

		elif self.rnk[a] == self.rnk[b]:
			self.root[b] += self.rnk[a]
			self.root[a] = b
			self.rnk[b] += 1

	def judgeTree(self, a, b):
		return self.findRoot(a) == self.findRoot(b)

	def count(self, a):
		return self.rnk[self.findRoot(a)] * (-1)

N, K, S = map(int, input().split())
P = []
Q = []
R = []
L = []

for _ in range(K):
	p_tmp, q_tmp = map(int, input().split())
	P.append(p_tmp)
	Q.append(q_tmp)

for _ in range(S):
	r_tmp, l_tmp = map(int, input().split())
	R.append(r_tmp)
	L.append(l_tmp)
	
road = UnionFind(N)
train = UnionFind(N)

for i in range(K):
	road.uniteTree(P[i], Q[i])

for i in range(S):
	train.uniteTree(R[i], L[i])

ans = [1 for _ in range(N)]

for i in range(1, N+1):
	for j in range(1, N+1):
		if i == j:
			continue
		else:
			if road.judgeTree(i,j) and train.judgeTree(i,j):
				ans[i-1] += 1

for i in ans:
	print(i, end = " ")