UVAalive 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 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;
}<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;
}

 

 

 

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值