HDU3635 Dragon Balls———并查集

【HDU】3635 Dragon Balls (带权并查集 一) 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3635 【问题描述】 有标号为1到n的n个龙珠,分别放在对应标号为1到n的n个城市里。 下面有两种操作: T A B表示把A龙珠所在城市的所有龙珠都转移到B龙珠所在的城市中 Q A 表示查询A,需要知道A龙珠现在所在的城市,A所在的城市有几颗龙珠,A转移到这个城市移动了多少次,分别输出3个整数... 阅读详情

Dragon Balls
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8090 Accepted Submission(s): 2995

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 balls in the world. At first, for the ith dragon ball, the sacred dragon will puts it in the ith city. Through long years, some cities’ dragon ball(s) would be transported to other cities. To save physical strength WuKong plans to take Flying Nimbus Cloud, a magical flying cloud to gather dragon balls.
Every time WuKong will collect the information of one dragon ball, he will ask you the information of that ball. You must tell him which city the ball is located and how many dragon balls are there in that city, you also need to tell him how many times the ball has been transported so far.

Input
The first line of the input is a single positive integerT(0<T<=100).T(0<T<=100).
For each case, the first line contains two integers:NandQ(2<N<=10000,2<Q<=10000).NandQ(2<N<=10000,2<Q<=10000).
Each of the following Q lines contains either a fact or a question as the follow format:
T A BT A B : All the dragon balls which are in the same city with A have been transported to the city the Bth ball in. You can assume that the two cities are different.
QAQA : WuKong want to know X (the id of the city Ath ball is in), Y (the count of balls in Xth city) and Z (the tranporting times of the Ath ball).(1<=A,B<=N)(1<=A,B<=N)

Output
For each test case, output the test case number formated as sample output. Then for each query, output a line with three integers X Y Z  X Y Z saparated by a blank space.

Sample Input
2
3 3
T 1 2
T 3 2
Q 2
3 4
T 1 2
Q 1
T 1 3
Q 1

Sample Output
Case 1:
2 3 0
Case 2:
2 2 1
3 3 2


题意:
n个城市有n颗龙珠,开始时第i颗龙珠在第i个城市,经过一段时间,一些城市的龙珠会转移到其他城市
- T A B表示A所在的城市的龙珠都转移到B城市
每次询问 Q A:
输出

  • A龙珠在哪个城市
  • A 所在的城市有多少颗龙珠
  • A龙珠转移的次数。

#include<bits/stdc++.h>
using namespace std;
const int MAXN=10005;
int par[MAXN];//i 的父亲是 par[i]  (parent)
int ran[MAXN];//记录移动次数
int ad[MAXN];//记录数量
int n,m;

void init()
{
    memset(ran,0,sizeof(ran));//初始时移动次数全是0
    for(int i=1;i<=n;i++)
    {
        par[i]=i;//自己的上级是自己
        ad[i]=1;//每个城市只有一个龙珠
    }
}
int find(int x)//找 x 的根节点
{
    if(x==par[x])   return x;//自己就是根节点的情况下直接输出
    int t=find(par[x]);//x 的根节点是 t
    ran[x] += ran[par[x]];// x 的移动次数等于自己的移动次数加上自己跟着父亲的移动次数
    par[x]=t;//路径压缩
    return  t;
}
void join(int x,int y)
{
    if(x==y)    return ;
    par[x]=y;//y是上级
    ran[x]=1;//根节点的移动次数是1
    ad[y] += ad[x];//
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int z=1;z<=t;z++)
    {
        scanf("%d %d",&n,&m);
        getchar();
        init();
//        printf("%d %d\n",n,m);
        printf("Case %d:\n",z);
        for(int i=0;i<m;i++)
        {
            char ch;
            int a,b,q;
            scanf("%c",&ch);
            if(ch=='T')
            {
                scanf("%d %d",&a,&b);
                int fa=find(a);
                int fb=find(b);
                join(fa,fb);
                getchar();
            }
            if(ch=='Q')
            {
                scanf("%d",&q);
                int fq=find(q);
                printf("%d %d %d\n",fq,ad[fq],ran[q]);
                getchar();
            }
        }
    }
    return 0;
}
android10 部分机型(华为)无法正常更新安装app android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file://storage/emulated/0/文件夹名/***.apk typ=application/vnd.android.package-archive } 阅读详情

