HUD 3938 protal [ 离线并查集 ]

传送们:HDU_3938 portal


思路:主要是实现处理数据,不然便超时,所谓离线处理。

还有注意,离线的姿势,用cnt+=uset[ u] *uset[ v],,如果重新处理所有的集合,重新算的话,又会超时。大哭

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;

int n,m,q;
int uset[10010];
int r1[50010],r2[50010];

struct qq{
    int L,p;
    int res;
}LL[10010];

int cmp1(qq a,qq b)
{
    return a.L<b.L;
}

int cmp2(qq a,qq b)
{
    return a.p<b.p;
}


struct node{
    int u,v;
    int c;
}p[50010];


int cmp(node a,node b)
{
    return a.c<b.c;
}
void make_set(int nn)
{
    for(int i=1;i<=nn;i++)
        uset[i]=-1;
}

int find_set(int x)
{
    if(uset[x]<0) return x;
    uset[x]=find_set(uset[x]);
    return uset[x];
}


int union_set(int x,int y)
{
    int cnt;
    if((x=find_set(x))==(y=find_set(y)))
        return 0;
    if(uset[x]<uset[y])
    {
        cnt=uset[x]*uset[y];
        uset[x]+=uset[y];
        uset[y]=x;
    }
    else
    {
        cnt=uset[x]*uset[y];
        uset[y]+=uset[x];
        uset[x]=y;
    }

    return cnt;
}


int main()
{
    while(scanf("%d%d%d",&n,&m,&q)!=EOF)
    {
        int pos=0;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&p[i].u,&p[i].v,&p[i].c);
        }
        for(int i=0;i<q;i++)
        {
            scanf("%d",&LL[i].L);
            LL[i].p=i;
        
        }

        sort(p,p+m,cmp);
        p[m].c=-1;
        sort(LL,LL+q,cmp1);
        make_set(n);
        int cnt=0;
        for(int i=0;i<q;i++)
        {
            while(LL[i].L>=p[pos].c&&pos<m)
            {
                cnt+=union_set(p[pos].u,p[pos].v);
                pos++;
            }
            LL[i].res=cnt;
        }
        sort(LL,LL+q,cmp2);
        for(int i=0;i<q;i++)
           printf("%d\n",LL[i].res);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值