http://acm.hdu.edu.cn/showproblem.php?pid=3635

题目意思是说n个球在n个城市。

每次操作把编号i的球所在的城市的所有的求全部一道另一城市B

每次询问访问编号i的球的城市,以及这个城市的球的数量,以及这个球被移动了多少次。

方法就是用一个结构体记录每个球的父节点和移动次数以及这个节点的球的个数(球和城市可以堪为一个整体),然后每次操作就行。

 #include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX(a,b) (a > b ? a : b)
#define MIN(a,b) (a < b ? a : b)
#define mem0(a) memset(a,0,sizeof(a))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R typedef long long LL;
const double eps = 1e-;
const int MAXN = ;
const int MAXM = ; struct NODE
{
int num;
int fa;
int time;
}p[MAXN];
int N, Q; void init()//初始化
{
for(int i=;i<=N;i++) {
p[i].fa = i;
p[i].num = ;
p[i].time = ;
}
} int find(int x)//找父节点
{
if(x == p[x].fa) return x;
int fa = p[x].fa;//记录这个节点的父节点
p[x].fa= find(p[x].fa);
p[x].time += p[fa].time;//这个节点移动的次数加上他父节点次数
return p[x].fa;
} void Union(int x, int y)//合并
{
int a = find(x);
int b = find(y);
if(a != b)
{
p[a].fa = p[b].fa;//连边
p[b].num += p[a].num;//龙珠的数量
p[a]. time ++;//a又多移动了一次
}
} int main()
{
//freopen("in.txt", "r", stdin);
int T=, Cas;
scanf("%d", &Cas);
while(Cas--)
{
scanf("%d%d%*c", &N, &Q);
init();
char ch; int a, b;
printf("Case %d:\n", ++T);
for(int i=;i<Q;i++)
{
scanf("%c", &ch);
if(ch == 'T') {
scanf("%d %d%*c", &a, &b);
Union(a, b);
}
else {
scanf("%d%*c", &a);
b = find(a);//必须先找一遍他被移动了多少次
printf("%d %d %d\n", b, p[b].num, p[a].time);
}
}
}
return ;
}

但是我把树的层数减少那步去掉后字节搜索层数居然和这个没什么区别,只能说数据太弱了= =

 #include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX(a,b) (a > b ? a : b)
#define MIN(a,b) (a < b ? a : b)
#define mem0(a) memset(a,0,sizeof(a))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R typedef long long LL;
const double eps = 1e-;
const int MAXN = ;
const int MAXM = ; struct NODE
{
int num;
int fa;
}p[MAXN];
int N, Q; void init()
{
for(int i=;i<=N;i++) {
p[i].fa = i;
p[i].num = ;
}
} int find(int x)
{
if(x == p[x].fa) return x;
return find(p[x].fa);
} void Union(int x, int y)
{
int a = find(x);
int b = find(y);
if(a != b)
{
p[a].fa = b;
p[b].num += p[a].num;
}
} int main()
{
int T=, Cas;
scanf("%d", &Cas);
while(Cas--)
{
scanf("%d%d%*c", &N, &Q);
init();
char ch; int a, b;
printf("Case %d:\n", ++T);
for(int i=;i<Q;i++)
{
scanf("%c", &ch);
if(ch == 'T') {
scanf("%d %d%*c", &a, &b);
Union(a, b);
}
else {
int time = ;
scanf("%d%*c", &a);
while(a != p[a].fa){ time ++; a = p[a].fa; }
printf("%d %d %d\n", a, p[a].num, time);
}
}
}
return ;
}

HDU3635Dragon Balls(并查集)的更多相关文章

  1. hdu 3635 Dragon Balls(并查集)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  2. hdu 3635 Dragon Balls(并查集应用)

    Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...

  3. Codeforces Round #245 (Div. 2) B. Balls Game 并查集

    B. Balls Game Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem ...

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

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

  5. [HDOJ3635]Dragon Balls(并查集,路径压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3635 题意:有n个龙珠,n个城市.初始状态第i个龙珠在第i个城市里.接下来有两个操作: T A B:把 ...

  6. hdu 3635 Dragon Balls(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)

    这道题说,在很久很久以前,有一个故事.故事的名字叫龙珠.后来,龙珠不知道出了什么问题,从7个变成了n个. 在悟空所在的国家里有n个城市,每个城市有1个龙珠,第i个城市有第i个龙珠. 然后,每经过一段时 ...

  7. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

  8. ZOJ3761(并查集+树的遍历)

    Easy billiards Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Edward think a g ...

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

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

  10. WUSTOJ 1319: 球(Java)并查集

    题目链接:1319: 球 参考:wustoj 1319 球-wust_tanyao,并查集 并查集系列:WUSTOJ 1346: DARK SOULS(Java)并查集 Description Icy ...

随机推荐

  1. Jquery源码中的Javascript基础知识(二)

    接上一篇,jquery源码的这种写法叫做匿名函数自执行 (function( window, undefined ) { // code })( window ); 函数定义了两个参数window和u ...

  2. 旧书重温:0day2【3】 详细解读PEB法 查找kener32地址

    题外话:上一篇文章中的 PEB法查找kerner32地址的方法 对TEB.PEB .PE结构 知识要求很高,确实在写汇编代码时候小编 感觉自己能力,信手啪啪一顿乱撸,结果一运行,非法访问了,没办法翻阅 ...

  3. LSTM网络(Long Short-Term Memory )

    本文基于前两篇 1. 多层感知机及其BP算法(Multi-Layer Perceptron) 与 2. 递归神经网络(Recurrent Neural Networks,RNN) RNN 有一个致命的 ...

  4. LCS nlog(n) 但最坏情况还是比较悲剧 转载的文章;

    最长公共子序列问题: 给定2个字符串,求其最长公共子串.如abcde和dbada的最长公共字串为bd. 动态规划:dp[i][j]表示A串前i个和B串前j个的最长公共子串的长度. 则 若A[i] == ...

  5. yii2 html下拉框

    下拉框 带默认值 <?php $form=ActiveForm::begin(); echo $form->field($model,'uname', ['inputOptions'=&g ...

  6. DDoS攻防战(三):ip黑白名单防火墙frdev的原理与实现

    在上一篇文章<DDoS攻防战 (二) :CC攻击工具实现与防御理论>中,笔者阐述了一个防御状态机,它可用来抵御来自应用层的DDoS攻击,但是该状态机依赖一个能应对大量条目快速增删的ip黑白 ...

  7. Unity 内置着色器(转)

    Unity包括超过40种内置的shader. 标准着色器家族 Normal Shader Family 这些着色器都是Unity基本的着色器.适用于大多数的不透明物体,如果想要物体有透明.发光效果等, ...

  8. python 映射列表 学习

    列表映射是个非常有用的方法,通过对列表的每个元素应用一个函数来转换数据,可以使用一种策略或者方法来遍历计算每个元素. 例如: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  9. Python开发者最常犯的10个错误

    Python是一门简单易学的编程语言,语法简洁而清晰,并且拥有丰富和强大的类库.与其它大多数程序设计语言使用大括号不一样 ,它使用缩进来定义语句块. 在平时的工作中,Python开发者很容易犯一些小错 ...

  10. 机器学习&数据挖掘笔记(常见面试之机器学习算法思想简单梳理)

    机器学习&数据挖掘笔记_16(常见面试之机器学习算法思想简单梳理) 作者:tornadomeet 出处:http://www.cnblogs.com/tornadomeet 前言: 找工作时( ...