CCCP_2016四省 HDU5925

本文介绍了一个算法问题,即计算一个存在坏点的大规模地图中所有好点形成的联通块数量及大小。通过离散化和深度优先搜索的方法解决了该问题。
#include <bits/stdc++.h>

using namespace std;
using ll = long long ;
const int dirx[] = {0, 0, 1, -1};
const int diry[] = {1, -1, 0, 0};
const int maxn = 207;
bool vis[maxn*3][maxn*3]; //离散化后是图的标记
vector<int> xx, yy; //离散化后的图,每个点代表一个长度
pair<int, int> bad[maxn]; //输入的bad_point
int cntx, cnty;

bool inside(int i, int j)
{
    return i >= 0 && i < cntx && j >= 0 && j < cnty;
}
ll dfs(int i, int j)
{
    ll res = 0;
    res += xx[i] * 1LL * yy[j];
    vis[i][j] = 1;
    for(int k = 0;k < 4;k ++) {
        int ni = i + dirx[k];
        int nj = j + diry[k];
        if(inside(ni, nj) && !vis[ni][nj]) res += dfs(ni, nj);
    }
    return res;
}


int main()
{
    int T, Case = 0;
    scanf("%d", &T);
    while(T --) {
        xx.clear();
        yy.clear();
        memset(vis, 0, sizeof(vis));
        printf("Case #%d:\n", ++Case);
        int n, m;
        scanf("%d %d", &n, &m);
        int bad_num;
        scanf("%d", &bad_num);
        for(int i = 1;i <= bad_num;i ++) scanf("%d %d", &bad[i].first, &bad[i].second);

        unordered_map<int, int> umx;
        vector<int> x;
        x.push_back(0); //为了把第一个点x=1放入
        x.push_back(1);
        x.push_back(n);
        for(int i = 1;i <= bad_num;i ++) {
            x.push_back(bad[i].first);
        }
        sort(x.begin(), x.end());
        x.erase(unique(x.begin(), x.end()), x.end());
        int nx = x.size();
        for(int i = 1;i < nx;i ++) {
            if(x[i] - x[i-1] != 1) {
                xx.push_back(x[i] - x[i-1] - 1); //把中间长度放进去
            }
            xx.push_back(1);  //把x[i]代表的1个长度放进去
            umx[x[i]] = xx.size();
        }

        unordered_map<int, int> umy;
        vector<int> y;
        y.push_back(0);
        y.push_back(1);
        y.push_back(m);
        for(int i = 1;i <= bad_num;i ++) {
            y.push_back(bad[i].second);
        }
        sort(y.begin(), y.end());
        y.erase(unique(y.begin(), y.end()), y.end());
        int ny = y.size();
        for(int i = 1;i < ny;i ++) {
            if(y[i] - y[i-1] != 1) {
                yy.push_back(y[i] - y[i-1] - 1);
            }
            yy.push_back(1);
            umy[y[i]] = yy.size();
        }

        cntx = xx.size();
        cnty = yy.size();

//        for(int i = 0;i < cntx;i ++) printf("%d ", xx[i]); puts("");
//        for(int i = 0;i < cnty;i ++) printf("%d ", yy[i]); puts("");

        for(int i = 1;i <= bad_num;i ++) {
//            printf("%d %d\n", umx[bad[i].first],umy[bad[i].second] );
            vis[umx[bad[i].first]-1][umy[bad[i].second]-1] = 1;
        }

        vector<ll> res;
        for(int i = 0;i < cntx;i ++) {
            for(int j = 0;j < cnty;j ++) {
                if(!vis[i][j]) res.push_back(dfs(i,j));
            }
        }

        sort(res.begin(), res.end());
        n = res.size();
        printf("%d\n", n);
        for(int i = 0;i < n;i ++) {
            printf("%lld%c",res[i], i == n - 1 ? '\n':' ');
        }
    }
    return 0;
}

TanBig, a friend of Mr. Frog, likes eating very much, so he always has dreams about eating. One day, TanBig dreams of a field of coconuts, and the field looks like a large chessboard which has R rows and C columns. In every cell of the field, there is one coconut. Unfortunately, some of the coconuts have gone bad. For sake of his health, TanBig will eat the coconuts following the rule that he can only eat good coconuts and can only eat a connected component of good coconuts one time(you can consider the bad coconuts as barriers, and the good coconuts are 4-connected, which means one coconut in cell (x, y) is connected to (x - 1, y), (x + 1, y), (x, y + 1), (x, y - 1). 

Now TanBig wants to know how many times he needs to eat all the good coconuts in the field, and how many coconuts he would eat each time(the area of each 4-connected component). 
InputThe first line contains apositiveinteger T(T10T≤10) which denotes the test cases. T test cases begin from the second line. In every test case, the first line contains two integers R and C, 0<R,C1090<R,C≤109 the second line contains an integer n, the number of bad coconuts, 0n2000≤n≤200 from the third line, there comes n lines, each line contains two integers, xixi and yiyi, which means in cell(xi,yixi,yi), there is a bad coconut. 

It is guaranteed that in the input data, the first row and the last row will not have bad coconuts at the same time, the first column and the last column will not have bad coconuts at the same time. 
OutputFor each test case, output "Case #x:" in the first line, where x denotes the number of test case, one integer k in the second line, denoting the number of times TanBig needs, in the third line, k integers denoting the number of coconuts he would eat each time, you should output them in increasing order.Sample Input
2

3 3
2
1 2
2 1

3 3
1
2 2
Sample Output
Case #1:
2
1 6
Case #2:
1
8

题目链接:https://vjudge.net/contest/209266#problem/D

题意:给出一个N*M的地图(N,M<=1E9), 然后给你一些坏点,问你最后图中有多少联通块,并按从小到大的顺序输出这些联通快的大小。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值