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. Coursera系列-R Programming第三周-词法作用域

    完成R Programming第三周 这周作业有点绕,更多地是通过一个缓存逆矩阵的案例,向我们示范[词法作用域 Lexical Scopping]的功效.但是作业里给出的函数有点绕口,花费了我们蛮多心 ...

  2. php glob()函数实现目录文件遍历与寻找与模式匹配的文件路径

    采用PHP函数glob实现寻找与模式匹配的文件路径,主要讨论glob()函数的作用和用法,利用glob函数读取目录比其它的要快N倍,因为glob函数是内置函数处理起来自然要快. 一,函数原型 arra ...

  3. redis中的key设置过期时间

    EXPIRE key seconds 为给定  key  设置生存时间,当  key  过期时(生存时间为  0  ),它会被自动删除. 在 Redis 中,带有生存时间的  key  被称为『易失的 ...

  4. T-SQL实用查询之分析数据库表的大小

    IF OBJECT_ID('tempdb..#TB_TEMP_SPACE') IS NOT NULL DROP TABLE #TB_TEMP_SPACE GO CREATE TABLE #TB_TEM ...

  5. 在vs中char类型的实参与LPCWSTR类型的形参类型不兼容怎么解决?

    今天在做 COS脚本解释器的时候,遇到了这个问题 先了解一下 LPCTCHAR 这个东东 LPCTSTR用来表示你的字符是否使用UNICODE, 如果你的程序定义了UNICODE或者其他相关的宏,那么 ...

  6. angularjs 笔记(1) -- 引导

    首先: 1,引入angularJS文件,<script type="text/javascript" src="angularjs.min.js"> ...

  7. php魔术方法和魔术常量

    1.__construct() 实例化对象时被调用, 当__construct和以类名为函数名的函数同时存在时,__construct将被调用,另一个不被调用. 2.__destruct() 当删除一 ...

  8. java 项目中几种O实体类的概念

    经常会接触到vo,do,dto的概念,本文从领域建模中的实体划分和项目中的实际应用情况两个角度,对这几个概念进行简析. 得出的主要结论是:在项目应用中,vo对应于页面上需要显示的数据(表单),do对应 ...

  9. SQL入门语句之INSERT、UPDATE和DELETE

    一.SQL入门语句之INSERT insert语句的功能是向数据库的某个表中插入一个新的数据行 1.根据对应的字段插入相对应的值 insert into table_name(字段A, 字段B, 字段 ...

  10. 教你几种在SQLServer中删除重复数据方法(转)

    转载地址:http://www.jb51.net/article/22980.htm 方法一 复制代码 代码如下: declare @max integer,@id integer declare c ...