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:
import sys

n,k,l=map(int,sys.stdin.readline().split())
pk=list(range(n))
rk=[set([i]) for i in range(n)]
pl=list(range(n))
rl=[set([i]) for i in range(n)]

def find(x,parent):
    if parent[x] != x:
        parent[x] = find(parent[x],parent)
    return parent[x]

def union(x,y,parent,road):
    x,y = find(x,parent),find(y,parent)
    parent[y] = x
    if len(road[y]) > len(road[x]): x,y = y,x
    road[x] = road[x] | road[y]


for i in range(k):
    a,b = map(int,sys.stdin.readline().split())
    if find(a-1,pk) != find(b-1,pk):
        union(a-1,b-1,pk,rk)

for i in range(l):
    a,b = map(int,sys.stdin.readline().split())
    if find(a-1,pl) != find(b-1,pl):
        union(a-1,b-1,pl,rl)

for i in range(n):
    print(len(rk[find(i,pk)] & rl[find(i,pl)]),end=" ")

Submission status: Wrong Answer

Solution code:
import sys

n,k,l=map(int,sys.stdin.readline().split())
pk=list(range(n))
rk=[set([i]) for i in range(n)]
pl=list(range(n))
rl=[set([i]) for i in range(n)]

def find(x,parent):
    if parent[x] != x:
        parent[x] = find(parent[x],parent)
    return parent[x]

def union(x,y,parent,road):
    x,y = find(x,parent),find(y,parent)
    parent[y] = x
    if len(road[y]) > len(road[x]): x,y = y,x
    road[x] = road[x] | road[y]


for i in range(k):
    a,b = map(int,sys.stdin.readline().split())
    if find(a-1,pk) != find(b-1,pk):
        union(a-1,b-1,pk,rk)

for i in range(l):
    a,b = map(int,sys.stdin.readline().split())
    if find(a-1,pl) != find(b-1,pl):
        union(a-1,b-1,pl,rl)

for i in range(n):
    print(len(rk[find(i,pk)] & rl[find(i,pl)]))

Submission status: Wrong Answer

Solution code:
import sys

n,k,l=map(int,sys.stdin.readline().split())
klist = [[] for i in range(k)]
llist = [[] for i in range(l)]
for i in range(k):
    a,b = map(int,sys.stdin.readline().split())
    klist[i] = [a-1,b-1]
for i in range(l):
    a,b = map(int,sys.stdin.readline().split())
    llist[i] = [a-1,b-1]

pk=list(range(n))
rk=[0]*n
pl=list(range(n))
rl=[0]*n

def find(x,parent):
    if parent[x] != x:
        parent[x] = find(parent[x],parent)
    return parent[x]

def union(x,y,parent):
    x,y = find(x,parent),find(y,parent)
    parent[y] = x
    
def research(x,parent):
    x = find(x,parent)
    res = set()
    for i in range(n):
        if x == parent[i]:
            res.add(i)
    return res

for a,b in klist:
    if find(a,pk) != find(b,pk):
        union(a,b,pk)

for a,b in llist:
    if find(a,pl) != find(b,pl):
        union(a,b,pl)

for i in range(n):
    tmp1 = research(i,pk)
    tmp2 = research(i,pl)
    tmp3 = tmp1 & tmp2
    print(len(research(i,pk) & research(i,pl)))

Submission status: Wrong Answer

Solution code:
def inpl(): return [int(i) for i in input().split()]

def root(x,par):
    if x == par[x]:
        return [x,[x]]
    else:
        return [root(par[x],par)[0],root(par[x],par)[1]+[x]]


N, K, L = inpl()
parK = list(range(N))
for i in range(K):
    p, q = inpl()
    rp = root(p-1,parK)
    rq = root(q-1,parK)
    for i in rp[1]:
        parK[i] = rp[0]
    for j in rq[1]:
        parK[j] = rp[0]

parL = list(range(N))
for i in range(L):
    r, s = inpl()
    rr = root(r-1,parL)
    rs = root(s-1,parL)
    for i in rr[1]:
        parL[i] = rr[0]
    for j in rs[1]:
        parL[j] = rr[0]

from collections import defaultdict
ans = defaultdict(lambda: 0)
for i in range(N):
    ans[parK[i],parL[i]] +=1
Ans = [0 for _ in range(N)]
for i in range(N):
    Ans[i]=ans[parK[i],parL[i]]
