[2016-03-19][UVALive][3027][Corporative Network]

  • 时间:2016-03-19 13:24:23 星期六

  • 题目编号:[2016-03-19][UVALive][3027][Corporative Network]

  • 题目大意:给定n个节点,I u v表示把u节点的父节点设置为v,距离为|u-v|%1000,E u表示询问u到根节点的距离,给定若干个I E操作,输出相应答案

  • 分析:带权并查集

  • 方法:d[maxn] 维护到父节点的距离,每次询问的时候,把当前节点压缩到根节点输出对应距离即可

  1. #ifdef _WORK_
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include<cstring>
  5. using namespace std;
  6. #define CLR(x,y) memset((x),(y),sizeof((x)))
  7. #define FOR(x,y,z) for(int (x)=(y);(x)<(z);++(x))
  8. const int maxn = 20000 +10;
  9. int fa[maxn],d[maxn];
  10. inline void ini(int n){
  11. CLR(d,0);
  12. FOR(i,0,n+1) fa[i] = i;
  13. }
  14. int fnd(int x){
  15. if(x == fa[x]) return x;
  16. int tmp = fa[x];
  17. fa[x] = fnd(fa[x]);
  18. d[x] = d[x] + d[tmp];
  19. return fa[x];
  20. }
  21. int main(){
  22. int t,n,p,q;
  23. char str[10];
  24. scanf("%d",&t);
  25. while(t--){
  26. scanf("%d",&n);
  27. ini(n);
  28. while(~scanf("%s",str) && str[0] != ‘O‘){
  29. if(str[0] == ‘I‘){
  30. scanf("%d%d",&p,&q);
  31. fa[p] = q;
  32. d[p] = abs(p - q)%1000;
  33. }else{
  34. scanf("%d",&p);
  35. fnd(p);
  36. printf("%d\n",d[p]);
  37. }
  38. }
  39. }
  40. return 0;
  41. }

来自为知笔记(Wiz)

时间: 2024-08-25 22:11:55

[2016-03-19][UVALive][3027][Corporative Network]的相关文章

【暑假】[实用数据结构]UVAlive 3027 Corporative Network

UVAlive 3027 Corporative Network 题目:   Corporative Network Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 3450   Accepted: 1259 Description A very big corporation is developing its corporative network. In the beginning each of the N ent

UVALive - 3027 - Corporative Network (并查集!!)

UVALive - 3027 Corporative Network Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A very big corporation is developing its corporative network. In the beginning each of the N enterprises of the cor

UVALive 3027 Corporative Network 带权并查集

                     Corporative Network A very big corporation is developing its corporative network. In the beginning each of the N enterprisesof the corporation, numerated from 1 to N, organized its own computing and telecommunication center.Soon,

UVALive 3027 Corporative Network(并查集之四)

问题描述: 求出点到根的距离,带权并查集. 代码思路: 在 I 命令的执行过程中,并不路径压缩. 当执行 E 查询命令时在路径压缩的同时递归求解存储在dist数组里面. 代码: #include<stdio.h> #include<math.h> #include<stdlib.h> #include<string.h> #define MOD 1000 #define MAX 20000 int pre[MAX+7],dist[MAX+7]; int fi

[LA] 3027 - Corporative Network [并查集]

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

3027 - Corporative Network

3027 - Corporative Network 思路:并查集: cost记录当前点到根节点的距离,每次合并时路径压缩将cost更新. 1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<string.h> 5 #include<math.h> 6 #include<stack> 7 #include<set> 8 #inc

(DS 《算法竞赛入门经典》)LA 3027 Corporative Network(查询某一个节点到根节点之间的距离)

题目大意: 查询某一个节点到根节点之间的距离 解题思路: 加权并查集问题.之前做的题目是"查看两个或多个节点是否在同一个集合下",现在的题目是"查询某个节点到 根节点之间的距离".之前只需要使用到father[x]这个数组,用来表示x的父亲节点是谁.现在引入dist[x]数组,用来记录 x节点到根节点的距离 1)在并查集中,根节点不懂,其他节点都可以动. A very big corporation is developing its corporative net

LA 3027 Corporative Network(并查集,求某个节点到根节点的距离)

A very big corporation is developing its corporative network. In the beginning each of the N enterprisesof the corporation, numerated from 1 to N, organized its own computing and telecommunication center.Soon, for amelioration of the services, the co

【LA 3027 Corporative Network】

·一些很可爱的询问和修改,放松地去用并查集解决. ·英文题,述大意: 输入n(5<=n<=20000)表示树有n个节点,并且会EOF结束地读入不超过 20000个操作,一共有两种:    ①I v u:表示将v的父亲节点设置为u(在这之前v没有爸爸),边权设置为abs(v-u)%1000.    ②E u:表示询问u到当前它所在树的根节点的距离. ·分析:      为了记录当前一系列加边操作后所有的点的位置情况(因为你随时可能回答询问啊),根据这道题的点关系特点[只在乎点和其根节点的信息],