UVA 1665(并查集题目)

本题目的意思是给定一个n*m的数阵(n,m<=1000),并给定T个递增的权值序列,对每一个要求输出大于该值的所有格子组成的四连块个数。

思路:

给所有格子按权排序,从大到小处理这T个数,每次只需把满足条件的格子加入并查集,并在合并过程中动态统计联通分量的个数。

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <list>
#include <cstdlib>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cassert>
#define ALL(a) a.begin(), a.end()
#define clr(a, x) memset(a, x, sizeof a)
#define X first
#define Y second
#define pb push_back
#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define rep1(i,x,y) for(int i=x;i<=y;i++)
#define rep(i,n) for(int i=0;i<(int)n;i++)
using namespace std;
const double eps = 1e-10;
typedef long long LL;
typedef long long ll;
typedef pair<int, int> pii;
const int oo =0x3f3f3f3f;

const int N = 1e6+100;
typedef pair<int,pii> pip;
pip a[N];
int n,m , b[N] ,all = 0,pa[N] ,ans[N];
int find(int x){
   if(x == -1) return -1;
   return (pa[x]==x ? x:pa[x]=find(pa[x]));
}
const int dx[]={-1,0,1,0};
const int dy[]={0,1,0,-1};
void add(pii u){
   int pre = u.X*m+u.Y;
   pa[pre] = pre; all++;
   rep(d,4){
      int nx = u.first+dx[d], ny = u.second+dy[d];
      if(nx>=0&&nx<n&&ny>=0&&ny<m){
          int p = nx*m+ny , px = find(p);
          if(px==-1) continue;
          int ppre = find(pre);
          if(ppre != px){
               all--; pa[ppre] = px;
          }
      }
   }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
       scanf("%d %d",&n,&m);
       int x , q ,cnt = 0;
       rep1(i,0,n-1) rep1(j,0,m-1) scanf("%d",&x),a[cnt++]=pip(x,pii(i,j));
       sort(a,a+cnt);
       rep(i,cnt) pa[i] = -1;
       scanf("%d",&q);
       rep1(i,1,q) scanf("%d",&b[i]);
       int p = cnt-1;  all = 0;
       for(int i=q;i>=1;i--){
          while(p>=0 && a[p].first > b[i]) add(a[p--].second);
          ans[i] = all;
       }
       rep1(i,1,q){
          printf("%d ",ans[i]);
       } printf("\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值