hdu 3635

http://acm.hdu.edu.cn/showproblem.php?pid=3635

1-n个城市,对应放着编号1-n的龙珠。

两种操作

T A B 把编号A的龙珠所在城市内的全部龙珠放到有编号B的龙珠的城市内

Q A 查询编号A的龙珠的信息,输出三个数,A所在城市,A所在城市龙珠数目,A转移到该城市所用次数

思路:对球进行并查集维护,sum[a]表示a点移动到目前根的次数,每次合并的时候把A球根节点的sum设为1,这是并查集一个经典的操作,转移次数问题解决。A球所属城市的龙珠数目单开一个数组记录一下即可。所属城市就是根节点编号(这个点的球没有被合并过,所以球编号等于城市编号)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#include <queue>
#include <cmath>
#include <stack>
#include <set>

using namespace std;

int fa[10005],sum[10005],cnt[10005];

int find(int x){
    if(fa[x]!=x){
        int pre=fa[x];
        fa[x]=find(fa[x]);
        sum[x]+=sum[pre];
    }
    return fa[x];
}

int main(){
    int T;
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++){
        int n,q;
        scanf("%d%d",&n,&q);
        for(int i=0;i<=n;i++){
            fa[i]=i;
            cnt[i]=1;
        }
        memset(sum,0,sizeof(sum));
        printf("Case %d:\n",cas);
        while(q--){
            char op[5];
            scanf("%s",op);
            if(op[0]==‘T‘){
                int a,b;
                scanf("%d%d",&a,&b);
                int pa=find(a);
                int pb=find(b);
                if(pa!=pb){
                    fa[pa]=pb;
                    cnt[pb]+=cnt[pa];
                    sum[pa]=1;
                }
            }
            else{
                int a;
                scanf("%d",&a);
                int rt=find(a);
                printf("%d %d %d\n",rt,cnt[rt],sum[a]);
            }
        }
    }
    return 0;
}

时间: 2024-08-25 22:25:29

hdu 3635的相关文章

HDU 3635 Dragon Balls(带权并查集)

题目地址:HDU 3635 加权并查集水题. 用num数组维护该城市有多少龙珠,用times数组维护每个龙珠运输了多少次.num数组在合并的时候维护.times数组由于每个都不一样,所以要在找根的时候递归来全部维护. 最终,x龙珠所在的城市就是x节点所在的根,x结点所在的跟的num数组值是该城市的龙珠数.times[x]为该龙珠运输了多少次. 代码如下: #include <iostream> #include <cstdio> #include <string> #i

HDU 3635 延缓更新的并查集

Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2839    Accepted Submission(s): 1097 Problem Description Five hundred years later, the number of dragon balls will increase unexpecte

HDU 3635 Dragon Balls 七龙珠 Union Find算法

孙悟空要寻找七龙珠,这回是七龙珠的增强版了,因为这些龙珠会衍生,最后不止七颗龙珠了. 悟空带着布玛的龙珠雷达探测器出发了,却发现布玛的龙珠雷达探测器的程序太垃圾了,所以找到我们这些ACM高手为龙珠雷达探测器写个程序,要求可以显示某颗龙珠所在的城市的位置,该龙珠所在的城市共有多少颗龙珠,龙珠移动过的次数. 布玛是个有钱人啊,写个程序我要价5百万,不算过分吧.因为本程序需要用到Union Find(并查集)算法,而且最困难的部分是如何压缩路径,不压缩路径自然容易做到,要压缩路径可以使得程序加快很多,

HDU 3635 Dragon Balls(并查集)

Dragon Balls Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 64   Accepted Submission(s) : 26 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Five hundred years later

HDU 3635 Dragon Balls (并查集)

Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3360    Accepted Submission(s): 1303 Problem Description Five hundred years later, the number of dragon balls will increase unexpect

hdu 3635 Dragon Balls(并查集应用)

Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, so it's too difficult for Monkey King(WuKong) to gather all of the dragon balls together. His country has N cities and there are exactly N dragon bal

HDU 3635 Dragon Balls

题意: 一开始每个城市里有一个龙珠  每次T操作使得A龙珠所在城市的所有龙珠飞到B龙珠所在城市  Q操作询问X龙珠在哪个城市  那个城市里有几个龙珠  X飞过几次 思路: 从T操作的功能来看想到并查集  即  每次找到所在城市(找根)飞到另一个城市(并集) 但是并查集无法统计每个龙珠飞过几次  这时候我们可以给并查集加上一个权值 每次飞的时候使A集合的权值++  这样当询问X飞过几次的时候  只需要从X开始找根  并把路径上的权值求和即可 代码: #include<cstdio> #inclu

hdu 3635 Dragon Balls(并查集技巧)

题意:n个点m次询问,两种操作:1.将含有龙珠i的集合加入含有龙珠j的集合中:2.查询龙珠i所在堆的编号,龙珠个数,龙珠i的搬运次数: 思路:并查集,数组分别维护关系.数量.搬运次数: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n,m; int fa[500010],time[500010],num[500010]; void init() { for(

hdu 3635 并查集

每个结点除了维护父亲结点编号以外,多维护一个sum值和r值,表示该子树的总结点数(如果该结点是根则sum值有效)以及它到根的距离(即被运输了几次),然后在路径压缩和集合合并的时候顺便维护这两个值即可. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int N = 10001; 7 int f[N]; 8 int sum[N];