무한 격자에서 도시들이 하루에 한 겹씩 번호 순서대로 성장할 때, 모든 도시 쌍이 처음 연결되는 날짜의 합을 구한다.
어려움8그래프BFS기하유니온 파인드아직 제출이 없습니다시간 제한5초메모리 제한768 MBKim is playing a city-building simulation game in an infinite chessboard. Every cell is denoted with two integer (x,y). Two cells are adjacent if they share an edge - formally, cell (x,y) is adjacent with four cells, (x+1,y),(x,y+1),(x−1,y),(x,y−1).
In the beginning (day 0), city i occupies the single cell (x_i,y_i). If a cell is occupied by any city, then it’s called *developed cells*. Thus, in day 0, there is exactly N developed cells.
After each day, the city will grow and expand itself by occupying any undeveloped adjacent cells. Formally, for each city i, let S_i be the set that is adjacent to at least one cell in city i. Starting from city 1 to city n, any undeveloped cell in S_i becomes the part of city i, and become developed.
We call two city u,v “connected”, when you can move between (x_u,y_u) to (x_v,y_v) by only passing between adjacent developed cells. For two city u,v, let f(u,v) the first day where two city u,v become connected. Kim is interested in each values of f(u,v), but there are too much values to consider. Thus, Kim only want to calculate ∑_1≤i<j≤Nf(u,v) for a given first developed cells.
The first line contains the number of city N.
In next N lines, the position of i-th city’s starting cell is given as two integer x_i,y_i.
Print ∑_1≤u<v≤Nf(u,v), when f(u,v) is the first day where two city u,v become connected.