一个并查集问题的优化(CDOJ 203)

一个多月前在成电OJ上看到一个并查集的问题,当时刚看完一些基础的数据结构,所以就试着做了。当然没有做出来。这题坑就在于:每组测试数据有10^5个,地图还是1000*1000的。挨着个每次都搜一遍不太可能。容易超时。

解决方法:潮水高度是递增的。所以想象一个退潮的过程,把每次退潮露出的岛屿记录下来,从最后最高的潮水高度算起。把露出的岛屿加入集合,每个测试数据对应的答案可以接着在下一组继续使用。

Islands

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 Z≤20,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 1≤n,m≤1000. Next n lines contain m integers from the range [1,10^9] separated by single spaces, denoting the heights of the respective fields. Next line contains an integer T (1≤T≤105). The last line contains T integers tj , separated by single spaces, such that 0≤t1≤t2≤⋯≤tT≤10^9
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

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

Sample Output

2 3 1 0 0
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
class Field{
public:
    int height, father;
    bool under;
};

int Z;  int row, col, T;
int res[100000];
vector<int> height_list[100000];
vector<int> t;
int i,j,result,temp;
Field field[1000][1000];
int Father(int a, int b){
    if (field[a][b].father!=a*col+b)
        field[a][b].father=Father(field[a][b].father/col, field[a][b].father%col);
    return field[a][b].father;
}

void Merge(int a, int b, int c, int d){
    int f1, f2, x, y;
    f1=Father(a, b);    f2=Father(c, d);
    if(f1==f2) return;
    else{
        result--;
        x=field[a][b].father/col;
        y=field[a][b].father%col;
        field[x][y].father=f2;
    }
}

void Check(int r, int c){
    if (r+1<row && !field[r+1][c].under) {Merge(r, c, r+1, c); }
    if (r-1>=0 && !field[r-1][c].under) {Merge(r, c, r-1, c); }
    if (c+1<col && !field[r][c+1].under) {Merge(r, c, r, c+1); }
    if (c-1>=0 && !field[r][c-1].under) {Merge(r, c, r, c-1); }
}

void Solve(){
    int x, y, m, n;
    result=0;
    for(m=t.size()-1; m>=0; m--){
        for(n=height_list[m].size()-1; n>=0; n--){
            x=height_list[m][n]/col;   y=height_list[m][n]%col;
            field[x][y].under=false;
            result++;   Check(x, y);
        }
        res[m]=result;
    }
}

int main(){
    scanf("%d", &Z);
    while(Z--){
        result=0;
        t.clear();
        for (i=0; i<100000; i++) height_list[i].clear();

        scanf("%d%d", &row, &col);
        for(i=0; i<row; i++){
            for(j=0; j<col; j++){
                scanf("%d", &field[i][j].height);
                field[i][j].under=true;
                field[i][j].father=i*col+j;
            }
        }

        scanf("%d", &T);
        for(i=0; i<T; i++){
            scanf("%d", &temp);
            t.push_back(temp);
        }

        for(i=0; i<row; i++){
            for(j=0; j<col; j++){
                temp=lower_bound(t.begin(), t.end(), field[i][j].height)-t.begin();
                if(temp>0) height_list[temp-1].push_back(i*col+j);
            }
        }
        Solve();
        for(i=0; i<(int)t.size(); i++) printf("%d ", res[i]);
        printf("\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值