#P2906. [USACO08OPEN] Cow Neighborhoods G

[USACO08OPEN] Cow Neighborhoods G

Description

People who know cows understand how they form "cow neighborhoods." They observed Farmer John’s NN cows (numbered 11 through NN) grazing on a pasture modeled as a plane with XX and YY coordinates starting from 11, where each cow is located at a unique integer lattice point.

Two cows are neighbors if at least one of the following criteria holds:

  1. Their Manhattan distance is at most CC, i.e., XiXj+YiYjC|X_i - X_j| + |Y_i - Y_j| \leq C.
  2. They share a common neighbor; that is, there exists a cow kk such that ii and kk, and jj and kk, both belong to the same group.

Given the cows’ positions and the distance CC, determine the number of "cow neighborhoods" and the number of cows in the largest "cow neighborhood."

For example, consider the pasture below. When C=4C = 4, this pasture has four neighborhoods: one large neighborhood on the left, two neighborhoods of size 11, and a huge neighborhood on the right containing 6060 distinct cows.

.....................................*.................
....*...*..*.......................***.................
......*...........................****.................
..*....*..*.......................*...*.******.*.*.....
........................*.............***...***...*....
*..*..*...*..........................*..*...*..*...*...
.....................................*..*...*..*.......
.....................................*..*...*..*.......
...*................*..................................
.*..*............................*.*.*.*.*.*.*.*.*.*.*.
.*.....*..........................*.*.*.*.*.*.*.*.*.*.*
....*..................................................

Input Format

The first line contains two space-separated integers N,CN, C.

Each of the next NN lines contains two space-separated integers Xi,YiX_i, Y_i, giving the coordinates of a cow.

Output Format

Output a single line with two space-separated integers: the number of "cow neighborhoods" and the size of the largest "cow neighborhood."

4 2 
1 1 
3 3 
2 2 
10 10 

2 3 

Hint

Sample Explanation #1

There are 22 neighborhoods in the sample: one formed by the first three cows, and the other by the last cow. Therefore, the largest neighborhood size is 33.

Constraints

For 100%100\% of the testdata, 1N1051 \leq N \leq 10^5, 1C1091 \leq C \leq 10^9, 1Xi,Yi1091 \leq X_i, Y_i \leq 10^9, and Xi,YiX_i, Y_i are integers.

Translated by ChatGPT 5