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的输入, 表示 ...
随机推荐
- [Leetcode][JAVA] Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 ...
- Android中Button、ImageButton、ImageView背景设置区别
Button与ImageButton实际两者无关,Button继承自TextView,不支持src;ImageButton继承自ImageView.同一张图片在不设置大小,默认显示时,使用Button ...
- [XAF] Keep the DetailView open in a popup window
public class ViewController1 : ViewController { ListViewProcessCurrentObjectController controller; p ...
- python发邮件实现Redis通知功能
# -*- coding:utf-8 -*- import smtplib #import os from email.mime.text import MIMEText from email.mim ...
- C#中的IEnumable与IEnumator接口的简单理解
IEnumerable接口中的方法是返回IEnumator的对象,集合继承了IEnumerator接口才能实现Foreach方法实现遍历.集合类都继承IEnumable和IEnumerator接口,或 ...
- [Laravel-Swagger]如何在 Laravel 项目中使用 Swagger
如何在 Laravel 项目中使用 Swagger http://swagger.io/getting-started/ 安装依赖 swagger-php composer require zirco ...
- 用javascript写Android和iOS naitve应用,实在炫酷。
关注NativeScript有一段时间了,说好了的三月发第一个Beta版,终于发布了. // declare the extended NativeScriptActivity functionali ...
- ASP.NET 5系列教程 (五):在Visual Studio 2015中使用Grunt、Bower开发Web程序
基于Visual Studio 2015,你可以: 方便的管理前端包,如jQuery, Bootstrap, 或Angular. 自动运行任务,如LESS.JavaScript压缩.JSLint.Ja ...
- MQTT V3.1--我的理解
最近因为工作需要,需要对推送消息了解,因此对MQTT进行了整理,这里更多的是对MQTT英文版的翻译和理解. MQTT(Message Queue Telemetry Transport),遥测传输协议 ...
- Web Essentials之JavaScript,TypeScript和CoffeeScript
返回Web Essentials功能目录 一些Javascript功能也可以用于TypeScript. 本篇目录 功能 智能提示 TypeScript CoffeeScript 功能 JSHint J ...