UVALive - 3027 Corporative Network 并查集
2015-07-18 11:35
369 查看
传送门:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33982
#include <cstdio> #include <cstring> #include <stack> #include <queue> #include <iostream> #include <algorithm> #include <map> #include <vector> using namespace std; const int MAXN = 200005; int bin[MAXN]; int path[MAXN]; int findx(int x) { if(x != bin[x]) { int root = findx(bin[x]); path[x] += path[bin[x]]; bin[x] = root; } return bin[x]; } void Deal_with() { int T,n; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i = 0 ; i <= n ; i++) { bin[i] = i; path[i] = 0; } char tempc[10]; int u,v; while(scanf("%s",tempc) && tempc[0] != 'O') { if(tempc[0] == 'I') { scanf("%d %d",&u,&v); bin[u] = v; path[u] += abs(u - v) % 1000; } else { scanf("%d",&u); findx(u); printf("%d\n",path[u]); } } } } int main(void) { //freopen("a.in","r",stdin); Deal_with(); return 0; }
相关文章推荐
- 多重继承与名字二义性
- J2EE概述
- 失控--阅读笔记组装复杂性
- vim文本编辑器主要内容
- 算法导论 第十八章;B 树
- 使用Python操作Redis
- POJ-2159 Ancient Cipher
- MySQL 添加列、修改列、删除列的方法
- struts2 JSON 插件的使用
- RecyclerView
- bzoj 1189
- Coursera台大机器学习技法课程笔记02-Dual Support Vector Machine
- 让你的本本转身变成路由器?
- hibernate缓存机制详细分析
- Matlab中cell2mat的使用
- 程序设计项目 ——银行储蓄系统
- poj 3017
- opencv中waitkey(0)不起作用
- MySQL 错误
- odoo联调