POJ 1838 Banana (並查集),poj1838banana
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 37 101 1101 12 2102 17 11200 2022 13 2103 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, rusing 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;}
北大acm題中有什題使用並查集來做的?
POJ 1611 The Suspects 最基礎的並查集
POJ 2524 Ubiquitous Religions 最基本的並查集
POJ 1182 食物鏈 並查集的拓展
注意: 只有一組資料;
要充分利用題意所給條件:有三類動物A,B,C,這三類動物的食物鏈
構成了有趣的環形。A吃B, B吃C,C吃A。也就是說:只有三個group
POJ 2492 A Bug's Life 並查集的拓展
法一:深度優先遍曆
每次遍曆記錄下該點是男還是女,只有:男-〉女,女-〉男滿足,否則,找到同性戀,結束程式。
法二:二分圖匹配
法三:並查集的拓展:和1182很像,只不過這裡就有兩組,而1182是三組,1611無限制
POJ 1861 Network == zju_1542 並查集+自訂排序+貪心求"最小產生樹"
答案不唯一,不過在ZOJ上用QSORT()和SORT()都能過,在POJ上只有SORT()才能過...
POJ 1703 Find them, Catch them 並查集的拓展
這個和POJ 2492 A Bug's Life很像,就是把代碼稍微修改了一下就AC了!
注意:And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. 就是說只有兩個組。
POJ 2236 Wireless Network 並查集的應用
需要注意的地方:1、並查集;2、N的範圍,可以等於1001;3、從N+1行開始,第一個輸入的可以是字串。
POJ 1988 Cube Stacking 並查集很好的應用
1、與 銀河英雄傳說==NOI2002 Galaxy一樣;2、增加了一個數組behind[x],記錄戰艦x在列中的相對位置;3、詳細解題報告見銀河英雄傳說。
JOJ 1905 Freckles == POJ 2560 最小產生樹
法一:Prim演算法;法二:並查集實現Kruskar演算法求最小產生樹
poj1611我什地方錯了(並查集+水題,不過我用STL做的)