Problem - 3635

  切切水题,并查集。

  记录当前根树的结点个数,记录每个结点相对根结点的转移次数。1y~

代码如下:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring> using namespace std;
const int N = ;
struct MFS {
int fa[N], tm[N], cnt[N];
void init() { for (int i = ; i < N; i++) fa[i] = i, tm[i] = , cnt[i] = ;}
int find(int x, int &t) {
if (fa[x] == x) {
t = ;
} else {
fa[x] = find(fa[x], t);
tm[x] += t;
t = tm[x];
}
return fa[x];
}
void merge(int x, int y) {
int tmp;
int fx = find(x, tmp);
int fy = find(y, tmp);
tm[fy] -= tm[fx] - ;
cnt[fx] += cnt[fy];
fa[fy] = fa[fx];
// for (int i = 0; i < 5; i++) cout << fa[i] << ' '; cout << endl;
}
void query(int x) {
int tmp;
int fx = find(x, tmp);
printf("%d %d %d\n", fx, cnt[fx], tm[x] + tm[fx]);
}
} mfs; int main() {
int T, n, m;
scanf("%d", &T);
for (int cas = ; cas <= T; cas++) {
scanf("%d%d", &n, &m);
mfs.init();
char op[];
int x, y;
printf("Case %d:\n", cas);
for (int i = ; i < m; i++) {
scanf("%s", op);
if (op[] == 'T') {
scanf("%d%d", &x, &y);
mfs.merge(y, x);
} else {
scanf("%d", &x);
mfs.query(x);
}
}
}
return ;
}

——written by Lyon

hdu 3635 Dragon Balls (MFSet)的更多相关文章

  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. HDU 3635 Dragon Balls(超级经典的带权并查集!!!新手入门)

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

  4. hdu 3635 Dragon Balls(并查集)

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

  5. hdu 3635 Dragon Balls

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

  6. HDU 3635 Dragon Balls(带权并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=3635 题意: 有n颗龙珠和n座城市,一开始第i颗龙珠就位于第i座城市,现在有2种操作,第一种操作是将x龙珠所在城 ...

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

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

  8. hdu 3635 Dragon Balls(并查集)

    题意: N个城市,每个城市有一个龙珠. 两个操作: 1.T A B:A城市的所有龙珠转移到B城市. 2.Q A:输出第A颗龙珠所在的城市,这个城市里所有的龙珠个数,第A颗龙珠总共到目前为止被转移了多少 ...

  9. hdoj 3635 Dragon Balls【并查集求节点转移次数+节点数+某点根节点】

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

随机推荐

  1. 集训队日常训练20180525-DIV2

    A.2295 求有多少素数对和等于n. 暴力. #include <bits/stdc++.h> using namespace std; int ss(int n) { ,a=sqrt( ...

  2. Leetcode665.Non-decreasing Array非递减数组

    给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i < n ...

  3. PHP 学习1.4

    1.session and cookie 示列: <?phpsession_start();?><!DOCTYPE html PUBLIC "-//W3C//DTD XHT ...

  4. Kibana将语言设置为中文

    6.7以后系统开始支持中文了,修改语言只需要添加一行配置即可. 设置方法 在kibana.yml配置文件中添加一行配置 i18n.locale: "zh-CN" 修改后重启,可以看 ...

  5. js 详解setTimeout定时器

    setTimeout: 定时器函数 第一个参数是匿名函数,第二个参数是延迟执行时间 setTimeout(function(){},time) 注意: 1.setTimeout函数是Window对象提 ...

  6. bnd.bnd属性文件格式

    1.Header以大写字母开头 Bundle-Name: StoreAdminProductsTool 2.Instruction以-和小写字母开头 -sources: true 3. Macro形式 ...

  7. 《2019年上半年Web应用安全报告》发布:90%以上攻击流量来源于扫描器,IP身份不再可信

    Web应用安全依然是互联网安全的最大威胁来源之一,除了传统的网页和APP,API和各种小程序也作为新的流量入口快速崛起,更多的流量入口和更易用的调用方式在提高web应用开发效率的同时也带来了更多和更复 ...

  8. 如何使我们的薪资15k?

    以下我们来探讨一下如何才能使我们具备高薪15K的能力? 要想使自己的薪资成为15K,我首先想到的是我们的能力?编码能力?语言表达能力? 我觉得为之重要的则是我们的综合能力: 方方面面的能力.综合素质: ...

  9. Directx11教程(60) tessellation学习(2)

    原文:Directx11教程(60) tessellation学习(2)        本教程中,我们开始tessellation编程,共实现了2个程序,第一个tessellation程序,是对一个三 ...

  10. Python学习之路13☞常用模块

    一 time模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行“type(t ...