主题链接:http://poj.org/problem?id=1962

思路:每一个集合中用根节点标记这个集合,每一个点到根节点的距离。

code:

<span style="font-size:18px;">#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<set> using namespace std; const int maxn=20005; int pa[maxn],d[maxn]; int findset(int x) //找出x节点到根节点的距离
{
if(pa[x]!=x)
{
int root=findset(pa[x]);
d[x]+=d[pa[x]]; //更新x节点的距离
return pa[x]=root;
}
else return x;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
char str[10];
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
pa[i]=i;
d[i]=0;
}
while(scanf("%s",str))
{
if(str[0]=='O') break;
if(str[0]=='E')
{
int x;
scanf("%d",&x);
findset(x);
printf("%d\n",d[x]);
}
if(str[0]=='I')
{
int x,y;
scanf("%d%d",&x,&y);
pa[x]=y;
d[x]=abs(x-y)%1000;
}
}
}
return 0;
}
</span>

版权声明:本文博主原创文章,博客,未经同意不得转载。

poj 1962 Corporative Network的更多相关文章

  1. 【35.86%】【POJ 1962】Corporative Network

    Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 3943 Accepted: 1414 Description A very bi ...

  2. UVALive 3027 Corporative Network

    ---恢复内容开始--- Corporative Network Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld ...

  3. 【暑假】[实用数据结构]UVAlive 3027 Corporative Network

    UVAlive 3027 Corporative Network 题目:   Corporative Network Time Limit: 3000MS   Memory Limit: 30000K ...

  4. [LA] 3027 - Corporative Network [并查集]

    A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...

  5. 3027 - Corporative Network(并差集)

    3027 - Corporative Network A very big corporation is developing its corporative network. In the begi ...

  6. LA 3027 Corporative Network 并查集记录点到根的距离

    Corporative Network Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [S ...

  7. POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Network / FZU 1161 (网络流,最大流)

    POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Networ ...

  8. 【POJ 3694】 Network(割边&lt;桥&gt;+LCA)

    [POJ 3694] Network(割边+LCA) Network Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7971 ...

  9. poj 1962(并查集+带权更新)

    Corporative Network Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 3664   Accepted: 13 ...

随机推荐

  1. How to use STA(sql tuning advisor)

    一.手工生成Sql tuning advisor  1.SQL text format: DECLARE   my_task_name VARCHAR2(30);   my_sqltext   CLO ...

  2. Defender Game 游戏实践(1) 基本游戏场景实现

    在网上看到 郑州|boy 这个博客,里面有几篇文章,记录了其用cocos2d-x这个游戏引擎编写的一个游戏,十分不错,所以这段时间,依样画葫芦,依次学习一下. 由于博主开发的平台是在win32,而且屏 ...

  3. 属性“dataProvider”有多个初始值设定项。(注意:“dataProvider”是“mx.charts.BarChart”的默认属性)。

    1.错误描写叙述 属性"dataProvider"有多个初始值设定项.(注意:"dataProvider"是"mx.charts.BarChart&q ...

  4. UVa 213 Message Decoding(World Finals1991,串)

     Message Decoding  Some message encoding schemes require that an encoded message be sent in two part ...

  5. poj3126(bfs)

    题目链接:http://poj.org/problem?id=3126 题意:给两个四位数n,m,将n变成m需要多少步,要求每次只能改变n的某一位数,即改变后的数与改变前的数只有一位不同,且每次改变后 ...

  6. codechef Sums in a Triangle题解

    Let's consider a triangle of numbers in which a number appears in the first line, two numbers appear ...

  7. sleep和wait的区别

    sleep指线程被调用时,占着CPU不工作,形象地说明为“占着CPU睡觉”,此时,系统的CPU部分资源被占用,其他线程无法进入,会增加时间限制.wait指线程处于进入等待状态,形象地说明为“等待使用C ...

  8. unity中的MonoBehaviour.OnMouseDown()

    在官网的api文档中仅说明了 Description OnMouseDown is called when the user has pressed the mouse button while ov ...

  9. iText操作word文档总结

    操作word文档的工具有很多,除了iText之外还有POI,但是POI擅长的功能是操作excel,虽然也可以操作word,但是能力有限,而且还有很多的bug,技术并不成熟,下面就重点介绍一种操作wor ...

  10. 图像库---Image Datasets---OpenSift源代码---openSurf源代码

    1.Computer Vision Datasets on the web http://www.cvpapers.com/datasets.html 2.Dataset Reference http ...