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. tcpdump命令介绍

    命令格式为:tcpdump [-nn] [-i 接口] [-w 储存档名] [-c 次数] [-Ae] [-qX] [-r 文件] [所欲捕获的数据内容] 参数: -nn,直接以 IP 及 Port ...

  2. Codeforces Round #436 (Div. 2) 题解864A 864B 864C 864D 864E 864F

    A. Fair Game time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  3. 【每日一linux命令7】用户及用户组

    一.查询用户及用户组相关命令 1.whoami 查询当前登录的用户名 2.groups 查询当前登录用户名所在的用户组 3.groups root 查询root用户名所在的用户组 二.怎么批量查看用户 ...

  4. notepad++ 退出后关闭所有文档(关闭“记住最后打开的文件”)

    旧版本: 设置->首选项->其他->取消勾选Remember current session for next launch 新版本: 设置->首选项->备份->取 ...

  5. js实现翻转一个字符串

    字符串作在程序中是非常常见的,因为程序中绝大部分的数据都可以当作字符串来处理.在这里介绍几种翻转字符串的方法. (1)使用字符串函数 //使用数组翻转函数 function reverseString ...

  6. 【JZOJ3598】【CQOI2014】数三角形

    Mission 对于100%的数据1<=m,n<=1000 Solution 鬼题,ans=C3(n∗m)−Ans,其中Ans表示三点共线的数目: 枚举最长边的向量(x,y),容易算出贡献 ...

  7. delete records in table A not in table B

    转)A.B两表,找出ID字段中,存在A表,但是不存在B表的数据.A表总共13w数据,去重后大约3W条数据,B表有2W条数据,且B表的ID字段有索引. 方法一 使用 not in ,容易理解,效率低  ...

  8. 报错OPTION SQL_SELECT_LIMIT=

    org.quartz.JobPersistenceException: Couldn't acquire next trigger: You have an error in your SQL syn ...

  9. Nginx教程(五) Nginx配置文件详解 (转)

    一. Nginx配置文件nginx.conf中文详解 #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_processe ...

  10. 如何写JavaScript中的callback回调函数

    如何写回调函数? 如果自己在写一个方法或函数,你有可能会遇到需要一个回调函数.下面就是一个简单的常见回调函数例子: function mySandwich(param1, param2, callba ...