Problem:


Score : 400 points

Problem Statement

There are N cities. There are also K roads and L railways, extending between the cities.
The i-th road bidirectionally connects the p_i-th and q_i-th cities, and the i-th railway bidirectionally connects the r_i-th and s_i-th cities.
No two roads connect the same pair of cities. Similarly, no two railways connect the same pair of cities.

We will say city A and B are connected by roads if city B is reachable from city A by traversing some number of roads. Here, any city is considered to be connected to itself by roads.
We will also define connectivity by railways similarly.

For each city, find the number of the cities connected to that city by both roads and railways.

Constraints

2 ≦ N ≦ 2*10^5

1 ≦ K, L≦ 10^5

1 ≦ p_i, q_i, r_i, s_i ≦ N

p_i < q_i

r_i < s_i

When i ≠ j, (p_i, q_i) ≠ (p_j, q_j)

When i ≠ j, (r_i, s_i) ≠ (r_j, s_j)

Input

The input is given from Standard Input in the following format:

N K L
p_1 q_1
:
p_K q_K
r_1 s_1
:
r_L s_L

Output

Print N integers. The i-th of them should represent the number of the cities connected to the i-th city by both roads and railways.

Sample Input 1

4 3 1
1 2
2 3
3 4
2 3

Sample Output 1

1 2 2 1

All the four cities are connected to each other by roads.

By railways, only the second and third cities are connected. Thus, the answers for the cities are 1, 2, 2 and 1, respectively.

Sample Input 2

4 2 2
1 2
2 3
1 4
2 3

Sample Output 2

1 2 2 1

Sample Input 3

7 4 4
1 2
2 3
2 5
6 7
3 5
4 5
3 4
6 7

Sample Output 3

1 1 2 1 2 2 2



Sample input:
4 3 1
1 2
2 3
3 4
2 3


Sample output:
1 2 2 1

Submission status: Wrong Answer

Solution code:
from collections import Counter


class UnionFind:
  def __init__(self, n):
    self.par = [x for x in range(n)]
    self.size = [1 for _ in range(n)]
    
  def find(self, x):
    if x == self.par[x]:
      return x
    self.par[x] = self.find(self.par[x])
    return self.par[x]
  
  def unite(self, x, y):
    x = self.find(x)
    y = self.find(y)
    if x != y:
      if self.size[x] < self.size[y]:
        x, y = y, x
      self.par[y] = x
      self.size[x] += self.size[y]
    
  def is_same(self, x, y):
    return self.find(x) == self.find(y)
  
  def size_of(self, x):
    return self.size[self.find(x)]

  
N, K, L = map(int, input().split())  

rails = UnionFind(N + 1)
roads = UnionFind(N + 1)

for _ in range(K):
  p, q = map(int, input().split())
  rails.unite(p, q)

for _ in range(L):
  p, q = map(int, input().split())
  roads.unite(p, q)

counter = Counter(zip(rails.par, roads.par))

print(' '.join(
  str(counter[(rails.find(i), roads.find(i))])
  for i in range(1, N + 1)
))

Submission status: Wrong Answer

Solution code:
class UnionFind:
  def __init__(self, n):
    self.par = [x for x in range(n)]
    self.size = [1 for _ in range(n)]
    
  def find(self, x):
    if x == self.par[x]:
      return x
    self.par[x] = self.find(self.par[x])
    return self.par[x]
  
  def unite(self, x, y):
    x = self.find(x)
    y = self.find(y)
    if x != y:
      if self.size[x] < self.size[y]:
        x, y = y, x
      self.par[y] = x
      self.size[x] += self.size[y]
    
  def is_same(self, x, y):
    return self.find(x) == self.find(y)
  
  def size_of(self, x):
    return self.size[self.find(x)]

  
N, K, L = map(int, input().split())  

rails = UnionFind(N + 1)
rails_and_loads = UnionFind(N + 1)

for _ in range(K):
  p, q = map(int, input().split())
  rails.unite(p, q)

for _ in range(L):
  p, q = map(int, input().split())
  if rails.is_same(p, q):
    rails_and_loads.unite(p, q)

