HDU 3635 Dragon Balls(带权并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=3635
题意:
有n颗龙珠和n座城市,一开始第i颗龙珠就位于第i座城市,现在有2种操作,第一种操作是将x龙珠所在城市的所有龙珠移至y龙珠所在城市,第二种操作是计算x龙珠所在城市y,y城市龙珠个数,以及x龙珠移动的次数。
思路:
num[x]用来维护x城市所包含的龙珠个数,cnt[x]维护x龙珠所移动的次数。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = +; int n, m;
int p[maxn], num[maxn], cnt[maxn]; int finds(int x)
{
if(p[x]==x) return x;
int tmp=p[x];
p[x]=finds(p[x]);
cnt[x]+=cnt[tmp];
return p[x];
} int main()
{
//freopen("in.txt","r",stdin);
int T;
int kase = ;
scanf("%d",&T);
while(T--)
{
printf("Case %d:\n",++kase);
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) {p[i]=i;num[i]=;cnt[i]=;}
while(m--)
{
char op[];
scanf("%s",op);
if(op[]=='T')
{
int x,y;
scanf("%d%d",&x,&y);
int xx = finds(x);
int yy = finds(y);
if(xx!=yy)
{
p[xx]=yy;
cnt[xx]++;
num[yy]+=num[xx];
num[xx]=;
}
}
else
{
int x;
scanf("%d",&x);
int ans = finds(x);
printf("%d %d %d\n",ans,num[ans],cnt[x]);
}
}
}
return ;
}
HDU 3635 Dragon Balls(带权并查集)的更多相关文章
- hdu 3635 Dragon Balls (带权并查集)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 3635 Dragon Balls(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)
这道题说,在很久很久以前,有一个故事.故事的名字叫龙珠.后来,龙珠不知道出了什么问题,从7个变成了n个. 在悟空所在的国家里有n个城市,每个城市有1个龙珠,第i个城市有第i个龙珠. 然后,每经过一段时 ...
- hdu3635 Dragon Balls(带权并查集)
/* 题意:有N个城市, 每一个城市都有一个龙珠(编号与城市的编号相同),有两个操作 T A ,B 将标号为A龙珠所在城市的所有的龙珠移动到B龙珠所在城市中! 思路:并查集 (压缩路径的时候将龙珠移动 ...
- hdu 5441 Travel 离线带权并查集
Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...
- Hdu 2047 Zjnu Stadium(带权并查集)
Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- How Many Answers Are Wrong (HDU - 3038)(带权并查集)
题目链接 并查集是用来对集合合并查询的一种数据结构,或者判断是不是一个集合,本题是给你一系列区间和,判断给出的区间中有几个是不合法的. 思考: 1.如何建立区间之间的联系 2.如何发现悖论 首先是如何 ...
- hdu 5441 travel 离线+带权并查集
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...
- hdu 2818 Building Block (带权并查集,很优美的题目)
Problem Description John are playing with blocks. There are N blocks ( <= N <= ) numbered ...N ...
- How Many Answers Are Wrong HDU - 3038 (经典带权并查集)
题目大意:有一个区间,长度为n,然后跟着m个子区间,每个字区间的格式为x,y,z表示[x,y]的和为z.如果当前区间和与前面的区间和发生冲突,当前区间和会被判错,问:有多少个区间和会被判错. 题解:x ...
- hdu 3047–Zjnu Stadium(带权并查集)
题目大意: 有n个人坐在zjnu体育馆里面,然后给出m个他们之间的距离, A B X, 代表B的座位比A多X. 然后求出这m个关系之间有多少个错误,所谓错误就是当前这个关系与之前的有冲突. 分析: 首 ...
随机推荐
- 20165215 2017-2018-2 《Java程序设计》第2周学习总结
20165215 2017-2018-2 <Java程序设计>第2周学习总结 教材学习内容总结 chapter2 逻辑类型boolea只能赋值true或false Java没有无符号整数类 ...
- SQL 语法速记
----------------------------------DML(数据操作语言)---------------------------------- -- 一.INSERT VALUES语句 ...
- DNS缓存中毒的知识
网络上出现互联网漏洞——DNS缓存漏洞,此漏洞直指我们应用中互联网脆弱的安全系统,而安全性差的根源在于设计缺陷.利用该漏洞轻则可以让用户无法打开网页,重则是网络钓鱼和金融诈骗,给受害者造成巨大损失. ...
- Java操作文件Util
package io.guangsoft.utils; import java.io.File; import java.io.FileInputStream; import java.io.File ...
- glibc 2.x release note
glibc 2.x release note,参见: https://sourceware.org/glibc/wiki/Glibc%20Timeline https://www.gnu.org/so ...
- java中线程的停止以及LockSupport工具类
看jstack输出的时候,可以发现很多状态都是TIMED_WAITING(parking),如下所示: "http-bio-8080-exec-16" #70 daemon pri ...
- 维护keepalived与mysql漂移脚本
环境拓扑 chengAlived #!/bin/bash function checkModelone(){ echo "重新进行获取" wget 192.168.158.147: ...
- 线性回归、Logistic回归、Softmax回归
线性回归(Linear Regression) 什么是回归? 给定一些数据,{(x1,y1),(x2,y2)…(xn,yn) },x的值来预测y的值,通常地,y的值是连续的就是回归问题,y的值是离散的 ...
- Codeforces Round #425 (Div. 2) Problem C Strange Radiation (Codeforces 832C) - 二分答案 - 数论
n people are standing on a coordinate axis in points with positive integer coordinates strictly less ...
- shell 变量相关的命令
变量="变量" readonly 变量名="变量" 表示设置该变量为只读变量 ,这个变量不能别改变 echo $变量名 set 显示本地所有的变量 unse ...