Colonization

시간 제한4초메모리 제한1024 MB

요약
두 집단 사이의 평균 거리가 가장 작은 두 집단을 반복해서 합치고, 그 합병 순서와 거리를 출력한다.
난이도

어려움10점 중 8점

유형
유니온 파인드, 구현, 완전 탐색, 수학
정답자
아직 제출이 없습니다

문제

The yoghurt bottle is populated with NN bacteria. But bacteria want company, so they form colonies, and this happens in the following way. Initially each bacterium is a colony consisting of itself. While there are at least two colonies in the yoghurt, two nearest colonies are chosen at each step and merged together.

Initially for each pair of bacteria uu and vv the distance d_u,vd\_{u,v} between them is known. The distance for the colonies GG and HH is calculated as the average distance between all pairs of bacteria: 1∣G∣⋅∣H∣∑_u∈G∑_v∈Hd_u,v\frac{ 1 }{|G| \cdot |H|} \sum\_{u \in G} \sum\_{v \in H} d\_{u, v}

Bacteria aren't very particular when it comes to accuracy --- you cannot expect too much from single-cell organisms, anyway. So when there are several pairs of colonies with the distance within 10−610^{-6} of the minimal, then any of these pairs can be selected for merging at this step.

You are to model the colony merging process and find one of the possible scenarios.

입력

The first line of the input file contains an integer NN -- the number of bacteria (2≤N≤20142 \leq N \leq 2014).

The following NN lines contain the description of the distance matrix (d_i,j)(d\_{i,j}). Each line contains NN characters. The character d_i,jd\_{i,j} in the jj-th position of the ii-th line specifies the distance between the ii-th and jj-th bacteria.

It is guaranteed that d_i,i=0d\_{i,i} = 0, d_i,j=d_j,id\_{i, j} = d\_{j, i}, 0≤d_i,j≤90 \leq d\_{i, j} \leq 9 for all ii, jj = 1,…,N1, \ldots, N.

출력

Assign numbers to each initial colony, from 11 to NN. The colony obtained on the ii-th step, i=1,…,N−1i = 1, \ldots, N-1, will have the number N+iN + i.

The input file must contain N−1N-1 lines. The ii-th line must contain three numbers: the numbers of colonies merged on the ii-th step, and the distance between them with an absolute or relative error not greater than 10−610^{-6} (1≤i≤N−11 \le i \le N-1).

예제1

  1. 예제 1

    입력
    4
    0146
    1025
    4203
    6530
    
    예상 출력
    1 2 1.000000
    3 4 3.000000
    5 6 4.250000