*HDU3635 并查集
Dragon Balls
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5972 Accepted Submission(s): 2194
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.
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)
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.
3 3
T 1 2
T 3 2
Q 2
3 4
T 1 2
Q 1
T 1 3
Q 1
2 3 0
Case 2:
2 2 1
3 3 2
//重点是转移次数,和转移量,转移量就是一个根合并到另一个根时,把数量加到另一个根上,每个点的转移次数是这个点的加上他的
//父亲的。一个点合并到一个集合里后注意次数就是这个点合并到该集合之前的转移次数加上该集合根节点的转移次数。
#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 并查集的更多相关文章
- hdu3635 Dragon Balls(带权并查集)
/* 题意:有N个城市, 每一个城市都有一个龙珠(编号与城市的编号相同),有两个操作 T A ,B 将标号为A龙珠所在城市的所有的龙珠移动到B龙珠所在城市中! 思路:并查集 (压缩路径的时候将龙珠移动 ...
- 【lazy标记得思想】HDU3635 详细学习并查集
部分内容摘自以下大佬的博客,感谢他们! http://blog.csdn.net/dm_vincent/article/details/7769159 http://blog.csdn.net/dm_ ...
- 并查集(Union-Find) 应用举例 --- 基础篇
本文是作为上一篇文章 <并查集算法原理和改进> 的后续,焦点主要集中在一些并查集的应用上.材料主要是取自POJ,HDOJ上的一些算法练习题. 首先还是回顾和总结一下关于并查集的几个关键点: ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- 关押罪犯 and 食物链(并查集)
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- bzoj1854--并查集
这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...
- [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)
Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...
- [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)
Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...
随机推荐
- [Linux] 账户管理命令(一)
用户和用户组 Linux用户组的所有信息都存放在/etc/group文件中.具有某种共同特征的用户集合起来就是用户组(Group).用户组(Group)配置文件主要有 /etc/group和/etc/ ...
- PHP数组函数: array_walk()与 array_map() 的区别
详细的介绍如下: PHP数组函数: array_walk() PHP数组函数: array_map() 实际应用中的一点区别与总结: array_walk() 主要用于对某个数组的迭代,相当于 for ...
- exception catch doesn't work?? (python 3)
exception catch doesn't work?? (python 3) except u.URLError, e: ^ SyntaxError: invalid syntax in Pyt ...
- 在 IIS 6 和 IIS 7中配置Https,设置WCF同时支持HTTP和HTPPS,以及使用HttpWebRequest和HttpClient调用HttpS
IIS 7 ,给IIS添加CA证书以支持https IIS 6 架设证书服务器 及 让IIS启用HTTPS服务 WCF IIS 7中配置HTTPS C#利用HttpWebRequest进行post请求 ...
- DNSmasq – 配置DNS和DHCP
DNSmasq是一个小巧且方便地用于配置DNS和DHCP的工具,适用于小型网络.它提供了DNS功能和可选择的DHCP功能可以取代dhcpd(DHCPD服务配置)和bind等服务,配置起来更简单,更适用 ...
- Laravel 创建数据库
1.根目录输入 php artisan migrate 2.创建表 php artisan migrate:make create_authors_table --table authors --cr ...
- SQL实现表名更改,列名更改,约束更改
修改表名 格式:sp_rename tablename,newtablename sp_rename tablename,newtablename 修改字段名 格式:sp_rename 'tablen ...
- LeetCode 26 Remove Duplicates from Sorted Array
Problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...
- navigation和tabbar上的文字.图片 自定义
[[UITabBarItem appearance] setTitleTextAttributes:@{ UITextAttributeTextColor : [UIColor blackColor] ...
- tomcat 常用优化配置
1.精简Tomcat和配置文件 1.删除不需要的管理应用和帮助应用,提高tomcat安全性. # 删除webapps下所有文件 # rm –fr $CATALINA_HOME/webapps/* # ...