LA 3027 合作网络

https://vjudge.net/problem/UVALive-3027

题意:

有n个结点,初始时每个结点的父节点都不存在。你的任务是执行一次I操作和E操作,格式如下:

I u v:把结点u的父节点设为v,距离为|u-v|除以1000的余数。输入保证执行指令前u没有父节点。

E u:询问u到根结点的距离。

思路:

主要思想是并查集,记录一下每个结点到根结点的距离即可。

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4
 5 const int maxn = 20000 + 5;
 6
 7 int p[maxn], d[maxn];
 8 int n;
 9 char c;
10
11 int find(int x)
12 {
13     if (p[x] != x)
14         return d[x] + find(p[x]);
15     else
16         return d[x];
17 }
18
19 int main()
20 {
21     //freopen("D:\\txt.txt", "r", stdin);
22     int T;
23     scanf("%d", &T);
24     while (T--)
25     {
26         memset(d, 0, sizeof(d));
27         scanf("%d", &n);
28         for (int i = 0; i <= n; i++)
29             p[i] = i;
30         int u, v;
31         while (scanf("%c",&c) && c != ‘O‘)
32         {
33             if (c == ‘E‘)
34             {
35                 scanf("%d", &u);
36                 int sum=find(u);
37                 cout << sum << endl;
38             }
39             else if (c == ‘I‘)
40             {
41                 scanf("%d%d", &u, &v);
42                 p[u] = v;
43                 d[u] = abs(v - u) % 1000;
44             }
45         }
46     }
47 }
时间: 2024-08-02 04:46:26

LA 3027 合作网络的相关文章

[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

并查集(路径更新) LA 3027 Corporative Network

题目传送门 题意:训练指南P192 分析:主要就是一个在路径压缩的过程中,更新点i到根的距离 #include <bits/stdc++.h> using namespace std; const int N = 2e4 + 5; struct DSU { int rt[N], d[N]; void init(void) { memset (rt, -1, sizeof (rt)); memset (d, 0, sizeof (d)); } int Find(int x) { if (rt[x

(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】

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

LA 3027 Corporative Network

这题感觉和 POJ 1988 Cube Stacking 很像,在路径压缩的同时递归出来的时候跟新distant数组 1 //#define LOCAL 2 #include <algorithm> 3 #include <cstdio> 4 using namespace std; 5 6 const int maxn = 20000 + 10; 7 int parent[maxn], distant[maxn]; 8 9 int GetParent(int a) 10 { 11

LA 3027 Corporative Network(并查集)

有n个点,一开始都是孤立的,然后有I,E两种操作 I        u v,把u的父节点设为v,距离为abs(u-v) % 1000,保证u之前没有父节点 E      询问u到根节点的距离 #include<iostream> #include<cmath> using namespace std; const int maxn=2e4+5; int par[maxn],dis[maxn]; void init(int n) { for(int i=0;i<=n;i++)

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

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

hdu 5745 la vie en rose

这道题的官方题解是dp,但是可以暴力出来.改天再研究怎么dp. 暴力的时候,如果计算sum的时候,调用strlen函数会超时,可见这个函数并不是十分的好.以后能不用尽量不用. La Vie en rose Time Limit: 14000/7000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 861    Accepted Submission(s): 461 Problem