Загрузка данных


import math


def dist(a, b):
    return math.sqrt((a[0] - b[0])**2 + (a[1] - b[1])**2)


# --- поиск кластеров ---
def get_clusters(points, threshold=3.5):
    clusters = []
    used = [False] * len(points)
    
    for i in range(len(points)):
        if used[i]:
            continue
        
        stack = [i]
        cluster = []
        used[i] = True
        
        while stack:
            v = stack.pop()
            cluster.append(points[v])
            
            for j in range(len(points)):
                if not used[j] and dist(points[v], points[j]) < threshold:
                    used[j] = True
                    stack.append(j)
        
        clusters.append(cluster)
    
    return clusters


# --- поиск центра (медоида) ---
def get_center(cluster):
    best_point = None
    best_sum = float('inf')
    
    for p in cluster:
        s = sum(dist(p, q) for q in cluster)
        if s < best_sum:
            best_sum = s
            best_point = p
    
    return best_point


# --- обработка файла ---
def solve(file_name):
    points = []
    
    with open(file_name) as f:
        for line in f:
            x, y = map(float, line.split())
            points.append((x, y))
    
    clusters = get_clusters(points)
    
    centers = [get_center(c) for c in clusters]
    
    Px = sum(c[0] for c in centers) / len(centers)
    Py = sum(c[1] for c in centers) / len(centers)
    
    return int(Px * 10000), int(Py * 10000)


# --- запуск ---
print(*solve('A.txt'))
print(*solve('B.txt'))