LA 3027 并查集
2015-08-24 09:06
246 查看
比较基础的带权并查集,需要注意终止条件是'O'而不是'0'...
#include <iostream> #include <cstring> #include <cstdio> #include <cmath> using namespace std; const int N = 20001; const int MOD = 1000; int f ; int d ; void init( int n ) { for ( int i = 1; i <= n; i++ ) { f[i] = i; d[i] = 0; } } void findf( int x ) { if ( f[x] == x ) return ; int tx = f[x]; findf(tx); d[x] += d[tx]; f[x] = f[tx]; } int main () { int t; scanf("%d", &t); while ( t-- ) { int n; scanf("%d", &n); init(n); char op[2]; while ( scanf("%s", op) != EOF ) { if ( op[0] == 'O' ) break; int x, y; if ( op[0] == 'E' ) { scanf("%d", &x); findf(x); printf("%d\n", d[x]); } else { scanf("%d%d", &x, &y); f[x] = y; d[x] = ( ( int ) abs( x - y ) ) % MOD; } } } return 0; }
相关文章推荐
- ECMAScript之操作符重点小解
- abstract的方法是否可同时是static 是否可同时是native 是否可同时
- UIScrollView
- php实现递归的三种基本方式
- MySQL的安装过程
- Android开发初识Volley的基本用法
- arcgis发布几何服务(geometryService)
- 第二十三天 TextView、Button
- 分享让人折服的优秀代码基因
- 九度oj1514数值的整数次方
- leetcode题解
- Hibernate的三种缓存机制
- Ubuntu之常用命令
- POJ 1250 Tanning Salon(简单模拟)
- 【Dijkstra+邻接表求次短路】HDOJ How Many Paths Are There 3191
- 基于注解的 Spring MVC 简单入门
- Storm Topology 提交 总结---Kettle On Storm 实现
- 笔记(实习第二周)
- Java中Overload和Override的区别
- POJ 1248 && HDU 1015 Safecracker(dfs)