#P2906. Cow Communities

    ID: 16164 Type: Default 1000ms 256MiB

Cow Communities

Cow Communities

Farmer John's cows graze on a vast pasture where each cow occupies a unique integer coordinate. Two cows, with coordinates \((x_i, y_i)\) and \((x_j, y_j)\), are considered neighbors if at least one of the following conditions holds:

  • Their Manhattan distance satisfies \( |x_i - x_j| + |y_i - y_j| \leq C \), or
  • They share a common neighbor (i.e. there exists a cow \( k \) such that cow \( i \) and cow \( k \) are in the same group and cow \( j \) and cow \( k \) are in the same group).

This defines a connectivity relation among the cows. In essence, if two cows are directly connected through the Manhattan distance condition or indirectly connected via a chain of neighbors, they belong to the same cow community.

Your task is to determine the number of distinct cow communities (i.e. connected components) and the size of the largest community.

inputFormat

The first line contains two integers \( N \) and \( C \), where \( N \) is the number of cows and \( C \) is the Manhattan distance threshold.

Each of the following \( N \) lines contains two integers \( x \) and \( y \), representing the unique coordinates of a cow in the pasture.

outputFormat

Output two integers separated by a space: the number of cow communities and the size of the largest community.

sample

3 1
1 1
1 2
3 3
2 2