dx = [0, -1, 1]
dy = [1, 0, 0]
class FindUnion:
def __init__(self):
self.parents = {}
self.ranks = {}
self.roots = 0
def addAdress(self, address: int):
if address not in self.parents:
self.parents[address] = address
self.ranks[address] = 1
self.roots += 1
def find(self, address: int) -> int:
if self.parents[address] != address:
self.parents[address] = self.find(self.parents[address])
return self.parents[address]
def union(self, ad1: int, ad2: int) -> None:
root1 = self.find(ad1)
root2 = self.find(ad2)
if self.ranks[root1] > self.ranks[root2]:
self.parents[root2] = root1
self.roots -= 1
elif self.ranks[root1] < self.ranks[root2]:
self.parents[root1] = root2
self.roots -= 1
elif root1 != root2:
self.parents[root2] = root1
self.ranks[root1] += 1
self.roots -= 1
n, k = [int(x) for x in input().strip().split(" ")]
grid = [[0] * n, [0] * n]
grid_map = {}
i = 0
for x in input().strip().split(" "):
grid[0][i] = int(x)
grid_map[int(x)] = (0, i)
i += 1
i = 0
for x in input().strip().split(" "):
grid[1][i] = int(x)
grid_map[int(x)] = (1, i)
i += 1
res = [0] * k
for i in range(2 * n):
fu = FindUnion()
for j in range(i, 2 * n):
l = i + 1
r = j + 1
fu.addAdress(r)
y, x = grid_map[r]
for di, dj in zip(dx, dy):
new_x, new_y = (x + di) % n, (y + dj) % 2
new_index = grid[new_y][new_x]
if l <= new_index <= r:
fu.union(r, new_index)
interest = fu.roots
if interest <= k:
res[interest - 1] += 1
for x in res:
print(x, end=' ')
print()
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | dx = [0, -1, 1] dy = [1, 0, 0] class FindUnion: def __init__(self): self.parents = {} self.ranks = {} self.roots = 0 def addAdress(self, address: int): if address not in self.parents: self.parents[address] = address self.ranks[address] = 1 self.roots += 1 def find(self, address: int) -> int: if self.parents[address] != address: self.parents[address] = self.find(self.parents[address]) return self.parents[address] def union(self, ad1: int, ad2: int) -> None: root1 = self.find(ad1) root2 = self.find(ad2) if self.ranks[root1] > self.ranks[root2]: self.parents[root2] = root1 self.roots -= 1 elif self.ranks[root1] < self.ranks[root2]: self.parents[root1] = root2 self.roots -= 1 elif root1 != root2: self.parents[root2] = root1 self.ranks[root1] += 1 self.roots -= 1 n, k = [int(x) for x in input().strip().split(" ")] grid = [[0] * n, [0] * n] grid_map = {} i = 0 for x in input().strip().split(" "): grid[0][i] = int(x) grid_map[int(x)] = (0, i) i += 1 i = 0 for x in input().strip().split(" "): grid[1][i] = int(x) grid_map[int(x)] = (1, i) i += 1 res = [0] * k for i in range(2 * n): fu = FindUnion() for j in range(i, 2 * n): l = i + 1 r = j + 1 fu.addAdress(r) y, x = grid_map[r] for di, dj in zip(dx, dy): new_x, new_y = (x + di) % n, (y + dj) % 2 new_index = grid[new_y][new_x] if l <= new_index <= r: fu.union(r, new_index) interest = fu.roots if interest <= k: res[interest - 1] += 1 for x in res: print(x, end=' ') print() |
English