HDU3635(并查集)
来源:互联网 发布:关于经济最优化的问题 编辑:程序博客网 时间:2024/06/04 18:41
题目大意:n个龙珠,q个操作。操作T是将第Ath所在的城市里的所有龙珠移向第Bth龙珠所在的城市;操作Q是询问第Ath龙珠所在的城市、所在城市的龙珠总数,以及第Ath龙珠被移动的次数。
#include <cstdio>#include <algorithm>#include <iostream>#include <cstring>#include <string>#include <cmath>using namespace std;const int maxn = 11111;struct node{ int fa, step, sum;}p[maxn];int n, m;void init(){ for(int i=0; i<=n; i++) { p[i].fa = i; p[i].sum = 1; p[i].step = 0; }}int find_fa(int x){ if(x == p[x].fa) return x; int tot = p[x].fa; p[x].fa = find_fa(p[x].fa); p[x].step += p[tot].step; return p[x].fa;}void un(int x, int y){ int fx = find_fa(x); int fy = find_fa(y); if(fx != fy) { p[fx].fa = fy; p[fx].step++; p[fy].sum += p[fx].sum; p[fx].sum = 0; }}int main(){ int T, kase = 0; scanf("%d", &T); while(T--) { scanf("%d%d", &n, &m); init(); char op[5]; int from, to, vex; printf("Case %d:\n", ++kase); for(int i=0; i<m; i++) { scanf("%s", op); if(op[0] == 'T') { scanf("%d%d", &from, &to); un(from, to); } else { scanf("%d", &vex); int ans = find_fa(vex); printf("%d %d %d\n", ans, p[ans].sum, p[vex].step); } } } return 0;}
0 0
- hdu3635(并查集)
- hdu3635并查集
- HDU3635(并查集)
- hdu3635并查集
- hdu3635之并查集
- hdu3635(并查集)
- HDU3635 Dragon Balls 并查集
- HDU3635 带权并查集
- HDU3635 Dragon Balls(并查集)
- hdu3635 Dragon Balls--并查集
- HDU3635 Dragon Balls(并查集)
- HDU3635:Dragon Balls(并查集)
- Dragon Balls(hdu3635带权并查集)
- 【hdu3635】Dragon Balls —— 并查集
- HDU3635 Dragon Balls 解题报告【带权并查集】
- 带权并查集--hdu3635 Dragon Balls
- 09-07 HDU_Steps5.1 并查集 HDU1829 HDU1325 HDU1598 HDU3461 HDU3635 HDU2473 HDU3172 HDU3038
- hdu3635
- 大题---打印回型嵌套
- hibernate: Duplicate class/entity; Could not parse mapping document from resource
- BUCK 使用体验
- c++ vector
- java反射实例
- HDU3635(并查集)
- TBB之concurrent_vector
- HTML状态码
- Tun/Tap interface tutorial
- Fragment源码分析(一) 构造
- 机器学习技法 笔记一 linear support vector machine
- SVG图形绘制与滤镜
- 在Eclipse中将Java项目打包为jar
- 【JavaScript 未完】情人节&音乐项目总结