算法思路:带权并查集。
但是要注意“权值”的选取,下面的代码采取的方案是:初始的时候保存每个节点到父节点的距离d[i], 当进行一次findset的时候同时压缩路径和依次更新路径上的每个结点的d[i], 使得此时的的d[i]为该节点到根节点的距离,执行findset 后cout<<d[i]就是题目的结果啦。(本菜鸟个人认为这种思想比较经典,有代表性,有助于类似的题目启发思维)
代码来源:刘汝佳 - 信息学竞赛入门经典(强烈推荐,难得的好书)
//模板开始
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <fstream>
#include <map>
#include <set>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include<iomanip>
#define SZ(x) (int(x.size()))
using namespace std;
int toInt(string s){
istringstream sin(s);
int t;
sin>>t;
return t;
}
template<class T> string toString(T x){
ostringstream sout;
sout<<x;
return sout.str();
}
typedef long long int64;
int64 toInt64(string s){
istringstream sin(s);
int64 t;
sin>>t;
return t;
}
template<class T> T gcd(T a, T b){
if(a<0)
return gcd(-a, b);
if(b<0)
return gcd(a, -b);
return (b == 0)? a : gcd(b, a % b);
}
//模板结束(通用部分)
#define ifs cin
const int maxn = 20000 + 10;
int pa[maxn], d[maxn];
int findset(int x)
{
if(pa[x] != x)
{
int root = findset(pa[x]);
d[x] += d[pa[x]];
return pa[x] = root;
}
else
{
return x;
}
}
//LA 3027 Corporative Network
int main()
{
//ifstream ifs("shuju.txt", ios::in);
int T;
ifs>>T;
while(T--)
{
int n, u, v;
char cmd[9];
ifs>>n;
for(int i = 1; i <= n; i++) //初始化,每个节点单独是一颗树
{
pa[i] = i;
d[i] = 0;
}
while(ifs>>cmd && cmd[0] != 'O')
{
if(cmd[0] == 'E')
{
ifs>>u;
findset(u);
cout<<d[u]<<endl;
}
if(cmd[0] == 'I')
{
ifs>>u>>v;
pa[u] = v;
d[u] = abs(u - v) % 1000;
}
}
}
return 0;
}