print(*Ans)

Submission status: Wrong Answer

Solution code:
# Union-Findデータ構造
class UnionFind:
    def __init__(self, numV):
        self.pars = [i for i in range(numV)]
        self.ranks = [0] * numV
        self.sizes = [1] * numV ###

    def find(self, x):
        if self.pars[x] == x:
            return x
        else:
            self.pars[x] = self.find(self.pars[x])
            return self.pars[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.ranks[x] < self.ranks[y]:
            self.pars[x] = y
            self.sizes[y] += self.sizes[x] ###
        else:
            self.pars[y] = x
            self.sizes[x] += self.sizes[y] ###
            if self.ranks[x] == self.ranks[y]:
                self.ranks[x] += 1

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

    def getSize(self, x): ###
        return self.sizes[self.find(x)] ###


N, K, L = map(int, input().split())
pqs = [tuple(map(lambda x: int(x) - 1, input().split())) for _ in range(K)]
rss = [tuple(map(lambda x: int(x) - 1, input().split())) for _ in range(L)]

# 道路
UFRoad = UnionFind(N)
for p, q in pqs:
    UFRoad.union(p, q)

# 鉄道
UFRailway = UnionFind(N)
for r, s in rss:
    if UFRoad.same(r, s):
        UFRailway.union(r, s)

anss = [UFRailway.getSize(i) for i in range(N)]
print(' '.join(map(str, anss)))

Submission status: Wrong Answer

Solution code:
import queue
N,K,L=map(int,input().split())
G=[[] for i in range(N)]
H=[[] for i in range(N)]
#T=[-1 for i in range(N)]
#B=[-1 for i in range(N)]
for i in range(K):
    p,q=map(int,input().split())
    G[p-1].append(q-1)
    G[q-1].append(p-1)
for i in range(L):
    r,s=map(int,input().split())
    H[r-1].append(s-1)
    H[s-1].append(r-1)
def bfs(A):
    k=1
    q=queue.Queue()
    reached=[0 for i in range(N)]
    res=[0 for i in range(N)]
    for i in range(N):
        if reached[i]==1:
            continue
        q.put(i)
        while(not(q.empty())):
            r=q.get()
            res[r]=k
            for p in A[r]:
                if reached[p]==1:
                    continue
                q.put(p)
                reached[p]=1
        k+=1
    return res
T=bfs(G)
B=bfs(H)
print(T)
exit()
TB=[(T[i],B[i]) for i in range(N)]
D=dict()
for i in range(N):
    if TB[i] in D:
        D[TB[i]]+=1
    else:
        D[TB[i]]=1
ans=[str(D[TB[i]]) for i in range(N)]
print(" ".join(ans))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
N, K, L = map(int, input().split())


class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n)]
        self.rank = [0] * n
        self.size = [1] * n

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[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.par[x] = y
            self.size[y] += self.size[x]
            self.size[x] = 0
        else:
            self.par[y] = x
            self.size[x] += self.size[y]
            self.size[y] = 0
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

    # すべての頂点に対して親を検索する
    def all_find(self):
        for n in range(1, len(self.par)):
            self.find(n)


UF1 = UnionFind(N)
UF2 = UnionFind(N)
for k in range(K):
    p, q = map(int, input().split())
    UF1.union(p-1, q-1)

for l in range(L):
    l, s = map(int, input().split())
    UF2.union(l-1, s-1)

UF1.all_find()
UF2.all_find()

D = defaultdict(int)
for i in range(N):
    D[UF1.par[i], UF2.par[i]] += 1

ans = [D[UF1.par[i], UF2.par[i]] for i in range(N)]
print(" ".join(map(str, ans)))

Submission status: Wrong Answer

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


class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n + 1)]
        self.rank = [0] * (n + 1)
        self.size = [1] * (n + 1)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[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.par[x] = y
            self.size[y] += self.size[x]
            self.size[x] = 0
        else:
            self.par[y] = x
            self.size[x] += self.size[y]
            self.size[y] = 0
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

    # すべての頂点に対して親を検索する
    def all_find(self):
        for n in range(1, len(self.par)):
            self.find(n)


UF = UnionFind(N)
for k in range(K):
    p, q = map(int, input().split())
    UF.union(p, q)

ans = [1] * (N+1)
for l in range(L):
    l, s = map(int, input().split())
    if UF.same_check(l, s):
        ans[l] += 1
        ans[s] += 1

print(" ".join(map(str, ans[1:])))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

N, K, L = map(int, input().split())
roads = [list(map(int, input().split())) for i in range(K)]
trains = [list(map(int, input().split())) for i in range(L)]


class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n)]
        self.rank = [0] * (n)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)