print(
  ' '.join(
    str(rails_and_loads.size_of(x)) for x in range(1, N + 1)
  )
)

Submission status: Wrong Answer

Solution code:
class UnionFind():
    def __init__(self, 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 main():
    # import sys
    # readline = sys.stdin.readline
    # readlines = sys.stdin.readlines
    N, K, L = map(int, input().split())
    uf1 = UnionFind(N)
    uf2 = UnionFind(N)

    for i in range(K):
        p, q = map(int, input().split())
        p -= 1; q -= 1
        uf1.union(p, q)
    for j in range(L):
        r, s = map(int, input().split())
        r -= 1; s -= 1
        if uf1.find(r) == uf1.find(s):
            uf2.union(r, s)
    
    ans = []
    for i in range(N):
        ans.append(-uf2.parents[uf2.find(i)])
    print(*ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
class UnionFind():
    def __init__(self, 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 main():
    import sys
    # readline = sys.stdin.readline
    readlines = sys.stdin.readlines
    N, K, L = map(int, input().split())
    uf1 = UnionFind(N)
    uf2 = UnionFind(N)

    IN = readlines()
    for i in range(K):
        p, q = map(int, IN[i].split())
        p -= 1; q -= 1
        uf1.union(p, q)
    for j in range(K, K + L):
        r, s = map(int, IN[j].split())
        r -= 1; s -= 1
        if uf1.find(r) == uf1.find(s):
            uf2.union(r, s)
    
    ans = []
    for i in range(N):
        ans.append(-uf2.parents[uf2.find(i)])
    print(*ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
class UnionFind():
    def __init__(self, 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 main():
    import sys
    # readline = sys.stdin.readline
    readlines = sys.stdin.readlines
    N, K, L = map(int, input().split())
    uf1 = UnionFind(N)
    uf2 = UnionFind(N)

    IN = readlines()
    for i in range(K):
        p, q = map(int, IN[i].split())
        p -= 1; q -= 1
        uf1.union(p, q)
    for j in range(K, K + L):
        r, s = map(int, IN[j].split())
        r -= 1; s -= 1
        if uf1.find(r) == uf1.find(s):
            uf2.union(r, s)
    
    for i in range(N):
        print(-uf2.parents[uf2.find(i)])


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def find(x):
    if(par[x]==x):
        return x
    else:
        par[x]=find(par[x])
        return par[x]

def union(a,b):
    a=find(a)
    b=find(b)
    if(a==b):
        return 0
    else:
        if rank[a]>rank[b]:
            par[b]=a
            siz[a]+=siz[b]
        else:
            par[a]=b
            siz[b]+=siz[a]
            if rank[a]==rank[b]:
                rank[b]+=1
            
def size(a):
    return siz[find(a)]

def same(a,b):
    return find(a)==find(b)

N,K,L=map(int,input().split())
par=[i for i in range(N)]
siz=[1 for _ in range(N)]
rank=[0 for _ in range(N)]

for i in range(K):
    p,q=map(lambda x:int(x)-1,input().split())
    union(p,q)
    
road=[find(i) for i in range(N)]
par=[i for i in range(N)]
siz=[1 for _ in range(N)]
rank=[0 for _ in range(N)]

for i in range(L):
    r,s=map(lambda x:int(x)-1,input().split())
    if(road[r]==road[s]):
        union(r,s)
for i in range(N):
    print(size(i),end=' ')
print()

Submission status: Wrong Answer

Solution code:
class UnionFind:
  def __init__(self, n):
    self.par = [x for x in range(n)]
    self.size = [1 for _ in range(n)]
    
  def find(self, x):
    if x == self.par[x]:
      return x
    self.par[x] = self.find(self.par[x])
    return self.par[x]
  
  def unite(self, x, y):
    x = self.find(x)
    y = self.find(y)
    if x != y:
      if self.size[x] < self.size[y]:
        x, y = y, x
      self.par[y] = x
      self.size[x] += self.size[y]
    
  def is_same(self, x, y):
    return self.find(x) == self.find(y)
  
  def size_of(self, x):
    return self.size[self.find(x)]

  
N, K, L = map(int, input().split())  

rails = UnionFind(N + 1)

connections = [1] * (N + 1)

for _ in range(K):
  p, q = map(int, input().split())
  rails.unite(p, q)

for _ in range(L):
  p, q = map(int, input().split())
  if rails.is_same(p, q):
    connections[p] += 1
    connections[q] += 1

print(' '.join(str(x) for x in connections[1:]))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

N, K, L = map(int, input().split())
road = []
for i in range(K):
  road.append(list(map(int, input().split())))
train = []
for i in range(L):
  train.append(list(map(int, input().split())))

road.sort()
train.sort()

Ar, Br = [], []
for i in range(N+1):
  Ar.append(i)
  Br.append(i)

for i in range(K):
  Ar[road[i][0]] = Ar[road[i][1]] = min(Ar[road[i][0]], Ar[road[i][1]])

for i in range(L):
  Br[train[i][0]] = Br[train[i][1]] = min(Br[train[i][0]], Br[train[i][1]])
  
S = []
for i in range(N+1):
  S.append([Ar[i], Br[i]])

d = defaultdict(int)
for x in S:
  d[str(x)] += 1
  
for i in range(1, N+1):
  print(d[str(S[i])], end = " ")

Submission status: Wrong Answer

Solution code:
N, K, L = map(int, input().split())
road = []
for i in range(K):
  road.append(list(map(int, input().split())))
train = []
for i in range(L):
  train.append(list(map(int, input().split())))

road.sort()
train.sort()

Ar, Br = [], []
for i in range(N+1):
  Ar.append(i)
  Br.append(i)

for i in range(K):
  Ar[road[i][0]] = Ar[road[i][1]] = min(Ar[road[i][0]], Ar[road[i][1]])

for i in range(L):
  Br[train[i][0]] = Br[train[i][1]] = min(Br[train[i][0]], Br[train[i][1]])
  
S = []
for i in range(N+1):
  S.append([Ar[i], Br[i]])

for i in range(1, N+1):
  result = S.count(S[i])
  print(result, end = " ")

Submission status: Wrong Answer

Solution code:
N, K, L = map(int, input().split())
road = []
for i in range(K):
  road.append(list(map(int, input().split())))
train = []
for i in range(L):
  train.append(list(map(int, input().split())))

road.sort()
train.sort()

Ar, Br = [], []
for i in range(N+1):
  Ar.append(i * 3)
  Br.append(i * 5)

for i in range(K):
  Ar[road[i][0]] = Ar[road[i][1]] = min(Ar[road[i][0]], Ar[road[i][1]])

for i in range(L):
  Br[train[i][0]] = Br[train[i][1]] = min(Br[train[i][0]], Br[train[i][1]])
  
S = []
for i in range(N+1):
  S.append(Ar[i] + Br[i])

for i in range(1, N+1):
  f = S[i]
  result = S.count(S[i])
  print(result, end = " ")

Submission status: Wrong Answer

Solution code:
def main():  
  #Union Find

  #xの根を求める
  def find(x):
      if par[x] < 0:
          return x
      else:
          par[x] = find(par[x])
          return par[x]
  #xとyの属する集合の併合
  def unite(x,y):
      x = find(x)
      y = find(y)

      if x == y:
          return False
      else:
          #sizeの大きいほうがx
          if par[x] > par[y]:
              x,y = y,x
          par[x] += par[y]
          par[y] = x
          return True

  #xとyが同じ集合に属するかの判定
  def same(x,y):
      return find2(x) == find2(y)

  def find2(x):
    if pa[x] < 0:
        return x
    else:
        pa[x] = find2(pa[x])
        return pa[x]

  #根なら-size,子なら親の頂点

  import sys
  input = sys.stdin.readline

  n,k,l = [int(i) for i in input().split()]
  par = [-1]*n
  pa = [-1]*n
  ans = [0]*n

  for i in range(k):
    p,q = [int(i) for i in input().split()]
    unite(p-1,q-1)

  def unite2(x,y):
      x = find2(x)
      y = find2(y)
      if x == y:
          return False
      else:
          if pa[x] > pa[y]:
              x,y = y,x
          pa[x] += pa[y]
          pa[y] = x
          return True

  for i in range(l):
    r,s = [int(i) for i in input().split()]
    unite2(r-1,s-1)

  import collections
  par = [find(i) for i in par] 
  pa = [find2(i) for i in pa]
  chk = [x*10**6+y for x,y in zip(par,pa)]
  
  for i in range(n):
    c = collections.Counter(chk)
    ans[i] += c[chk[i]]
  ans = list(map(str,ans))
  print(' '.join(ans))
  
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#Union Find
 
#xの根を求める
def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
#xとyの属する集合の併合
def unite(x,y):
    x = find(x)
    y = find(y)
    
    if x == y:
        return False
    else:
        #sizeの大きいほうがx
        if par[x] > par[y]:
            x,y = y,x
        par[x] += par[y]
        par[y] = x
        return True

#xとyが同じ集合に属するかの判定
def same(x,y):
    return find(x) == find(y)

def find2(x):
  if pa[x] < 0:
      return x
  else:
      pa[x] = find2(pa[x])
      return pa[x]
    
#xが属する集合の個数
def size(x):
    return -pa[find2(x)]

#根なら-size,子なら親の頂点
n,k,l = [int(i) for i in input().split()]
par = [-1]*n
pa = [-1]*n
ans = [0]*n

for i in range(k):
  p,q = [int(i) for i in input().split()]
  unite(p-1,q-1)

def unite2(x,y):
    x = find2(x)
    y = find2(y)
    
    if x == y:
        return False
    else:
        if pa[x] > pa[y]:
            x,y = y,x
        pa[x] += pa[y]
        pa[y] = x
        return True
      
for i in range(l):
  r,s = [int(i) for i in input().split()]
  if same(r-1,s-1):
    unite2(r-1,s-1)
    
for i in range(n):
  ans[i] += size(i)
  
ans = list(map(str,ans))
print(' '.join(ans))

Submission status: Wrong Answer

Solution code:
#Union Find
 
#xの根を求める
def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
#xとyの属する集合の併合
def unite(x,y):
    x = find(x)
    y = find(y)
    
    if x == y:
        return False
    else:
        #sizeの大きいほうがx
        if par[x] > par[y]:
            x,y = y,x
        par[x] += par[y]
        par[y] = x
        return True

#xとyが同じ集合に属するかの判定
def same(x,y):
    return find(x) == find(y)

#xが属する集合の個数
def size(x):
    return -par[find(x)]

#根なら-size,子なら親の頂点
n,k,l = [int(i) for i in input().split()]
par = [-1]*n
ans = [1]*n

for i in range(k):
  p,q = [int(i) for i in input().split()]
  unite(p-1,q-1)

for i in range(l):
  r,s = [int(i) for i in input().split()]
  if same(r-1,s-1):
    ans[r-1] += 1
    ans[s-1] += 1
    
ans = list(map(str,ans))
print(' '.join(ans))

Submission status: Wrong Answer

Solution code:
N,K,L=map(int,input().split())
R=[{i} for i in range(N)]
T=[{i} for i in range(N)]
for i in range(K):
  l,r=map(int,input().split())
  R[l-1]|={r-1}
  R[r-1]|={l-1}
for i in range(L):
  l,r=map(int,input().split())
  T[l-1]|={r-1}
  T[r-1]|={l-1}
print(" ".join(map(str, [len(R[i]&T[i]) for i in range(N)])))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


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())


n, k, l = map(int, input().split())
uf_k = UnionFind(n)
uf_l = UnionFind(n)
ans = [0] * n
for i in range(k):
    p, q = map(int, input().split())
    uf_k.union(p - 1, q - 1)
for i in range(l):
    r, s = map(int, input().split())
    uf_l.union(r - 1, s - 1)
all_k_member = uf_k.all_group_members()
all_l_member = uf_l.all_group_members()
for i in range(n):
    k_member = all_k_member[uf_k.find(i)]
    l_member = all_l_member[uf_l.find(i)]
    all_member = k_member + l_member
    cnt = len(all_member)
    check = len(set(all_member))
    if cnt == check:
        ans[i] = 0
    elif cnt - 1 == check:
        ans[i] = 1
    else:
        ans[i] = 2
print(*ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
class UnionFind:


    def __init__(self,n):
        self.parent = [i for i in range(n)]
        self.rank = [0]*n
        self.n = n
    def find(self,x):
        if self.parent[x] == x:
            return x
        while x != self.parent[x]:
            self.parent[x] = self.parent[self.parent[x]]
            x = self.parent[x]
        return x
    def union(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if x == y:return
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
        else:
            self.parent[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
    def same(self,x,y):
        return bool(self.find(x)==self.find(y))

from sys import stdin
def main():
    N,K,L = map(int,stdin.readline().split())
    uf = UnionFind(N)
    uf1 = UnionFind(N)
    ans = [0 for _ in range(N)]
    for _ in range(K):
        p,q = map(int,stdin.readline().split())
        uf.union(p-1,q-1)
    for _ in range(L):
        r,s = map(int,stdin.readline().split())
        uf1.union(r-1,s-1)
    a = uf.parent
    b = uf1.parent
    l = [(a[i],b[i]) for i in range(N)]
    d = defaultdict(int)
    for i in l:
        d[i] += 1
    for i in range(N):
        ans[i] += d[l[i]]
        #print((uf.find(i),uf1.find(i)))
    print(*ans)
    #print(l)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
class UnionFind:


    def __init__(self,n):
        self.parent = [i for i in range(n)]
        self.rank = [0]*n
        self.n = n
    def find(self,x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]
    def union(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if x == y:return
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
        else:
            self.parent[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
    def same(self,x,y):
        return bool(self.find(x)==self.find(y))

from sys import stdin
def main():
    N,K,L = map(int,stdin.readline().split())
    uf = UnionFind(N)
    uf1 = UnionFind(N)
    ans = [0 for _ in range(N)]
    for _ in range(K):
        p,q = map(int,stdin.readline().split())
        uf.union(p-1,q-1)
    for _ in range(L):
        r,s = map(int,stdin.readline().split())
        uf1.union(r-1,s-1)
    l = [(uf.find(i),uf1.find(i)) for i in range(N)]
    d = defaultdict(int)
    for i in l:
        d[i] += 1
    for i in range(N):
        ans[i] += d[l[i]]
    print(*ans)
    print(l)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
class UnionFind:


    def __init__(self,n):
        self.parent = [i for i in range(n)]
        self.rank = [0]*n
        self.n = n
    def find(self,x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]
    def union(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if x == y:return
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
        else:
            self.parent[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
    def same(self,x,y):
        return bool(self.find(x)==self.find(y))

from sys import stdin
def main():
    N,K,L = map(int,stdin.readline().split())
    uf = UnionFind(N)
    uf1 = UnionFind(N)
    ans = [0 for _ in range(N)]
    for _ in range(K):
        p,q = map(int,stdin.readline().split())
        uf.union(p-1,q-1)
    for _ in range(L):
        r,s = map(int,stdin.readline().split())
        uf1.union(r-1,s-1)
    a = uf.parent
    b = uf1.parent
    l = [(a[i],b[i]) for i in range(N)]
    d = defaultdict(int)
    for i in l:
        d[i] += 1
    for i in range(N):
        ans[i] += d[l[i]]
    print(*ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
class UnionFind:


    def __init__(self,n):
        self.parent = [i for i in range(n)]
        self.rank = [0]*n
    def find(self,x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]
    def union(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if x == y:return
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
        else:
            self.parent[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
    def same(self,x,y):
        return bool(self.find(x)==self.find(y))

from sys import stdin
def main():
    N,K,L = map(int,stdin.readline().split())
    uf = UnionFind(N)
    ans = [1 for _ in range(N)]
    for _ in range(K):
        p,q = map(int,stdin.readline().split())
        uf.union(p-1,q-1)
    for _ in range(L):
        r,s = map(int,stdin.readline().split())
        if uf.same(r-1,s-1):
            ans[r-1] += 1
            ans[s-1] += 1
    print(*ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N, K, L = map(int,input().split())
data = [[0, 0] for _ in range(N)]
Lpath = [[] for _ in range(N)]
Tpath = [[] for _ in range(N)]

for _ in range(K):
    p, q = map(int,input().split())
    p -= 1
    q -= 1
    Lpath[p].append(q)
    Lpath[q].append(p)
    
for _ in range(L):
    r, s = map(int,input().split())
    r -= 1
    s -= 1
    Tpath[r].append(s)
    Tpath[s].append(r)
    
for i in range(N):
    if data[i][0]:
        continue
    data[i][0] = i + 1
    stack = [i]
    while stack:
        s = stack.pop()
        for to in Lpath[s]:
            if data[to][0]:
                continue
            data[to][0] = i + 1
            stack.append(to)

for i in range(N):
    if data[i][1]:
        continue
    data[i][1] = i + 1
    stack = [i]
    while stack:
        s = stack.pop()
        for to in Tpath[s]:
            if data[to][1]:
                continue
            data[to][1] = i + 1
            stack.append(to)

ans = [0] * N
            
for i in range(0):
    ans[i] = data.count(data[i])

print(*ans)

Submission status: Wrong Answer

Solution code:
N, K, L = map(int,input().split())
data = [[0, 0] for _ in range(N)]
Lpath = [[] for _ in range(N)]
Tpath = [[] for _ in range(N)]

for _ in range(K):
    p, q = map(int,input().split())
    p -= 1
    q -= 1
    Lpath[p].append(q)
    Lpath[q].append(p)
    
for _ in range(L):
    r, s = map(int,input().split())
    r -= 1
    s -= 1
    Tpath[r].append(s)
    Tpath[s].append(r)
    
for i in range(N):
    if data[i][0]:
        continue
    data[i][0] = i + 1
    stack = [i]
    while stack:
        s = stack.pop()
        for to in Lpath[s]:
            if data[to][0]:
                continue
            data[to][0] = i + 1
            stack.append(to)

for i in range(N):
    if data[i][1]:
        continue
    data[i][1] = i + 1
    stack = [i]
    while stack:
        s = stack.pop()
        for to in Tpath[s]:
            if data[to][1]:
                continue
            data[to][1] = i + 1
            stack.append(to)

ans = [0] * N
            
for i in range(N//8):
    ans[i] = data.count(data[i])

print(*ans)

Submission status: Wrong Answer

Solution code:
N, K, L = map(int,input().split())
data = [[0, 0] for _ in range(N)]
Lpath = [[] for _ in range(N)]
Tpath = [[] for _ in range(N)]

for _ in range(K):
    p, q = map(int,input().split())
    p -= 1
    q -= 1
    Lpath[p].append(q)
    Lpath[q].append(p)
    
for _ in range(L):
    r, s = map(int,input().split())
    r -= 1
    s -= 1
    Tpath[r].append(s)
    Tpath[s].append(r)
    
for i in range(N):
    if data[i][0]:
        continue
    data[i][0] = i + 1
    stack = [i]
    while stack:
        s = stack.pop()
        for to in Lpath[s]:
            if data[to][0]:
                continue
            data[to][0] = i + 1
            stack.append(to)

for i in range(N):
    if data[i][1]:
        continue
    data[i][1] = i + 1
    stack = [i]
    while stack:
        s = stack.pop()
        for to in Tpath[s]:
            if data[to][1]:
                continue
            data[to][1] = i + 1
            stack.append(to)

ans = [0] * N
            
for i in range(N//4):
    ans[i] = data.count(data[i])

print(*ans)

Submission status: Wrong Answer

Solution code:
N, K, L = map(int,input().split())
data = [[0, 0] for _ in range(N)]
Lpath = [[] for _ in range(N)]
Tpath = [[] for _ in range(N)]

for _ in range(K):
    p, q = map(int,input().split())
    p -= 1
    q -= 1
    Lpath[p].append(q)
    Lpath[q].append(p)
    
for _ in range(L):
    r, s = map(int,input().split())
    r -= 1
    s -= 1
    Tpath[r].append(s)
    Tpath[s].append(r)
    
for i in range(N):
    if data[i][0]:
        continue
    data[i][0] = i + 1
    stack = [i]
    while stack:
        s = stack.pop()
        for to in Lpath[s]:
            if data[to][0]:
                continue
            data[to][0] = i + 1
            stack.append(to)

for i in range(N):
    if data[i][1]:
        continue
    data[i][1] = i + 1
    stack = [i]
    while stack:
        s = stack.pop()
        for to in Tpath[s]:
            if data[to][1]:
                continue
            data[to][1] = i + 1
            stack.append(to)

ans = [0] * N
            
for i in range(N//2):
    ans[i] = data.count(data[i])

print(*ans)

Submission status: Wrong Answer

Solution code:
from sys import setrecursionlimit
setrecursionlimit(10 ** 9)

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.d = [-1] * n

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

        if x == y:
            return False
        else:
            if self.d[x] > self.d[y]:
                x, y = y, x
            self.d[x] += self.d[y]
            self.d[y] = x
            return True

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


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


n, k, l = map(int, input().split())
uf = UnionFind(n)
for i in range(k):
    p, q = map(int, input().split())
    p -= 1
    q -= 1
    uf.unite(p, q)
ans = [1] * n
for i in range(l):
    r, s = map(int, input().split())
    r -= 1
    s -= 1
    if uf.same(r, s) == True:
        ans[r] += 1
        ans[s] += 1
# print(uf.d)
print(' '.join(map(str, ans)))

Submission status: Wrong Answer

Solution code:
from sys import setrecursionlimit
setrecursionlimit(10 ** 9)

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.d = [-1] * n

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

        if x == y:
            return False
        else:
            if self.d[x] > self.d[y]:
                x, y = y, x
            self.d[x] += self.d[y]
            self.d[y] = x
            return True

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


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


n, k, l = map(int, input().split())
uf = UnionFind(n)
for i in range(k):
    p, q = map(int, input().split())
    p -= 1
    q -= 1
    uf.unite(p, q)
ans = [1] * n
for  i in range(l):
    r, s = map(int, input().split())
    r -= 1
    s -= 1
    if uf.same(r, s):
        ans[r] += 1
        ans[s] += 1
print(' '.join(map(str, ans)))

Submission status: Wrong Answer

Solution code:
def find(x, parent):
    if parent[x] == x:
        return x
    else:
        # keyとvalueが同じ位置に当たるまで親をたどる
        parent[x] = find(parent[x], parent)
        return parent[x]

# 併合
def unite(x, y, parent):
    x = find(x, parent)
    y = find(y, parent)
    if x == y: return

    parent[y] = x

def solve():
    N, K, L = map(int, input().split())
    
    street = [i for i in range(N+1)]
    train = [i for i in range(N+1)]

    for _ in range(K):
        a, b = map(int, input().split())
        unite(0, a, street)
        unite(0, b, street)

    for _ in range(L):
        a, b = map(int, input().split())
        unite(0, a, train)
        unite(0, b, train)

    ans = []
    for i in range(1, N+1):
        c = 0
        if street[i] == 0:
            c += 1
        if train[i] == 0:
            c += 1
        ans.append(str(c))

    print(' '.join(ans))

solve()

Submission status: Wrong Answer

Solution code:
def find(x, parent):
    if parent[x] == x:
        return x
    else:
        # keyとvalueが同じ位置に当たるまで親をたどる
        parent[x] = find(parent[x], parent)
        return parent[x]

# 併合
def unite(x, y, parent):
    x = find(x, parent)
    y = find(y, parent)
    if x == y: return

    parent[y] = x

def solve():
    N, K, L = map(int, input().split())
    
    street = [i for i in range(N+1)]
    train = [i for i in range(N+1)]

    for _ in range(K):
        a, b = map(int, input().split())
        unite(0, a, street)
        unite(0, b, street)

    for _ in range(L):
        a, b = map(int, input().split())
        unite(0, a, train)
        unite(0, b, train)

    ans = []
    for i in range(1, N+1):
        c = 0
        if street[i] == 0:
            c += 1
        if train[i] == 0:
            c += 1
        ans.append(str(c))

    print(' '.join(ans))

print(solve())