UVAalive 3027 Corporative Network(并查集的路径压缩维护)
2015-07-22 16:03
477 查看
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 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.
Your program has to be ready to solve more than one test case. The first line of the input file 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.
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.
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 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 file 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.
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
1 4 E 3 I 3 1 E 3 I 1 2 E 3 I 2 4 E 3 O
Sample Output
0 2 3 5 题意:给你2种操作,一种是I u v,意思是使u得父节点为v,并且2点间的距离为|u-v|%1000并且保证之前u没有父节点,另外一种是E u,查询u到根节点的距离。 思路:逐层更新是不是可行呢,即每次更新的时候只要一点更新了,那么就再更新它的子节点!思路不错,但是会超时!已经以身试险了==! 后来发现,实际上每次我们只需要记录下每个节点到父节点的距离为d[i],然后在路径压缩时维护这个数组就可以了!所以标准的并查集的应用题! AC代码: #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<vector> using namespace std; #define maxn 20010 int d[maxn]; int f[maxn]; int find(int x) { if(x!=f[x]) { int t=find(f[x]); d[x]+=d[f[x]]; return f[x]=t; } else return x; } int main() { int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); getchar(); for(int i=1;i<=n;i++) { d[i]=0; f[i]=i; } char s[3]; while(scanf("%s",s)!=EOF) { if(s[0]=='O')break; else if(s[0]=='E') { int s; scanf("%d",&s); find(s); printf("%d\n",d[s]); } else if(s[0]=='I') { int u,v; scanf("%d %d",&u,&v); f[u]=v; d[u]=abs(u-v)%1000; } } } return 0; }
相关文章推荐
- opencv配置指南
- OpenStack local.conf
- 微信、陌陌等著名IM软件设计架构详解
- TrustZone——开源库—Linaro—OP-TEE
- VMware虚拟机的CentOS无法上网的解决方法
- linux cooked capture
- HBase 系统架构
- 常用Hbase Shell
- 分布式网站架构后续:zookeeper技术浅析
- ZooKeeper监控
- nginx配置多个虚拟主机,只有其中某个有作用
- Android工程架构的总结
- 研究Openshare的原理
- linux日志服务器搭建
- linux日志服务器搭建
- 执行shell命令
- Linux系统防止黑客NMAP扫描的方法
- Linux read()函数
- Apache与Tomcat
- opencv内存不足问题