uvalive 3027
来源:互联网 发布:ps3 linux 编辑:程序博客网 时间:2024/05/16 06:30
Description
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.
Input
E I � asking the length of the path from the enterprise I to its serving center in the moment;The test case finishes with a line containing the word O. The I commands are less than N.
I I J � informing that the serving center I is linked to the enterprise J.
Output
Sample Input
14E 3I 3 1E 3I 1 2E 3I 2 4E 3O
Sample Output
0235
题意:这英语给我看晕了,不说了
思路:传统的并查集路径压缩,类似于那道经典的食物链问题,注意压缩的方法即可
#include <iostream>#include <stdio.h>#include <cstring>using namespace std;int f[50000],len[50000];int n;int find(int x){if(x==f[x])return x;int tmp=f[x];f[x]=find(f[x]);len[x]=(len[x]+len[tmp]);return f[x];}int ab(int x,int y){return x>y?x-y:y-x;}int merge(int x,int y){int xx=find(x);int yy=find(y); if(xx==yy) return 1; else { f[xx]=yy; len[xx]=(len[y]-len[x]+ab(x,y)%1000); find(x); return 0; }}int main(){//freopen("in.txt","r",stdin);int t;while(scanf("%d",&t)!=EOF){ while(t--) {scanf("%d",&n);for(int i=0;i<=20008;i++){f[i]=i;len[i]=0;}char g[2];int x,y; while(scanf("%s",g)!=EOF) { if(g[0]=='O') break;if(g[0]=='E'){scanf("%d",&x);find(x);printf("%d\n",len[x]);}else{scanf("%d%d",&x,&y);merge(x,y);}}}}}
- uvalive 3027
- UVALive 3027 Corporative Network
- UVALive 3027 Corporative Network
- UVALive 3027 Corporative Network
- UVALive 3027 Corporative Network
- UVALive 3027 Corporative Network
- UVALive - 3027 Corporative Network
- UVALive 3027 Corporative Network
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- HDU 4260(The End of The World-Hanoi塔从中间状态移动)
- OC便利方法
- 用户接受度问题
- R语言实战1
- BLE4.0蓝牙center与peripheral建立连接绑定过程
- uvalive 3027
- makefile 函数学习
- 新手笔记之print(?)
- HDU 5389 Zero Escape(dp啊 多校)
- 第85讲:Scala中For表达式的强大表现力实战学习笔记
- [leetcode] Delete Node in a Linked List 删除链表中的一个结点
- KMP hihoCoder1015 KMP算法
- HDU1253-胜利大逃亡
- GDOI2016模拟8.13挑选玩具