Dragon Balls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5972    Accepted Submission(s): 2194

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 integer T(0 < T <= 100).
For each case, the first line contains two integers: N and Q (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 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.
  Q A : 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)
 
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 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
 
Author
possessor WC
 
Source
 
题意:
刚开始每个龙珠在对应的城市,T 1,2 表示1龙珠所在的城市里的所有龙珠转移到2龙珠所在的城市中。Q 1 表示询问1龙珠在哪个城市,该城市有几个龙珠,该龙珠的转移次数。
代码:
 //重点是转移次数,和转移量,转移量就是一个根合并到另一个根时,把数量加到另一个根上,每个点的转移次数是这个点的加上他的
//父亲的。一个点合并到一个集合里后注意次数就是这个点合并到该集合之前的转移次数加上该集合根节点的转移次数。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int t,n,m;
int fat[];
int cnt[],num[];
int find(int x)
{
if(fat[x]!=x)
{
int tem=fat[x]; //保存下他的父亲。后面路径压缩直接指向根了。
fat[x]=find(fat[x]);
num[x]+=num[tem];
}
return fat[x];
}
void connect(int x,int y)
{
int a=find(x),b=find(y);
if(a!=b)
{
fat[a]=b;
cnt[b]+=cnt[a];
num[a]++; // 初始转移了一次。
}
}
int main()
{
int a,b;
char ch[];
scanf("%d",&t);
for(int k=;k<=t;k++)
{
printf("Case %d:\n",k);
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
fat[i]=i;
cnt[i]=;
num[i]=;
}
for(int i=;i<=m;i++)
{
scanf("%s",ch);
if(ch[]=='T')
{
scanf("%d%d",&a,&b);
connect(a,b);
}
else
{
scanf("%d",&a);
int ans=find(a);
printf("%d %d %d\n",ans,cnt[ans],num[a]);
}
}
}
return ;
}
 

*HDU3635 并查集的更多相关文章

  1. hdu3635 Dragon Balls(带权并查集)

    /* 题意:有N个城市, 每一个城市都有一个龙珠(编号与城市的编号相同),有两个操作 T A ,B 将标号为A龙珠所在城市的所有的龙珠移动到B龙珠所在城市中! 思路:并查集 (压缩路径的时候将龙珠移动 ...

  2. 【lazy标记得思想】HDU3635 详细学习并查集

    部分内容摘自以下大佬的博客,感谢他们! http://blog.csdn.net/dm_vincent/article/details/7769159 http://blog.csdn.net/dm_ ...

  3. 并查集(Union-Find) 应用举例 --- 基础篇

    本文是作为上一篇文章 <并查集算法原理和改进> 的后续,焦点主要集中在一些并查集的应用上.材料主要是取自POJ,HDOJ上的一些算法练习题. 首先还是回顾和总结一下关于并查集的几个关键点: ...

  4. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  5. 关押罪犯 and 食物链(并查集)

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...

  6. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  7. bzoj1854--并查集

    这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...

  8. [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)

    Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...

  9. [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)

    Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...

随机推荐

  1. Java_Array数组1

    package org.array.demo; /** * 数组可以看成一组相同属性的元素的集合 * 1.静态声明 * 静态声明的格式: * 数组类型[] 标识 = new 数组类型[数组长度]; * ...

  2. 【Alpha】Daily Scrum Meeting第十次

    一.本次Daily Scrum Meeting主要内容 每个人学习情况 测试的任务的安排 Alpha版本展示的具体内容 二.任务安排 学号尾数 昨天做的任务 今天做的任务 任务用时 612 完成将计时 ...

  3. java基础知识(四)java内存机制

    Java内存管理:深入Java内存区域 上面的文章对于java的内存管理机制讲的非常细致,在这里我们只是为了便于后面内容的理解,对java内存机制做一个简单的梳理. 程序计数器:当前线程所执行的字节码 ...

  4. 各大安卓应用市场APP上传提交与收录

    360 提交网址 应用宝 提交网址 百度 提交网址 91 提交网址 安卓 提交网址 PP助手 提交网址 小米 提交网址 华为 提交网址 OPPO 提交网址 魅族 提交网址 乐视 提交网址 豌豆荚 提交 ...

  5. Unity3D LuaComponent(基于ulua)

    LuaComponent可以支持配一个需要执行在这个gameObject上的lua脚本,并且每个gameObject上的lua都是一个实例 using UnityEngine; using LuaIn ...

  6. JScrollBar

    接到了GUI相关的task,从来没看Java的我只好各种百度加看书了.这里介绍了 JScrollBar 的简单应用.    话不多说,直接上代码和效果图. import java.awt.*; imp ...

  7. node06-path

    目录:node01-创建服务器 node02-util node03-events node04-buffer node05-fs node06-path node07-http node08-exp ...

  8. 利用委托与Lambada创建和调用webapi接口

    前言 现在项目中用的是webapi,其中有以下问题:    1.接口随着开发的增多逐渐增加相当庞大. 2.接口调用时不好管理. 以上是主要问题,对此就衍生了一个想法: 如果每一个接口都一个配置文件来管 ...

  9. JavaOO面向对象中的注意点(二)

    1.封装: 封装主要体现在:组装类 和 信息隐藏. a.信息隐藏:①访问修饰符:private ②方法的实现:get/set方法.run()方法-控制流程 b.组装类: ①类的定义:属性.构造.行为( ...

  10. SeekBar进度条简单案例

    SeekBar是进度条.我们使用进度条时,可以使用系统默认的进度条:也可以自定义进度条的图片和滑块图片等 向右拉进度条让图片显示出来 向右拉五角星加载有色进度条 baseSeekBar package ...