BNU Corporative Network(并查集求点到根节点的距离)@
来源:互联网 发布:有人在淘宝买过鹿肉吗 编辑:程序博客网 时间:2024/06/05 05:26
Corporative Network
3000ms
30000KB
This problem will be judged on PKU. Original ID: 1962
64-bit integer IO format: %lld Java class name: Main
64-bit integer IO format: %lld Java class name: Main
Font Size:
A very big corporation is developing its corporative network. In the beginning each of the N enterprises of the corporation, numerated from 1 to N, organized its own computing and telecommunication center. Soon, for amelioration of the services, the corporation started to collect some enterprises in clusters, each of them served by a single computing and telecommunication center as follow. The corporation chose one of the existing centers I (serving the cluster A) and one of the enterprises J in some other cluster B (not necessarily the center) and link them with telecommunication line. The length of the line between the enterprises I and J is |I – J|(mod 1000).In such a way the two old clusters are joined in a new cluster, served by the center of the old cluster B. Unfortunately after each join the sum of the lengths of the lines linking an enterprise to its serving center could be changed and the end users would like to know what is the new length. Write a program to keep trace of the changes in the organization of the network that is able in each moment to answer the questions of the users.
Input
Your program has to be ready to solve more than one test case. The first line of the input will contains only the number T of the test cases. Each test will start with the number N of enterprises (5<=N<=20000). Then some number of lines (no more than 200000) will follow with one of the commands:
E I – asking the length of the path from the enterprise I to its serving center in the moment;
I I J – informing that the serving center I is linked to the enterprise J.
The test case finishes with a line containing the word O. The I commands are less than N.
E I – asking the length of the path from the enterprise I to its serving center in the moment;
I I J – informing that the serving center I is linked to the enterprise J.
The test case finishes with a line containing the word O. The I commands are less than N.
Output
The output should contain as many lines as the number of E commands in all test cases with a single number each – the asked sum of length of lines connecting the corresponding enterprise with its serving center.
Sample Input
14E 3I 3 1E 3I 1 2E 3I 2 4E 3O
Sample Output
0235
Source
Southeastern Europe 2004
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <vector>#include <map>#include <queue>#include <algorithm>using namespace std;const int N = 1e5+10;int f[N], d[N];char str[N];int getf(int x){ if(x!=f[x]) { int root=getf(f[x]); d[x]+=d[f[x]]; f[x]=root; return f[x]; } return x;}int main(){ int n, t; scanf("%d", &t); while(t--) { scanf("%d", &n); for(int i=0; i<=n; i++) f[i]=i; memset(d,0,sizeof(d)); while(scanf("%s", &str)&&str[0]!='O') { int x, y; if(str[0]=='I') { scanf("%d %d", &x, &y); f[x]=y, d[x]=abs(x-y)%1000; } else { scanf("%d", &x); getf(x); printf("%d\n",d[x]); } } } return 0;}
0 0
- BNU Corporative Network(并查集求点到根节点的距离)@
- LA 3027 Corporative Network 并查集记录点到根的距离
- (DS 《算法竞赛入门经典》)LA 3027 Corporative Network(查询某一个节点到根节点之间的距离)
- 1329 - Corporative Network(并查集)
- UVAalive 3027 Corporative Network(并查集的路径压缩维护)
- 指南 第三章 例题6 UVALive 3027 Corporative Network(并查集的应用)
- poj 1962 Corporative Network 并查集的应用
- UVALive 3027---Corporative Network+并查集的应用
- LA-3027-Corporative Network-更新深度的并查集
- UVa:1329 Corporative Network(带权并查集)
- Corporative Network(带权并查集)
- UVALive - 3027Corporative Network(带权并查集)
- poj 1962 Corporative Network(并查集)
- UVALive - 3027 - Corporative Network (并查集!!)
- poj 1962 Corporative Network(带权并查集)
- POJ - 1962 Corporative Network(带权并查集)
- UVALive 3027 Corporative Network (带权并查集)
- poj1962 Corporative Network(带权并查集)
- 1112
- 使用hexo并部署到aws上
- javascript基础知识(一)
- android 布局相关
- 提供给开发者 10 款最好的 Python IDE
- BNU Corporative Network(并查集求点到根节点的距离)@
- android 图片相关处理
- hexo部署到阿里云
- Elasticsearch集群搭建
- VS2015配置opencv3.2.0
- Ubuntu编译安装linux内核
- SpringMVC4.1.5 源代码分析-01写在前面
- OLAP引擎Apache Kylin的分析和学习
- 【算法和数据结构】1.3–分治思想之二分查找(C++实现)