poj 1962 Corporative Network
主题链接: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的更多相关文章
- 【35.86%】【POJ 1962】Corporative Network
Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 3943 Accepted: 1414 Description A very bi ...
- UVALive 3027 Corporative Network
---恢复内容开始--- Corporative Network Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld ...
- 【暑假】[实用数据结构]UVAlive 3027 Corporative Network
UVAlive 3027 Corporative Network 题目: Corporative Network Time Limit: 3000MS Memory Limit: 30000K ...
- [LA] 3027 - Corporative Network [并查集]
A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...
- 3027 - Corporative Network(并差集)
3027 - Corporative Network A very big corporation is developing its corporative network. In the begi ...
- LA 3027 Corporative Network 并查集记录点到根的距离
Corporative Network Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [S ...
- 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 ...
- 【POJ 3694】 Network(割边<桥>+LCA)
[POJ 3694] Network(割边+LCA) Network Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7971 ...
- poj 1962(并查集+带权更新)
Corporative Network Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 3664 Accepted: 13 ...
随机推荐
- 【C语言天天练(二四)】内存分配
引言: 对于C语言程序,了解它执行时在内存中是怎样分配的对于我们理解它的执行机制是很实用的.以下就总结一下C语言程序的一些内存分配知识. 一 一段C程序.编译连接后形成的可运行文件一般有代码段.数据段 ...
- [ACM] poj 1258 Agri-Net (最小生成树)
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37131 Accepted: 14998 Descri ...
- Cygwin下vim按方向键出现ABCD;
1:乱码解决Option->Text设置编码 2:vim按方向键出现A.B.C.D 解决:--$ cd /usr/share/vim/vim73 (ps:看你的版本号.假设没有这个文件可能是/u ...
- J2EE互联网产品打造
CSDN的各位技术朋友们,你们好: 我司最近正在研发一套J2EE的互联网产品,前期功能设计例如以下: 1.权限管理 2.菜单管理 3.系统设置 4.页面管理[主要做静态化] 5.任务管理[数据同步以及 ...
- 【从翻译mos文章】正在实施的获取job的 session id
正在实施的获取job的 session id 参考原始: How to get the session Id of the Running Job (Doc ID 1604966.1) 申请: Ora ...
- 关于WHERE后面不能放聚合函数(如SUM(...))的解决办法
我们在编写SQL语句的时候,常常会遇到需要将SUM()放到WHERE后面作为条件查询,事实证明这样是无法执行的,执行会报异常:聚合不应出现在 WHERE 子句中. 那么如何解决呢,使用HAVING关键 ...
- ubuntu Linux 安装和首次使用
1.ubuntu Linux 安装后切换到root账户,在默认情况下,系统安装过程中需要创建一个用户,切换到root账号命令如下:$ sudo -s -H输入 当前账户密码就可以切换到root.2.u ...
- ubuntu下使用自带的openJDK查看java源码
如题 Ubuntu自带的OpenJDK仅仅有jre环境,不提供源代码,所以我们还是须要去下载. JDK6:http://download.java.net/openjdk/jdk6/ JDK7:htt ...
- VIM命令集
Command Action Notes vim file +54 open file and go to line 54 any : command can be run using + on co ...
- Android数字签名解析(二)
在Android数字签名解析(一)中,介绍了android进行签名的两种方式,当中用密钥对进行签名用到了signapk.jar这个java库. 以下我们就看看signapk签名实现过程,signapk ...