点击打开链接uva 1329
思路: 带权并查集
分析:
1 看懂题目就是切菜了
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 20010;
int n;
int father[MAXN];
int rank[MAXN];
void init(){
memset(rank , 0 , sizeof(rank));
for(int i = 1 ; i < MAXN ; i++)
father[i] = i;
}
int find(int x){
if(father[x] == x)
return x;
int tmp = father[x];
father[x] = find(father[x]);
rank[x] += rank[tmp];
return father[x];
}
int main(){
int Case;
char c;
int x , y;
scanf("%d" , &Case);
while(Case--){
init();
scanf("%d%*c" , &n);
while(scanf("%c" , &c) && c != 'O'){
if(c == 'E'){
scanf("%d%*c" , &x);
int tmp = find(x);
printf("%d\n" , rank[x]);
}
else{
scanf("%d%d%*c" , &x , &y);
int fx = find(x);
int fy = find(y);
if(fx != fy){
father[fx] = fy;
rank[fx] = abs(x-y)%1000+rank[y]-rank[x];
}
}
}
}
return 0;
}
分享到:
相关推荐
法人出版系统。 一个供企业规模使用的内容管理系统,该系统需要通过Web发布大量内容。 该系统被建模为使用模块化许可体系结构来支持大型开发环境。
马西拉商店 该项目有2个层:Store(django)和Corporative Website(node),每个文件夹都有自己的自述文件。
Corporative 这项研究检验了 MSCA 在区分学习障碍儿童和普通教育儿童方面的诊断价值。 数据通过多种方法进行分析,包括平均得分比较、轮廓分析和判别分析。 结果表明,学习障碍儿童在 MSCA GCI 和所有五个主要量表...
Comparison of learning disabled and general education ...Porter County Special Education Corporative This study examined the diagnostic value of the MSCA in discriminating between learning disabled an
Corporative Lorem ipsum dolor sit amet, consectetur adipiscing elit. Delivery 中国领先的移动应用分发平台,致力于为用户在移动互联网时代提供丰富、安全、个性化的手机应用软件、游戏和内容。同时,依托...