相关推荐

【NIFI】实现HANA->ORACLE数据同步

实现 HANA->ORACLE数据同步。

weixin_43159039的博客 1508

HDU 3635 Dragon Balls

题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3635 题目类型: 并查集 题目: Five hundred years later, the number of dragon balls will increase unexpectedly, so it's too difficult for Monkey Kin

D_Double's Journey 1654

手把手教你用USBasp给Arduino UNO烧写程序(ICSP模式,含BootLoader选择指南)

本文详细介绍了如何使用USBasp编程器通过ICSP模式为Arduino UNO烧写程序,包括硬件连接、软件配置、Hex文件处理和BootLoader选择指南。内容涵盖从新手入门到高级优化的全流程,帮助开发者高效完成烧写任务,特别适合需要修复BootLoader或进行量产烧写的用户。

weixin_33958381的博客 365

HDU 3635 Dragon Balls(并查集)

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 ...

- 回忆゛ 231

在 dell precision 7920 tower CentOS 8 Stream 上安裝nvidia驱动程序、Anaconda

简单记录一下如何在实验室dell precision 7920 tower CentOS 8 Stream上安装nvidia显卡驱动。

chen9046的博客 995

esp32移植canopen协议栈(二)

本文续接上一篇文章,之前我们已经搭建好了esp32的开发环境,以及初始工程。接下来我们开始实现can驱动相关,以及使用c++封装can接口。本文使用esp32实现c++实现can驱动层的封装。下一步移植canopen!!!嘻嘻。

qq_50267418的博客 1907

HDU - 3635- Dragon Balls

HDU - 3635- Dragon Balls 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 ...

qq_43750980的博客 490

IEEE GRSL投稿全流程避坑指南:从Latex模板到校样缴费,新手必看的7个关键节点

本文详细解析了IEEE GRSL投稿全流程中的7个关键节点,从LaTeX模板选择到校样缴费,帮助新手避免常见陷阱。特别针对图像格式转换、审稿意见回复、版权转移等环节提供实用技巧,确保投稿过程高效顺利。

weixin_28720573的博客 573

HDU 3635 Dragon Balls (并查集路径压缩)

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.                             ...

mengxianglong123的博客 402

02-Redis 核心特性与数据结构详解:揭秘高性能的技术内核

本文深度拆解 Redis 高性能的技术内核:揭秘其凭“内存存储 + C 语言开发” 实现微秒级延迟、10 万 + QPS 的性能优势;详解 5 种基础数据类型(String/Hash 等)与 4 种扩展数据类型(Stream/Geospatial 等)的特性及场景;还介绍多语言支持、RDB/AOF 持久化、主从复制 / 哨兵 / 集群等高可用方案,助读者掌握 Redis 技术核心与实践逻辑。

蜗牛老师,专注编程领域 996

Python爬虫实战:采集行业协会、研究机构等平台的政策文件列表与PDF链接批量收集系统,支持自动下载、分类归档和数据库管理(SQLite持久化存储)!

我长期专注 Python 爬虫工程化实战,主理专栏 👉 《Python爬虫实战》:从采集策略到反爬对抗,从数据清洗到分布式调度,持续输出可复用的方法论与可落地案例。内容主打一个“能跑、能用、能扩展”,让数据价值真正做到——抓得到、洗得净、用得上。

喵手的博客 640

HDU3635 Dragon Balls【并查集】

Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10519 Accepted Submission(s): 3760 Problem Description Five hundred years later...

海岛Blog 617

大数据知识面试题-Hadoop(2022版)

