cdoj203-Islands 【并查集】

http://acm.uestc.edu.cn/#/problem/show/203

Islands

Time Limit: 30000/10000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
 

Deep in the Carribean, there is an island even stranger than the Monkey Island, dwelled by Horatio Torquemada Marley. Not only it has a rectangular shape, but is also divided into an n×m grid. Each grid field has a certain height. Unfortunately, the sea level started to raise and in year i, the level is i meters. Another strange feature of the island is that it is made of sponge, and the water can freely flow through it. Thus, a grid field whose height is at most the current sea level is considered flooded.Adjacent unflooded fields (i.e., sharing common edge) create unflooded areas. Sailors are interested in the number of unflooded areas in a given year.

An example of a 4×5 island is given below. Numbers denote the heights of respective fields in meters.Unflooded fields are darker; there are two unflooded areas in the first year and three areas in the second year.

.*

Input

Multiple Test Cases

The input contains several test cases. The first line of the input contains a positive integer Z20,denoting the number of test cases. Then Z test cases follow, each conforming to the format described in section Single Instance Input. For each test case, your program has to write an output conforming to the format described in section Single Instance Output.

Single Instance Input

The first line contains two numbers n and m separated by a single space, the dimensions of the island, where 1n,m1000. Next n lines contain m integers from the range [1,109] separated by single spaces, denoting the heights of the respective fields. Next line contains an integer T (1T105). The last line contains T integers tj , separated by single spaces, such that 0t1t2tT109

Output

Single Instance Output

Your program should output a single line consisting of T numbers rj , where rj is the number of unflooded areas in year tj . After every number ,you must output a single space!

Sample input and output

Sample InputSample Output
1
4 5
1 2 3 3 1
1 3 2 2 1
2 1 3 4 3
1 2 2 2 2
5
1 2 3 4 5
2 3 1 0 0

思路:

这种联通块的问题一看就知道是并查集的思想。

做法:从高水位到低水位依序进行操作,这样每次都有新的块浮出水面,可以在前面的基础上进行合并集合的操作。
给每个位置分配一个数字,方便合并集合。同时将这些数字也排一个序,降低枚举的复杂度。合并集合时向四周查询浮出水面但是没有合并到同一集合的点进行合并。

代码:

 1 #include <fstream>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <cmath>
 7 #include <cstdlib>
 8 
 9 using namespace std;
10 
11 #define PI acos(-1.0)
12 #define EPS 1e-20
13 #define lll __int64
14 #define ll long long
15 #define INF 0x7fffffff
16 #define INT 2147483646
17 const int N=1005;
18 
19 struct node{
20     int x,y,h;
21 }a[N*N];
22 
23 int n,m,t;
24 int tt[N*100],fa[N*N],cnt[N*100];
25 int xy[2][4]={{0,0,1,-1},{1,-1,0,0}};
26 
27 bool cmp(const node &r1,const node &r2);
28 inline bool Check(int x,int y);
29 inline int Find(int x);
30 
31 int main(){
32     //freopen("D:\input.in","r",stdin);
33     //freopen("D:\output.out","w",stdout);
34     int T;
35     scanf("%d",&T);
36     while(T--){
37         int tn=0;
38         scanf("%d%d",&n,&m);
39         for(int i=0;i<n;i++){
40             for(int j=0;j<m;j++){
41                 scanf("%d",&a[tn].h);
42                 a[tn].x=i;
43                 a[tn++].y=j;
44             }
45         }
46         sort(a,a+n*m,cmp);
47         scanf("%d",&t);
48         for(int i=0;i<t;i++){
49             scanf("%d",&tt[i]);
50         }
51         memset(fa,-1,sizeof(fa));
52         memset(cnt,0,sizeof(cnt));
53         for(int i=t-1,j=0;i>=0;i--){
54             cnt[i]=cnt[i+1];
55             while(1){
56                 if(j>=tn)   break;
57                 int tm=a[j].x*m+a[j].y;
58                 if(a[j].h<=tt[i]||fa[tm]!=-1)   break;
59                 cnt[i]++,fa[tm]=tm;
60                 for(int k=0;k<4;k++){
61                     int tx=a[j].x+xy[0][k];
62                     int ty=a[j].y+xy[1][k];
63                     if(Check(tx,ty)){
64                         int it=tx*m+ty;
65                         if(fa[it]!=-1&&Find(it)!=fa[tm])   fa[fa[it]]=fa[tm],cnt[i]--;
66                     }
67                 }
68                 j++;
69             }
70         }
71         for(int i=0;i<t;i++)
72             printf("%d ",cnt[i]);
73         puts("");
74     }
75     return 0;
76 }
77 bool cmp(const node &r1,const node &r2){
78     return r1.h>r2.h;
79 }
80 inline bool Check(int x,int y){
81     return x>=0&&x<n&&y>=0&&y<m;
82 }
83 inline int Find(int x){
84     return fa[x]==x?x:fa[x]=Find(fa[x]);
85 }
View Code
原文地址:https://www.cnblogs.com/jiu0821/p/4364208.html