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 ...
随机推荐
- java中clone的深入理解
Java中Clone的概念大家应该都很熟悉了,它可以让我们很方便的“制造”出一个对象的副本来,下面来具体看看java中的Clone机制是如何工作的? 1. Clone和Copy 假 ...
- JSON支持什么对象/类型?
原文:JSON支持什么对象/类型? 当一个面试官问你: JSON都支持什么对象/类型?你怎么回答? 也许他的本意是下面这个答案: JSON格式支持的数据类型有以下: 类型 描述 Number 在Jav ...
- ipv6加英文的中括号访问
加英文的中括号就可以,如[2001:4998:c:e33::1004],我发现这是yahoo首页.但并不是所有IPv6网站都可以通过IPv6地址访问,跟IPv4一样,网站服务器端可以只绑定域名,不接受 ...
- A Game of Thrones(13) - Tyrion
The north went on forever. Tyrion Lannister knew the maps as well as anyone, but a fortnight on the ...
- 检查Linux Bash安全漏洞以及各环境修复解决方法
第一.检测是否存在Bash漏洞$env x='() { :;}; echo vulnerable' bash -c "echo this is a test"如果返回以下内容:则请 ...
- poj1860(spfa判正环)
题目连接:http://poj.org/problem?id=1860 题意:有多种从a到b的汇率,在你汇钱的过程中还需要支付手续费,那么你所得的钱是 money=(nowmoney-手续费)*rat ...
- extern int *a与extern int a[]
extern int *a与int a[] Table of Contents 1. 问题: 2. 解答: 1 问题: 以下的声明取自某个源文件: int a[10]; int *b=a; 但在还有一 ...
- HDU4344(大数分解)
题目:Mark the Rope 题意就是给一个数,然后求这个数的所有因子中组成的最大的一个子集,其中1和本身除外,使得在这个子集中元素两两互素,求最大子集的元素个 数,并且求出和最大的值. 找规律就 ...
- hosts文件导致无法访问网站
前段时间有人反映无论怎么样都无法在自己的电脑上访问法兰克官网,那台电脑的DNS也无法解析,通过查看hosts文件后发现,原来该电脑的hosts文件木马修改过了,屏蔽了相关的域名,删除新增的或者用其他机 ...
- java中常用的字符串的截取方法
java中常用的字符串的截取方法 1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int l ...