import numpy as np import itertools import random N, M = map(int, input().split()) AB = [np.array(list(map(int, input().split()))) for _ in range(N)] AB = np.array(AB) class KMeans: def __init__(self, n_clusters, max_iter = 10, random_seed = 0): self.n_clusters = n_clusters self.max_iter = max_iter def fit(self, X): self.labels_ = [] now = 0 while len(self.labels_) < len(X): self.labels_.append(now) now += 1 now %= self.n_clusters random.shuffle(self.labels_) labels_prev = [0]*len(X) count = 0 self.cluster_centers_ = [(0, 0)] * self.n_clusters while (not (self.labels_ == labels_prev) and count < self.max_iter): syuukei = [[] for _ in range(self.n_clusters)] for i in range(len(X)): syuukei[self.labels_[i]].append(X[i]) for i,l in enumerate(syuukei): if l: x, y = sum(x for x,y in l)//len(l), sum(y for x,y in l)//len(l) else: x, y = random.randint(0, 1000), random.randint(0, 1000) self.cluster_centers_[i] = (x, y) labels_prev = self.labels_[:] for i in range(len(X)): min = -1 dist = 10**18 for j in range(self.n_clusters): tmp = (X[i][0] - self.cluster_centers_[j][0])**2 + (X[i][1] - self.cluster_centers_[j][1])**2 if tmp < dist: dist = tmp self.labels_[i] = j count += 1 def predict(self, X): dist = ((X[:, :, np.newaxis] - self.cluster_centers_.T[np.newaxis, :, :]) ** 2).sum(axis = 1) labels = dist.argmin(axis = 1) return labels model = KMeans(8) model.fit(AB) labels = model.labels_ centers = model.cluster_centers_ ans = [(1, 0)] L = sorted(enumerate(labels), key=lambda x: x[1]) for i in range(L[-1][1]+1): for j, cluster_number in L: if cluster_number == i: ans.append((1, j)) ans.append((2, i)) ans.append((1, 0)) for c,d in centers: print(c, d) print(len(ans)) for a,b in ans: print(a, b+1)