hdu3635 Dragon Balls(带权并查集)
/*
题意:有N个城市, 每一个城市都有一个龙珠(编号与城市的编号相同),有两个操作
T A ,B 将标号为A龙珠所在城市的所有的龙珠移动到B龙珠所在城市中! 思路:并查集 (压缩路径的时候将龙珠移动的次数进行更新)
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define M 10005
using namespace std; int f[M];//表示龙珠 i 所在的城市标号
int Tcnt[M];//记录每个龙珠移动的次数
int Scnt[M];//记录每个城市中龙珠总个数 int getFather(int x){
if(x==f[x])
return x; int ff=getFather(f[x]);
Tcnt[x]+=Tcnt[f[x]];//每一个龙珠移动的次数+=其依附的父亲龙珠移动的次数
f[x]=ff;
return f[x];
} void Union(int a, int b){
int fa=getFather(a);
int fb=getFather(b);
if(fa==fb) return;
f[fa]=fb;
Scnt[fb]+=Scnt[fa];//将fa城市的龙珠全部移动到fb城市中!
Scnt[fa]=;
Tcnt[fa]+=;//a球移动次数+1
} int main(){
int t, a, b;
int n, m;
char ch[];
scanf("%d", &t);
for(int cc=; cc<=t; ++cc){
printf("Case %d:\n", cc);
scanf("%d%d", &n, &m);
memset(Tcnt, , sizeof(int)*(n+));
for(int i=; i<=n; ++i)
f[i]=i, Scnt[i]=;
while(m--){
scanf("%s", ch);
if(ch[]=='T'){
scanf("%d%d", &a, &b);
Union(a, b);
}
else {
scanf("%d", &a);
int ff = getFather(a);
printf("%d %d %d\n", ff, Scnt[ff], Tcnt[a]);
}
}
}
return ;
}
hdu3635 Dragon Balls(带权并查集)的更多相关文章
- hdu 3635 Dragon Balls (带权并查集)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- POJ 1703 Find them, Catch them(带权并查集)
传送门 Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42463 Accep ...
- (中等) POJ 1703 Find them, Catch them,带权并查集。
Description The police office in Tadu City decides to say ends to the chaos, as launch actions to ro ...
- POJ 1703 Find them, Catch them【种类/带权并查集+判断两元素是否在同一集合/不同集合/无法确定+类似食物链】
The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the ...
- [NOIP摸你赛]Hzwer的陨石(带权并查集)
题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...
- poj1417 带权并查集 + 背包 + 记录路径
True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2713 Accepted: 868 Descrip ...
- poj1984 带权并查集(向量处理)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5939 Accepted: 2 ...
- 【BZOJ-4690】Never Wait For Weights 带权并查集
4690: Never Wait for Weights Time Limit: 15 Sec Memory Limit: 256 MBSubmit: 88 Solved: 41[Submit][ ...
- hdu3038(带权并查集)
题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 题意: n表示有一个长度为n的数组, 接下来有m行形如x, y, d的输入, 表示 ...
随机推荐
- 20145301&20145321&20145335实验五
20145301&20145321&20145335实验五 这次实验我的组员为:20145301赵嘉鑫.20145321曾子誉.20145335郝昊 实验内容详见:实验五
- 决策树 -- C4.5算法
C4.5是另一个分类决策树算法,是基于ID3算法的改进,改进点如下: 1.分离信息 解释:数据集通过条件属性A的分离信息,其实和ID3中的熵: 2.信息增益率 解释:Gain(A)为获的A ...
- tomcat 运行程序很慢 运行一段时间就报错: java.lang.OutOfMemoryError: PermGen space
java.lang.OutOfMemoryError: PermGen space 全称是Permanent Generation space,是指内存的永久保存区域. 由于这块内存主要是被JVM存放 ...
- [leetcode 48] rotate image
1 题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwi ...
- 两个实用的工具推荐:ResxManager和ValueInjecter
一.ResxManager 1. 资源文件编辑工具ResxManager,这个工具可以在一个界面中编辑所有语言的内容. 2. 可以新增.删除key. 3. 注意:如果是新建的Resx,一定要有一 ...
- 使用git将代码push到osc上
1.下载git客户端 2.在osc上创建项目 ①使用:git bash here ②在目录下执行:git init ③ssh-keygen -t rsa -C "xqs@gmail.com& ...
- 我的ORM之三 -- 更新
我的ORM索引 更新语法 var 影响行数 = dbr.表.Update(实体).Where(条件).Execute(); 实体类型: 更新的实体类型和添加的实体类型一样,有三类: 1. 任何C#类. ...
- Backbone源码解析(五):Route和History(路由)模块
今天是四月十二号,距离上次写博已经将近二十天了.一直忙于工作,回家被看书的时间占用了.连续两个礼拜被频繁的足球篮球以及各种体育运动弄的精疲力竭,所以很少抽时间来写技术博客.今天抽出时间把backbon ...
- mac安装redis
1.到官网下载最新的redis. http://redis.io/download 2.将下载下来的tar.gz 压缩包,(在/Users/***/Downloads)拷贝到usr/local目录下 ...
- .NET Framework中重点类型的继承关系
继承关系 Object ├─Array │ └─T[] ├─ArrayList ├─List<T> └─String 集合类型的接口 下图展示了集合类型的各种接口的相互关系.注意,下图中所 ...