Description
Consider a tropical forrest, represented as a matrix. The cell from the right top corner of the matrix has the coordinates (1,1), and the coordinates of the other cells are determinated by the row and the column on which the cell
is. In some cells of the matrix are placed banana trees; a cell can contain no more than a banana tree. More banana trees which are neighbours on horizontal or vertical form a region of banana trees. In this kind of region, monkey CEKILI is moving easily,
with her well-known agility, from a banana tree to another.
CEKILI is eager and the bananas from a single region are not enough for her. Tarzan wants to help his friend. For that, he may connect exactly k banana tree regions knoting more lianas and so CEKILI could move from a region to another using lianas. Obviously,
Tarzan must choose the regions so that the total number of banana trees from those k regions must be maximum.
Detemine maximum number of banana trees which Tarzan can obtain connecting exactly k regions.
Input
The input has the following structure:
Nr K
x(1) y(1)
y(2) y(2)
...
x(Nr) y(Nr)
Nr is the number of banana trees. K is the number of zones which can be connected. x(i) is the row of the i-th banana tree, while y(i) is the column of the i-th banana tree.
There are Constraints:
? 1 <= Nr <= 16000;
? 1 <= x(i), y(i) <= 10000;
? In the tests used for grading k will never be bigger than the number of regions;
? Two positions are horizontally neighbours if they are on the same row and consecutive columns, respectively vertically neighbours if they are on the same column and on consecutive rows.
Output
The output will contain on the first line the maximum number of banana trees that can be obtained by connecting the k regions.
Sample Input
10 3 7 10 1 1 101 1 2 2 102 1 7 11 200 202 2 1 3 2 103 1
Sample Output
9
在一个集合里就只有当横坐标相等时,纵坐标相差为1, 或者是纵坐标相等时,横坐标差为1。 那么只需分别合并横坐标相等的,和纵坐标相等的情况。用并查集解决。
#include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <set> #include <stack> #include <cctype> #include <algorithm> #define lson o<<1, l, m #define rson o<<1|1, m+1, r using namespace std; typedef long long LL; const int mod = 99999997; const int MAX = 0x3f3f3f3f; const int maxn = 16005; int n, k, f[maxn], rank[maxn]; struct C { int x, y, id; } in[maxn]; int Find(int x) { return x == f[x] ? x : f[x] = Find(f[x]); } bool cmp0 (C a, C b) { if(a.x != b.x) return a.x < b.x; return a.y < b.y; } bool cmp1 (C a, C b) { if(a.y != b.y) return a.y < b.y; return a.x < b.x; } bool cmp2 (int a, int b) { return a > b; } void Union (int p, int q) { int i = Find(p), j = Find(q); if(i == j) return; rank[i] += rank[j]; f[j] = i; rank[j] = 0; } int main() { cin >> n >> k; for(int i = 0; i < n; i++) { in[i].id = i; scanf("%d%d", &in[i].x, &in[i].y); } for(int i = 0; i < n; i++) rank[i] = 1, f[i] = i; sort(in, in+n, cmp0); for(int i = 0; i < n-1; i++) { C cur = in[i], next = in[i+1]; if(cur.x == next.x && next.y-cur.y == 1) Union(cur.id, next.id); } sort(in, in+n, cmp1); for(int i = 0; i < n-1; i++) { C cur = in[i], next = in[i+1]; if(cur.y == next.y && next.x-cur.x == 1) Union(cur.id, next.id); } sort(rank, rank+n, cmp2); int sum = 0; for(int i = 0; i < k; i++) sum += rank[i]; cout << sum << endl; return 0; }