序列号 内容 链接 1 大数据知识面试题-通用(2022版) 待续… 2 大数据知识面试题-Hadoop(2022版) 待续… 3 大数据知识面试题-MapReduce和yarn(2022版) 待续… 4 大数据知识面试题-Zookeepr (2022版) 待续… 5 大数据知识面试题-Hive (2022版) 待续… 6 大数据知识面试题-Flume(2022版) 待续… 7 大数据知识面试题-Hbase(2022版) 待续… 8 大数据知识面试题-sqoop(....

拉格朗日的学习笔记 1万+

[HDU 3635]Dragon Balls[并查集][路径压缩]

题目链接:[HDU 3635]Dragon Balls[并查集][路径压缩] 题意分析: 'T‘操作,代表将A龙珠所在城市的所有龙珠转移到B龙珠所在的城市。 ’Q‘操作,查询A龙珠所在的城市,该城市的龙珠数和A龙珠总共转移了多少次。 解题思路: 我们使用并查集,将在同一个城市的龙珠并在一起。这样查询所在城市就So easy了。 剩下的问题就是城市的龙珠数和转移次数。 龙珠数,我们可以

闲停 560

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

【hdu3635】Dragon Balls —— 并查集

题目: Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5393 Accepted Submission(s): 2036 Problem Description Five

CFhM_R's Blog 761

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

hdu3635Dragon Balls 并查集

//n个龙珠,在n个城市 //T a b 将包含a的所有龙珠移到b处 //Q a //龙珠a在哪个城市,这个城市有多少龙珠 , 龙珠a移动了多少次 //并查集记录所有龙珠 //对于移动的次数在find更新的时候优化一下就行 #include #include #include using namespace std ; const int maxn = 10010 ; int

ijbuhv的专栏 781

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

HDU 3635 Dragon Balls——并查集

HDU 3635 Dragon Balls 因为这道题卡了我几个小时,后来发现变量命名错误 因此记录一下这里一些注意的点 题目链接 这里的思路是如果要移动的两个城市没有合并时(也就是px!=py),就将x的根节 点更新成y的根节点(x移到y),变换次数也加一,将当前的数量更新成x城的龙 珠数量加y城的龙珠数量(即将以y为根节点的龙珠数量加上以x为根结点的龙珠数量) #include<iostream> #include<cstdio> using namespace std;

qq_52571929的博客 197

hdu 3635 Dragon Balls(并查集)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3635 这题想得好蠢。一开始用cin果断超时了,后来一直wa,纠结一会才发现题目看错了= =!以为是只移动第A颗龙珠,实际上是移动该城市所有龙珠。 这题是赤果果的并查集,重点在于路径压缩的妙处。如果每一栋一次就遍历一边将A城市所有龙珠的移动次数+1,肯定会超时。 #include

Where amazing happens 425

HDU3635 Dragon Balls 并查集

Dragon Balls   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 cou...

cloudy_happy的博客 222

HDU - 3635 Dragon Balls 并查集

题意:1~N个龙珠,放在1~N个城市,有两种操作:T A B 将A所再城市的所有球转移到B所在的城市。 Q X 询问X所在的城市pls,该城市中龙珠数量nm,以及龙珠转移次数trs 题解:并查集模板题,顺带更新一些数据。pls不必更新,因为X所在的集和的根即为城市编号。nm只需在union时将A集合根的nm加到B集合根的nm上,最后输出根的nm。trs则是在每次T中对根++(暂时将操作...

Vincent19999999的博客 84

hdu 3635 Dragon Balls 并查集

题目大意:初始时,有n个龙珠,编号从1到n,分别对应的放在编号从1到n的城市中。现在有两种操作: T A B,表示把A球所在城市全部的龙珠全部转移到B城市。(第一次时,因为A球所在的城市只有一个球,所以只移动1个,如果有多个,则全部移动)。 Q A,表示查询A。要求得到的信息分别是:A现在所在的城市,A所在城市的龙珠数目,A转移到该城市移动的次数(如果没有移动就输出0) 这道...

amily05200的博客 173

HDU 3635 Dragon Balls 带权并查集

还以为蛮高深的并查集呢,原来是个大水题, n个城市n个球,m个查询,T表示把和x号求同样城市的球转移到y号球所在的城市,Q表示询问x号球在哪个城市,该城市有多少个球,x号球转移了几次 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #in

Macan - 生活点滴 311

HDU 3635 Dragon Balls (并查集)

题目大意: 初始时,有n个龙珠,编号从1到n,分别对应的放在编号从1到n的城市中。 现在又2种操作: T A B,表示把A球所在城市全部的龙珠全部转移到B城市。(第一次时,因为A球所在的城市只有一个球,所以只移动1个,如果有多个,则全部移动)。 Q A,表示查询A。要求得到的信息分别是:A现在所在的城市,A所在城市的龙珠数目,A转移到该城市移动的次数(如果没有移动就输出0)

AK_HuangYC的博客 425
上一篇: 畅通工程再续—————最小生成树
下一篇: POJ2485 Highways——————最小生成树
陶鸿杰
博客等级 码龄9年 93粉丝 298原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值