road_uf = UnionFind(N)
train_uf = UnionFind(N)

for p, q in roads:
    road_uf.union(p-1, q-1)

for r, s in trains:
    train_uf.union(r-1, s-1)

road_par = road_uf.par
train_par = train_uf.par

ans = []
d = defaultdict(int)
for i in range(N):
    d[road_par[i], train_par[i]] += 1
ans = (d[road_par[i], train_par[i]] for i in range(N))

print(' '.join(str(i) for i in ans))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

N, K, L = map(int, input().split())
roads = [list(map(int, input().split())) for i in range(K)]
trains = [list(map(int, input().split())) for i in range(L)]


class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            pass
        elif self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)


road_uf = UnionFind(N)
train_uf = UnionFind(N)

for p, q in roads:
    road_uf.union(p, q)

for r, s in trains:
    train_uf.union(r, s)

road_par = road_uf.par
train_par = train_uf.par

ans = []
d = defaultdict(int)
for i in range(1, N+1):
    d[road_par[i], train_par[i]] += 1
ans = (d[road_par[i], train_par[i]] for i in range(1, N+1))

print(' '.join(str(i) for i in ans))

Submission status: Wrong Answer

Solution code:
# 頂点 v の所属するグループを調べる
def root(v, uni):
  if uni[v] < 0:  # v が親の場合
    return v
  else:           # v が子の場合
    uni[v] = root(uni[v], uni)  # 親のrootを調べる
    return uni[v]

# 頂点 a と頂点 b をつなぐ。もともと同じグループのとき、False を返す
def connect(a, b, uni, sets):
  # まずはそれぞれ根の番号に置き換える
  ra = root(a, uni)
  rb = root(b, uni)
  if ra == rb:  # a と b がそもそも同じグループに属しているなら即終了
    return False
  
  # ra と rb を結合し、rb の親を ra とする
  sets[ra] = sets[ra] | sets[rb]
  uni[rb] = ra
  return True


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

road = [0]*(N+1)
rail = [0]*(N+1)

road_sets = [0]*(N+1)
rail_sets = [0]*(N+1)

for i in range(1, N+1):
  road[i] = -i
  rail[i] = -i
  road_sets[i] = set([i])
  rail_sets[i] = set([i])

for i in range(K):
  a, b = map(int, input().split())
  connect(a, b, road, road_sets)

for i in range(L):
  a, b = map(int, input().split())
  connect(a, b, rail, rail_sets)

print("hoge")
# for i in range(1, N+1):
#   tmp = road_sets[root(i, road)] & rail_sets[root(i, rail)]
#   ans = len(tmp)
#   if i == N:
#     print(ans)
#   else:
#     print(ans, end=" ")

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
class UnionFind:
    def __init__(self, size):
        self.rank = [0] * size
        self.par = range(size)

    def find(self, x):
        if x == self.par[x]: return x
        self.par[x] = self.find(self.par[x])
        return self.par[x]

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

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

        if (self.rank[x] > self.rank[y]):
            self.par[y] = x
        else:
            self.par[x] = y
            if (self.rank[x] == self.rank[y]): self.rank[y] += 1

N, K, L = map(int, raw_input().split())
pq = [map(int, raw_input().split()) for i in xrange(K)]
rs = [map(int, raw_input().split()) for i in xrange(L)]

road = UnionFind(N)
rail = UnionFind(N)
for p, q in pq:
    road.unite(p - 1, q - 1)
for r, s in rs:
    rail.unite(r - 1, s - 1)

pair = defaultdict(int) 
for i in xrange(N):
    pair[road.par[i], rail.par[i]] += 1

print " ".join(map(str, [pair[road.par[i], rail.par[i]] for i in xrange(N)]))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
class UnionFind:
    def __init__(self, size):
        self.rank = [0] * size
        self.par = range(size)

    def find(self, x):
        if x == self.par[x]: return x
        self.par[x] = self.find(self.par[x])
        return self.par[x]

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

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

        if (self.rank[x] > self.rank[y]):
            self.par[y] = x
        else:
            self.par[x] = y
            if (self.rank[x] == self.rank[y]): self.rank[y] += 1

