UVA 1329 Corporative Network
2014-06-24 18:33
323 查看
利用并查集的路径压缩来更新距离
#include <stdio.h>
const int max_n = 20000+100;
struct node{
int len2father;
int father_index;
};
struct node node_arr[max_n];
int _abs(int n){
return n<0 ? n*(-1) : n;
}
void merge(int child, int father){
node_arr[child].father_index = father;
node_arr[child].len2father = _abs(child-father)%1000;
}
//update the length between the child node and the father node while compressing the path
int get_root(int child){
int root, father;
if(node_arr[child].father_index == child){
return child;
}
father = node_arr[child].father_index;
root = get_root(father);
node_arr[child].len2father += node_arr[father].len2father;
node_arr[child].father_index = root;
return root;
}
int main(void){
int case_n, node_n, i;
int child, father;
char oper[10];
//freopen("input.dat", "r", stdin);
scanf("%d", &case_n);
while(case_n--){
scanf("%d", &node_n);
//init
for(i=1; i<=node_n; i++){
node_arr[i].father_index = i;
node_arr[i].len2father = 0;
}
while(1){
scanf("%s", oper);
if('O' == *oper)
break;
if('I' == *oper){
scanf("%d %d", &child, &father);
merge(child, father); //when merging, don't compress the path
}
else if('E' == *oper){
scanf("%d", &child);
get_root(child);
printf("%d\n", node_arr[child].len2father ); //only compress the path when search the root index
}
}
}
return 0;
}
#include <stdio.h>
const int max_n = 20000+100;
struct node{
int len2father;
int father_index;
};
struct node node_arr[max_n];
int _abs(int n){
return n<0 ? n*(-1) : n;
}
void merge(int child, int father){
node_arr[child].father_index = father;
node_arr[child].len2father = _abs(child-father)%1000;
}
//update the length between the child node and the father node while compressing the path
int get_root(int child){
int root, father;
if(node_arr[child].father_index == child){
return child;
}
father = node_arr[child].father_index;
root = get_root(father);
node_arr[child].len2father += node_arr[father].len2father;
node_arr[child].father_index = root;
return root;
}
int main(void){
int case_n, node_n, i;
int child, father;
char oper[10];
//freopen("input.dat", "r", stdin);
scanf("%d", &case_n);
while(case_n--){
scanf("%d", &node_n);
//init
for(i=1; i<=node_n; i++){
node_arr[i].father_index = i;
node_arr[i].len2father = 0;
}
while(1){
scanf("%s", oper);
if('O' == *oper)
break;
if('I' == *oper){
scanf("%d %d", &child, &father);
merge(child, father); //when merging, don't compress the path
}
else if('E' == *oper){
scanf("%d", &child);
get_root(child);
printf("%d\n", node_arr[child].len2father ); //only compress the path when search the root index
}
}
}
return 0;
}
相关文章推荐
- C - Corporative Network UVA - 1329 (并查集)
- C - Corporative Network UVA - 1329 -加权并查集
- Corporative Network UVA - 1329(并查集的修改与查询)
- Corporative Network UVA - 1329 加权并查集
- UVA 1329 - Corporative Network
- Corporative Network UVA - 1329 加权并查集(压缩路径优化)
- Corporative Network UVA - 1329
- Corporative Network +uvalive+并查集
- UVALive 3027---Corporative Network+并查集的应用
- Corporative Network UVALive - 3027 并查集
- UVA - 1267 Network
- UVA-1329 - Corporative Network(并查集)
- uva 1267 - Network(dfs)
- UVA 315 Network tarjan 双连通分量
- UVa 1329 - Corporative Network Union Find题解
- UVA - 1329 Corporative Network
- Network - UVA 315 - 无向图求割点
- poj_1144/uva_315/zoj_1311 Network(割點模板題)
- UVA 1329 Corporative Network(并查集:路径压缩)
- UVA 1329 Corporative Network——加权并查集