- Time: 2016-03-19-13:24:23 Saturday
- Title Number: [2016-03-19][uvalive][3027][corporative Network]
- The main topic: given n nodes, I u v means the parent node of the U node is set to V, the distance is |u-v|%1000,e u to ask u to the root node distance, given a number of I-E operations, output the corresponding answer
- Analysis: Take right and check the set
- Method: D[MAXN] Maintenance to the parent node distance, each time the query, the current node compression to the root node output corresponding distance can be
#ifdef _WORK_
#include <algorithm>
#include <cstdio>
#include<cstring>
using namespace std;
#define CLR(x,y) memset((x),(y),sizeof((x)))
#define for ( x y z ) for ( int ( x ) = ( y );( x z x
const int maxn = 20000 +10;
int fa[maxn],d[maxn];
inline void ini(int n){
CLR(d,0);
FOR(i,0,n+1) fa[i] = i;
}
int fnd(int x){
if(x == fa[x]) return x;
int tmp = fa[x];
fa[x] = fnd(fa[x]);
d[x] = d[x] + d[tmp];
return fa[x];
}
int main(){
int t,n,p,q;
char str[10];
scanf("%d",&t);
while(t--){
scanf("%d",&n);
ini(n);
while(~scanf("%s",str) && str[0] != ‘O‘){
if(str[0] == ‘I‘){
scanf("%d%d",&p,&q);
fa[p] = q;
d[p] = abs(p - q)%1000;
}else{
scanf("%d",&p);
fnd(p);
printf("%d\n",d[p]);
}
}
}
return 0;
}
From for notes (Wiz)
[2016-03-19] [Uvalive] [3027] [Corporative Network]