Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
![Download as PDF Download as PDF](https://icpcarchive.ecs.baylor.edu/components/com_onlinejudge/images/button_pdf.png)
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
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