Dragon Balls(hdu3635带权并查集)
题意:n个城市有n个龙珠,T a,b 代表将龙珠a移动到b城市,Q a代表查询龙珠a所在城市,该城市有多少颗龙珠,该龙珠移动了多少次。
注意:移动时是将龙珠所在该城市所有龙珠都移动,每个龙珠不会再回到自己以前呆过的城市。就是说假如龙珠1,2,3,都在城市2,那么龙珠就不能再移动了
在路径压缩的时候更新他的距离值
rootxx --> rootx 移动是3,rootx -->x的移动是5 那么 rootxx --> x的移动就是rootxx --> rootx+rootx -->x = 3 + 5 = 8
所以还是向量
在find操作的时候
int temp = p[x].father;
p[x].father = Find_set(p[x].father);
p[x].trap += p[temp].trap;
先保存他以前父亲的节点,然后更新时将他自己的节点和他原父亲的节点运输次数相加。
这样一层一层更新就可以了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct bian
{
int sum;
int father;
int trap;
}p[10005]; void Make_set(int n)
{
int i;
for(i = 1; i <= n; i++)
{
p[i].sum = 1;
p[i].father = i;
p[i].trap = 0;
}
} int Find_set(int x)
{
if(x != p[x].father)
{
int temp = p[x].father;
p[x].father = Find_set(p[x].father);
p[x].trap += p[temp].trap;
}
return p[x].father;
} void Union(int a,int b)
{
int x = Find_set(a);
int y = Find_set(b); if(x == y)
return ;
else
{
p[x].father = y;
p[x].trap++;
p[y].sum += p[x].sum;
}
}
int main()
{
int t,cas = 1;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
int i;
Make_set(n);printf("Case %d:\n",cas++);
for(i = 0; i < m; i++)
{
char c[5];
int a,b;
scanf("%s",c);
if(c[0] == 'T')
{
scanf("%d%d",&a,&b);
Union(a,b);
}
else
{
scanf("%d",&a);
int temp = Find_set(a);
printf("%d %d %d\n",temp,p[temp].sum,p[a].trap);
}
}
}
return 0;
}
Dragon Balls(hdu3635带权并查集)的更多相关文章
- HDU 3635 Dragon Balls(带权并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=3635 题意: 有n颗龙珠和n座城市,一开始第i颗龙珠就位于第i座城市,现在有2种操作,第一种操作是将x龙珠所在城 ...
- 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的输入, 表示 ...
随机推荐
- 继续推荐几款VisualStudio的插件
原文:继续推荐几款VisualStudio的插件 继前几天推荐了一款转换vs插件的插件后,借着安装VS2013之际,把我比较喜欢的几个插件继续推荐一下. C# Outline 2013 2013 C# ...
- 3D人脸识别预处理,3D face recognition preprocess
本文由兔崩溃公布http://blog.csdn.net/smartempire/article/details/31373817. 转载请注明出处.howdeshui#163.com 近期在做三维人 ...
- CentOs Linux 常见命令
整理一些常用的命令(持续更新): 查看端口是否开启: netstat -an | grep prot (查看是否打开23端口) |:通道的意思,grep是指查看当前字符所在的行 LINUX通过下面的命 ...
- How to:如何在调用外部文件时调试文件路径(常见于使用LaunchAppAndWait和LaunchApp函数)
原文:How to:如何在调用外部文件时调试文件路径(常见于使用LaunchAppAndWait和LaunchApp函数) IS里调用外部文件的时候,一般都是用LaunchAppAndWait函数,比 ...
- Installshield在安装结束时刷新系统
原文:Installshield在安装结束时刷新系统 在OnEnd里添加代码,两种解决方案 群友kevin的解决方案 #include "ifx.h" //Call to Win ...
- VS2015预览
VS2015预览版体验 .NET开源了,JAVA颤抖吧...据说VS2015可以开发android,ios,wp应用程序了,还可以开发能运行在mac,linux上的ASP.NET网站,如果真是这样 ...
- [译]JVM运行时数据区
(本篇文章翻译自JVM Run-Time Data Areas) 这是我阅读JVM规范的笔记,而且我画了一个图来帮助我理解. 1.每一个单独的线程(非共享的)的数据区 针对每一个单独的线程的数据区包括 ...
- 使用Windows2003创建DNS服务器 - 进阶者系列 - 学习者系列文章
Windows 2003的DNS服务器的配置还是比较简单的.下面简要介绍下DNS服务器的配置. 1. 打开控制面板,选择 添加删除Windows组件,选择 网络服务 ,选择 详细信息,选择 域名 ...
- Spring.Net+NHibenate+Asp.Net Mvc+Easyui框架
Spring.Net+NHibenate+Asp.Net Mvc+Easyui框架 初次接触Spring.Net+NHibenate+Asp.Net Mvc+Easyui框架,查阅了相关资料,了解了框 ...
- bat批量目光声明
写bat同一批次,盯着函数应使用.这个程序对可读性 在批处理,凝视节还有一种更常用的方法: goto start = 能够是多行文本,能够是命令 = 能够包括重定向符号和其它特殊字 ...