N, K, L = map(int, raw_input().split())
pq = [map(int, raw_input().split()) for i in xrange(K)]
rs = [map(int, raw_input().split()) for i in xrange(L)]

road = UnionFind(N)
rail = UnionFind(N)
for p, q in pq:
    road.unite(p - 1, q - 1)
for r, s in rs:
    rail.unite(r - 1, s - 1)

pair = defaultdict(int) 
for i in xrange(N):
    pair[(road.par[i], rail.par[i])] += 1

ans = [0]*N
for i in xrange(N):
    ans[i] = pair[(road.par[i], rail.par[i])]

print " ".join(map(str, ans))

Submission status: Wrong Answer

Solution code:
class UnionFind:
    def __init__(self, size):
        self.rank = [0] * size
        self.par = range(size)
	self.num = [1]*size

    def find(self, x):
        if x == self.par[x]: return x
        self.par[x] = self.find(self.par[x])
        return self.par[x]

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

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

        if (self.rank[x] > self.rank[y]):
            self.num[x] += self.num[y]
            self.par[y] = x
        else:
            self.num[y] += self.num[x]
            self.par[x] = y
            if (self.rank[x] == self.rank[y]): self.rank[y] += 1

    def getnum(self, x):
       return self.num[self.par[x]]

N, K, L = map(int, raw_input().split())
pq = [map(int, raw_input().split()) for i in xrange(K)]
rs = [map(int, raw_input().split()) for i in xrange(L)]

road = UnionFind(N)
rail = UnionFind(N)
for p, q in pq:
    road.unite(p - 1, q - 1)
for r, s in rs:
    if road.same(r - 1, s - 1):
        rail.unite(r - 1, s - 1)

print " ".join(map(str, [rail.getnum(i) for i in xrange(N)]))

Submission status: Wrong Answer

Solution code:
class UnionFind:
    def __init__(self, size):
        self.rank = [0] * size
        self.par = range(size)
	self.num = [1]*size

    def find(self, x):
        if x == self.par[x]: return x
        self.par[x] = self.find(self.par[x])
        return self.par[x]

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

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

        if (self.rank[x] > self.rank[y]):
            self.num[x] += self.num[y]
            self.par[y] = x
        else:
            self.num[y] += self.num[x]
            self.par[x] = y
            if (self.rank[x] == self.rank[y]): self.rank[y] += 1

    def getnum(self, x):
       return self.num[self.par[x]]

N, K, L = map(int, raw_input().split())
pq = [sorted(map(int, raw_input().split())) for i in xrange(K)]
rs = set([tuple(sorted(map(int, raw_input().split()))) for i in xrange(L)])

uf = UnionFind(N)
for p, q in pq:
    if (p, q) in rs:
        uf.unite(p - 1, q - 1)
print " ".join(map(str, [uf.getnum(i) for i in xrange(N)]))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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


