LA 3027 Corporative Network 并查集记录点到根的距离

Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

[]   [Go Back]   [Status]  

Description

Download as PDF
 

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

题目大意:有N个结点,初始时每个结点的父亲都不存在,你的任务时执行一次I操作和E操作,格式如下:

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

E u   :询问u 到根节点的距离

 

思路:

带距离的并查集,在每次合并父亲节点的时候更新一个距离就可以了

可参考刘汝佳入门经典训练指南

 
 

#include <iostream>
using namespace std;
const int maxn = 20000 + 10;        
int d[maxn], fa[maxn];       
int find(int x)      
{
	if(x == fa[x]) return x;
	else
	{
		int root = find(fa[x]);
		d[x] += d[fa[x]];       
		return fa[x] = root;       
	}
}
int main()
{
	int T, N, i, I, J;
	cin>>T;
	while(T--)
	{
		cin>>N;
		for(i = 0; i <= N; i++)      
		{
			fa[i] = i;       
			d[i] = 0;       //自己到自己(根)的距离为0
		}
		char c;
		bool ok = 1;         
		while(ok && cin>>c)
		{
			switch(c)
			{
			case 'O':
				{
					ok = 0;
					break;
				}
			case 'I':
				{
					//int x,y;
					cin>>I>>J;
				/*	x=find(I);
					y=find(J);
					if(x==y)  continue;
					fa[x]=y;
					*/
					fa[I] = J;
					int ans = I > J ? (I-J) : (J-I);
					d[I] =ans % 1000;      
					break;
				}
			case 'E':
				{
					cin>>I;
					find(I);         
					cout<<d[I]<<endl;
					break;
				}
			}
		}
	}
	return 0;
}


 

 

不能按代码中注释的 那样

x=find(I);

y=find(J);

if(x==y) continue;

fa[x]=y;

代替 fa[I] = J;

一换掉 就wa  

:  假设用dis表示点到根的距离

因为 为了求dis   所以谁的父亲就是谁的父亲 不能压缩 , 但是上面的find代码是进行压缩的     不过它是在递归求dis后 才压缩的    所以说 是可以压缩的   

即 必须先递归 后压缩     这样就能保证求出的dis 确实是按照父亲节点算出来的

上面的那个错误  是由于先调用find函数压缩 ,这时候 点  I 的 dis还没有算出来,后面再求它的dis的时候 其就不再是按照真正的父亲节点找出的 而是按照祖先节点算的(因为压缩过了)

对于之后输入E   再次调用fand函数的时候  虽然已经压缩过了   但是所有点的dis已经求出来了  对于再加入的边 I  J   我们依旧能够通过接到已经求出的正确dis 求出来I 的dis

注意dis表示 点到根的距离

 

我的叙述能力比较差          想了好久才想出这么点东西  如果不对  请大家指正           希望有人能明白我讲述的意思吧    

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33982

 

CH341A编程器是一款广泛应用的通用编程设备,尤其在电子工程和嵌入式系统开发领域中,它被用来烧录各种类型的微控制器、存储器和其他IC芯片。这款编程器的最新版本为1.3,它的一个显著特是增加了对25Q256等32M芯片的支持。 25Q256是一种串行EEPROM(电可擦可编程只读存储器)芯片,通常用于存储程序代码、配置数据或其他非易失性信息。32M在这里指的是存储容量,即该芯片可以存储32兆位(Mbit)的数据,换算成字节数就是4MB。这种大容量的存储器在许多嵌入式系统中都有应用,例如汽车电子、工业控制、消费电子设备等。 CH341A编程器的1.3版更新,意味着它可以与更多的芯片型号兼容,特别是针对32M容量的芯片进行了优化,提高了编程效率和稳定性。26系列芯片通常指的是Microchip公司的25系列SPI(串行外围接口)EEPROM产品线,这些芯片广泛应用于各种需要小体积、低功耗和非易失性存储的应用场景。 全功能版的CH341A编程器不仅支持25Q256,还支持其他大容量芯片,这意味着它具有广泛的兼容性,能够满足不同项目的需求。这包括但不限于微控制器、EPROM、EEPROM、闪存、逻辑门电路等多种类型芯片的编程。 使用CH341A编程器进行编程操作时,首先需要将设备通过USB连接到计算机,然后安装相应的驱动程序和编程软件。在本例中,压缩包中的"CH341A_1.30"很可能是编程软件的安装程序。安装后,用户可以通过软件界面选择需要编程的芯片类型,加载待烧录的固件或数据,然后执行编程操作。编程过程中需要注意的是,确保正确设置芯片的电压、时钟频率等参数,以防止损坏芯片。 CH341A编程器1.3版是面向电子爱好者和专业工程师的一款实用工具,其强大的兼容性和易用性使其在众多编程器中脱颖而出。对于需要处理25Q256等32M芯片的项目,或者26系列芯片的编程工作,CH341A编程器是理想的选择。通过持续的软件更新和升级,它保持了与现代电子技术同步,确保用户能方便地对各种芯片进行编程和调试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值