cdoj 2015数据结构专题:J - 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  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 Input Sample 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

用并查集实现。

问题即求某一年有几个连通的并查集。并查集容易加点,不容易删除点,所以要按年份倒推,通过离线处理实现。而最后询问的年份有规定0≤t1≤t2≤⋯≤tT≤10^9,即年份依次递增,所以可以根据年份从后往前循环实现。

读入一次的所有数据后,根据山的高度对所有山进行降序排序。从大到小遍历年份,加入高度大于年份的山,将此山的根节点挂在四周不比它矮的山的根节点上(根节点不同时)。应注意,不能通过统计一个年份海面上的山有多少个父节点来得到结果,这样会超时,必须每次在新加山时,就通过对此年的第二年的数据进行更改得到结果。具体做法是加入一个山时,先使此年独立片区加一,遍历四周时,如果挂到了其他山上,独立片区减一,否则不变。

最后从年份小到大依次输出结果。


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

struct node
{
    int hi;
    int id;
}s[1000005];
int higt[1000005];
int fa[1000005];
int out[100005];
int year[100005];
int n,m;

bool cmp(node x, node y)
{
    return x.hi > y.hi;
}

int findset(int x)
{
    if (x == fa[x])
        return x;
    else
        return fa[x] = findset(fa[x]);
}

void uni(int x, int y,int ye)
{
    int fx = findset(x);
    int fy = findset(y);
    if(fx != fy && higt[y] <= higt[x])
    {
        fa[fy] = fx;
        out[ye]--;
    }
}

void upnode(int loc,int ye)
{
    int i, j;
    i = s[loc].id / m;
    j = s[loc].id - i * m;
    if(j - 1 >= 0)
        uni(i * m + (j - 1), s[loc].id, ye);
    if(i - 1 >= 0)
        uni((i - 1) * m + j, s[loc].id, ye);
    if(j + 1 < m)
        uni(i * m + (j + 1), s[loc].id, ye);
    if(i + 1 < n)
        uni((i + 1) * m + j, s[loc].id, ye);
}

void build()
{
    for(int i = 0; i < n * m; i++)
    {
        fa[i] = i;
        s[i].id = i;
    }
}

void solve(int t)
{
    int max = s[0].hi;
    int con = t - 1;
    out[t] = 0;
    while(year[con] > max - 1)
    {
        out[con] = 0;
        con--;
    }
    int i;
    for(i = 0; con >= 0; con--)
    {
        out[con] = out[con + 1];
        while(s[i].hi > year[con])
        {
            out[con]++;
            upnode(i,con);
            i++;
        }
    }
}

int main()
{
    int z;
    scanf("%d",&z);
    while(z--)
    {
        scanf("%d%d", &n, &m);
        build();
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
            {
                scanf("%d", &higt[i * m + j]);
                s[i * m + j].hi = higt[i * m + j];
            }
        int t;
        scanf("%d",&t);
        for(int i = 0; i < t; i++)
            scanf("%d",&year[i]);
        sort(s, s + m * n, cmp);
        solve(t);
        for(int i = 0; i < t; i++)
            printf("%d ",out[i]);
        printf("\n");
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值