class UnionFind(object):

    def __init__(self, n):
        self.n = n
        self.par = list(range(n))
        self.rank = [1] * n

    def is_same(self, a, b):
        return self.root(a) == self.root(b)

    def root(self, x):
        if self.par[x] == x:
            return x
        self.par[x] = self.root(self.par[x])
        return self.par[x]

    def unite(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        if self.rank[x] > self.rank[y]:
            self.par[y] = x
        elif self.rank[x]  < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            self.rank[x] += 1

    def group(self):
        d = defaultdict(set)
        for i in range(self.n):
            d[self.root(i)].add(i)
        return d

load_uf = UnionFind(N)
rail_uf = UnionFind(N)

for k in range(K):
    p, q = map(int, input().split())
    p -= 1
    q -= 1
    load_uf.unite(p, q)

for l in range(L):
    r, s = map(int, input().split())
    r -= 1
    s -= 1
    rail_uf.unite(r, s)
ld = load_uf.group()
rd = rail_uf.group()

ans = []
for i in range(N):
    lp = load_uf.par[i]
    rp = rail_uf.par[i]
    print(rd[rp])
    print(ld[lp])
    ans.append(len(ld[lp] & rd[rp]))
print(str(ans).replace(',', '')[1:-1])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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


class UnionFind(object):

    def __init__(self, n):
        self.n = n
        self.par = list(range(n))
        self.rank = [1] * n

    def is_same(self, a, b):
        return self.root(a) == self.root(b)

    def root(self, x):
        if self.par[x] == x:
            return x
        self.par[x] = self.root(self.par[x])
        return self.par[x]

    def unite(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        if self.rank[x] > self.rank[y]:
            self.par[y] = x
        elif self.rank[x]  < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            self.rank[x] += 1

    def group(self):
        d = defaultdict(set)
        for i in range(self.n):
            d[self.par[i]].add(i)
        return d

load_uf = UnionFind(N)
rail_uf = UnionFind(N)

for k in range(K):
    p, q = map(int, input().split())
    p -= 1
    q -= 1
    load_uf.unite(p, q)

for l in range(L):
    r, s = map(int, input().split())
    r -= 1
    s -= 1
    rail_uf.unite(r, s)
ld = load_uf.group()
rd = rail_uf.group()

ans = []
for i in range(N):
    lp = load_uf.par[i]
    rp = rail_uf.par[i]
    ans.append(len(ld[lp] & rd[rp]))
print(str(ans).replace(',', '')[1:-1])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sys
import re
import math
import itertools
import collections
import bisect
#sys.stdin=file('input.txt')
#sys.stdout=file('output.txt','w')
#10**9+7
mod=1000000007
#mod=1777777777
pi=3.141592653589
IS=float('inf')
xy=[(1,0),(-1,0),(0,1),(0,-1)]
bs=[(-1,-1),(-1,1),(1,1),(1,-1)]
def niten(a,b): return abs(a-b) if a>=0 and b>=0 else  a+abs(b) if a>=0 else abs(a)+b if b>=0 else abs(abs(a)-abs(b))
def fib(n): return [(seq.append(seq[i-2] + seq[i-1]), seq[i-2])[1] for seq in [[0, 1]] for i in range(2, n)]
def gcd(a,b): return a if b==0 else gcd(b,a%b)
def lcm(a,b): return a*b/gcd(a,b)
def eucl(x1,y1,x2,y2): return ((x1-x2)**2+(y1-y2)**2)**0.5
def choco(xa,ya,xb,yb,xc,yc,xd,yd): return 1 if abs((yb-ya)*(yd-yc)+(xb-xa)*(xd-xc))<1.e-10 else 0
def pscl(num,l=[1]):
    for i in range(num):
        l = map(lambda x,y:x+y,[0]+l,l+[0])
    return l

class UnionFind:
    def __init__(self, size):
        self.rank=[0]*size
        self.par =range(size)
        self.grp =size

    def find(self, x):
        if x==self.par[x]: return x

        self.par[x]=self.find(self.par[x])
        return self.par[x]

    def same(self, x, y): #2つの頂点が同じグループであるかを判定する
        return self.find(x)==self.find(y)

    def unite(self, x, y): #辺で接続されている2つの頂点を投げて統合する
        x,y=self.find(x),self.find(y)
        if x==y:
            return

        self.grp-=1
        if self.rank[x]<self.rank[y]:
            self.par[x]=y
        else:
            self.par[y]=x
            if self.rank[x]==self.rank[y]:
                self.rank[x]+=1

    def group_num(self):
        return self.grp
    def ra(self):
        return self.par

n,k,l=map(int,raw_input().split())
uf=UnionFind(n)
uf2=UnionFind(n)
ans=[1]*n

for i in range(k):
    a,b=map(int,raw_input().split())
    uf.unite(a-1,b-1)

for i in range(l):
    a,b=map(int,raw_input().split())
    uf2.unite(a-1,b-1)

q1=uf.ra()
q2=uf2.ra()
d={}
for i in range(n):
    if (q1[i],q2[i]) in d:
        d[(q1[i],q2[i])]+=1
    else:
        d[(q1[i],q2[i])]=1
for i in range(n):
    print d[(q1[i],q2[i])],

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
class UnionFind:
    def __init__(self,size):
        self.parent=range(size)
        self.tree_num=size
        self.tree_w=[1]*size
        self.rank=[0]*size

    def findset(self,x):
        if self.parent[x]==x:
            return x
        else:
            self.parent[x]=self.findset(self.parent[x])
            return self.parent[x]

    def unite(self,x,y):
        x=self.findset(x)
        y=self.findset(y)
        if x==y:
            return
        if self.rank[x]<self.rank[y]:
            self.parent[x]=y
            self.tree_num-=1
            self.tree_w[y]+=self.tree_w[x]
        else:
            self.parent[y]=x
            self.tree_num-=1
            self.tree_w[x]+=self.tree_w[y]
            if self.rank[x]==self.rank[y]:
                self.rank[x]+=1

    def same(self,x,y):
        if self.findset(x)==self.findset(y):
            return True
        else:
            return False

    def tree_number(self):
        return self.tree_num

    def tree_weight(self,x):
        return self.tree_w[self.findset(x)]

n,k,l=map(int,raw_input().split())
uf1=UnionFind(n)
uf2=UnionFind(n)
for i in xrange(k):
    p,q=map(int,raw_input().split())
    p-=1;q-=1
    uf1.unite(p,q)
for i in xrange(l):
    p,q=map(int,raw_input().split())
    p-=1;q-=1
    uf2.unite(p,q)
cnt=defaultdict(int)
for i in xrange(n):
    cnt[str(uf1.findset(i))+str(uf2.findset(i))]+=1
ans=[0]*n
for i in xrange(n):
    ans[i]+=cnt[str(uf1.findset(i))+str(uf2.findset(i))]
print " ".join(map(str,ans))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sys
import re
import math
import itertools
import collections
import bisect
#sys.stdin=file('input.txt')
#sys.stdout=file('output.txt','w')
#10**9+7
mod=1000000007
#mod=1777777777
pi=3.141592653589
IS=float('inf')
xy=[(1,0),(-1,0),(0,1),(0,-1)]
bs=[(-1,-1),(-1,1),(1,1),(1,-1)]
def niten(a,b): return abs(a-b) if a>=0 and b>=0 else  a+abs(b) if a>=0 else abs(a)+b if b>=0 else abs(abs(a)-abs(b))
def fib(n): return [(seq.append(seq[i-2] + seq[i-1]), seq[i-2])[1] for seq in [[0, 1]] for i in range(2, n)]
def gcd(a,b): return a if b==0 else gcd(b,a%b)
def lcm(a,b): return a*b/gcd(a,b)
def eucl(x1,y1,x2,y2): return ((x1-x2)**2+(y1-y2)**2)**0.5
def choco(xa,ya,xb,yb,xc,yc,xd,yd): return 1 if abs((yb-ya)*(yd-yc)+(xb-xa)*(xd-xc))<1.e-10 else 0
def pscl(num,l=[1]):
    for i in range(num):
        l = map(lambda x,y:x+y,[0]+l,l+[0])
    return l

class UnionFind:
    def __init__(self, size):
        self.rank=[0]*size
        self.par =range(size)
        self.grp =size

    def find(self, x):
        if x==self.par[x]: return x

        self.par[x]=self.find(self.par[x])
        return self.par[x]

    def same(self, x, y): #2つの頂点が同じグループであるかを判定する
        return self.find(x)==self.find(y)

    def unite(self, x, y): #辺で接続されている2つの頂点を投げて統合する
        x,y=self.find(x),self.find(y)
        if x==y:
            return

        self.grp-=1
        if self.rank[x]<self.rank[y]:
            self.par[x]=y
        else:
            self.par[y]=x
            if self.rank[x]==self.rank[y]:
                self.rank[x]+=1

    def group_num(self):
        return self.grp
    def ra(self):
        return self.rank

n,k,l=map(int,raw_input().split())
uf=UnionFind(n)
q1,q2=[],[]
ans=[1]*n
for i in range(k):
    a,b=map(int,raw_input().split())
    uf.unite(a-1,b-1)
    q1.append((a-1,b-1))

for i in range(l):
    a,b=map(int,raw_input().split())
    if uf.same(a-1,b-1):
        ans[a-1]+=1
        ans[b-1]+=1

uf=UnionFind(n)
for i in q2:
    uf.unite(i[0],i[1])
for i in q1:
    if uf.same(i[0],i[1]):
        ans[i[0]]+=1
        ans[i[1]]+=1
    
print ' '.join(map(str,ans))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sys
import re
import math
import itertools
import collections
import bisect
#sys.stdin=file('input.txt')
#sys.stdout=file('output.txt','w')
#10**9+7
mod=1000000007
#mod=1777777777
pi=3.141592653589
IS=float('inf')
xy=[(1,0),(-1,0),(0,1),(0,-1)]
bs=[(-1,-1),(-1,1),(1,1),(1,-1)]
def niten(a,b): return abs(a-b) if a>=0 and b>=0 else  a+abs(b) if a>=0 else abs(a)+b if b>=0 else abs(abs(a)-abs(b))
def fib(n): return [(seq.append(seq[i-2] + seq[i-1]), seq[i-2])[1] for seq in [[0, 1]] for i in range(2, n)]
def gcd(a,b): return a if b==0 else gcd(b,a%b)
def lcm(a,b): return a*b/gcd(a,b)
def eucl(x1,y1,x2,y2): return ((x1-x2)**2+(y1-y2)**2)**0.5
def choco(xa,ya,xb,yb,xc,yc,xd,yd): return 1 if abs((yb-ya)*(yd-yc)+(xb-xa)*(xd-xc))<1.e-10 else 0
def pscl(num,l=[1]):
    for i in range(num):
        l = map(lambda x,y:x+y,[0]+l,l+[0])
    return l

class UnionFind:
    def __init__(self, size):
        self.rank=[0]*size
        self.par =range(size)
        self.grp =size

    def find(self, x):
        if x==self.par[x]: return x

        self.par[x]=self.find(self.par[x])
        return self.par[x]

    def same(self, x, y): #2つの頂点が同じグループであるかを判定する
        return self.find(x)==self.find(y)

    def unite(self, x, y): #辺で接続されている2つの頂点を投げて統合する
        x,y=self.find(x),self.find(y)
        if x==y:
            return

        self.grp-=1
        if self.rank[x]<self.rank[y]:
            self.par[x]=y
        else:
            self.par[y]=x
            if self.rank[x]==self.rank[y]:
                self.rank[x]+=1

    def group_num(self):
        return self.grp
    def ra(self):
        return self.rank

n,k,l=map(int,raw_input().split())
uf=UnionFind(n)
q1,q2=[],[]
ans=[0]*n
for i in range(k):
    a,b=map(int,raw_input().split())
    uf.unite(a-1,b-1)
    q1.append((a-1,b-1))
    ans[a-1]=1
    ans[b-1]=1

for i in range(l):
    a,b=map(int,raw_input().split())
    if ans[a-1]==0: ans[a-1]=1
    if ans[b-1]==0: ans[b-1]=1
    if uf.same(a-1,b-1):
        ans[a-1]=2
        ans[b-1]=2

uf=UnionFind(n)
for i in q2:
    uf.unite(i[0],i[1])
for i in q1:
    if uf.same(i[0],i[1]):
        ans[i[0]]=2
        ans[i[1]]=2
    
print ' '.join(map(str,ans))

Submission status: Wrong Answer

Solution code:
n, k, l = map(int, raw_input().split())

def root(x):
    if x != parent[x]: x = parent[x] = root(parent[x])
    return x
def unite(x, y):
    px = root(x)
    py = root(y)
    if px < py:
        parent[py] = px
    else:
        parent[px] = py

parent = range(n)
for i in xrange(k):
    p, q = map(int, raw_input().split())
    unite(p-1, q-1)
parent1 = parent
for i in xrange(n):root(i)

parent = range(n)
for i in xrange(l):
    r, s = map(int, raw_input().split())
    unite(r-1, s-1)
parent2 = parent
for i in xrange(n):root(i)

dic = {}
for p1, p2 in zip(parent1, parent2):
    dic[p1, p2] = dic.get((p1, p2), 0) + 1

ans = [dic[p1, p2] for p1, p2 in zip(parent1, parent2)]
print " ".join(map(str, ans))

Submission status: Wrong Answer

Solution code:
n, k, l = map(int, raw_input().split())

def root(x):
    if x == parent[x]: return x
    parent[x] = root(parent[x])
    return parent[x]
def unite(x, y):
    px = root(x)
    py = root(y)
    if px < py:
        parent[py] = px
    else:
        parent[px] = py

parent = range(n)
for i in xrange(k):
    p, q = map(int, raw_input().split())
    unite(p-1, q-1)
parent1 = parent
for i in xrange(n):root(i)

parent = range(n)
for i in xrange(l):
    r, s = map(int, raw_input().split())
    unite(r-1, s-1)
parent2 = parent
for i in xrange(n):root(i)

dic = {}
for p1, p2 in zip(parent1, parent2):
    dic[p1, p2] = dic.get((p1, p2), 0) + 1
print dic

ans = [dic[p1, p2] for p1, p2 in zip(parent1, parent2)]
print " ".join(map(str, ans))