forked from abeaumont/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd.py
More file actions
executable file
·39 lines (34 loc) · 786 Bytes
/
Copy pathd.py
File metadata and controls
executable file
·39 lines (34 loc) · 786 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env pypy3
import sys
sys.setrecursionlimit(20000)
n, k, l = map(int, input().split())
a = [list() for _ in range(n)]
b = [list() for _ in range(n)]
for _ in range(k):
p, q = [int(x) - 1 for x in input().split()]
a[p].append(q)
a[q].append(p)
for _ in range(l):
r, s = [int(x) - 1 for x in input().split()]
b[r].append(s)
b[s].append(r)
def g(a, b, i, k):
b[i] = k
for e in a[i]:
if b[e] is not None: continue
g(a, b, e, k)
def f(a, b):
k = 0
for i in range(n):
if b[i] is not None: continue
g(a, b, i, k)
k += 1
c = [None] * n
f(a, c)
d = [None] * n
f(b, d)
e = {}
for i in range(n):
k = (c[i], d[i])
e[k] = e.get(k, 0) + 1
print(' '.join([str(e[(c[i], d[i])]) for i in range(n)]))