Corporative Network_并查集
Description
Input
E I – asking the length of the path from the enterprise I to its serving center in the moment;
I I J – informing that the serving center I is linked to the enterprise J.
The test case finishes with a line containing the word O. The I commands are less than N.
Output
Sample Input
1
4
E 3
I 3 1
E 3
I 1 2
E 3
I 2 4
E 3
O
Sample Output
0
2
3
5
【题意】给出t个例子,有n个结点,刚开始结点以自己为终点,而后I后给出两个x,y,表示x指向y,E后给出一个x,问x的终点与他的距离mod1000;
【解法】并查集
#include<iostream>
#include<math.h>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=; int t,n,x,y;
char str[];
int fa[N],dis[N];
int findx(int x)
{
if(x==fa[x])
return x;
int fx=findx(fa[x]);
dis[x]+=dis[fa[x]];
return fa[x]=fx;
}
void merge(int x,int y)
{
int fx=findx(x),fy=findx(y);
if(fx==fy)
{
return ;
}
fa[x]=x;dis[fx]=dis[x];
fa[x]=y;
dis[x]=abs(x-y)%;
}
void init()
{
for(int i=;i<=n;i++)
{
dis[i]=;
fa[i]=i;
}
}
int main()
{ scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
init(); while(~scanf("%s",str))
{
if(str[]=='O') break;
else if(str[]=='E')
{
scanf("%d",&x);
findx(x);
printf("%d\n",dis[x]);
}
else
{
scanf("%d%d",&x,&y);
merge(x,y);
}
}
}
return ;
}
Corporative Network_并查集的更多相关文章
- [LA] 3027 - Corporative Network [并查集]
A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...
- LA 3027 Corporative Network 并查集记录点到根的距离
Corporative Network Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [S ...
- UVALive - 3027 Corporative Network (并查集)
这题比较简单,注意路径压缩即可. AC代码 //#define LOCAL #include <stdio.h> #include <algorithm> using name ...
- UVALive 3027 Corporative Network 带权并查集
Corporative Network A very big corporation is developing its corporative networ ...
- 并查集 + 路径压缩(经典) UVALive 3027 Corporative Network
Corporative Network Problem's Link Mean: 有n个结点,一开始所有结点都是相互独立的,有两种操作: I u v:把v设为u的父节点,edge(u,v)的距离为ab ...
- Corporative Network (有n个节点,然后执行I u,v(把u的父节点设为v)和E u(询问u到根节点的距离))并查集
A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...
- POJ1962:Corporative Network【带权并查集】
<题目链接> 题目大意: n个节点,若干次询问,I x y表示从x连一条边到y,权值为|x-y|%1000:E x表示询问x到x所指向的终点的距离. 解题分析: 与普通的带权并查集类似 ...
- UVALive 3027 Corporative Network (带权并查集)
题意: 有 n 个节点,初始时每个节点的父节点都不存在,你的任务是执行一次 I 操作 和 E 操作,含义如下: I u v : 把节点 u 的父节点设为 v ,距离为| u - v | ...
- UVA 3027 Corporative Network 带权并查集、
题意:一个企业要去收购一些公司把,使的每个企业之间互联,刚开始每个公司互相独立 给出n个公司,两种操作 E I:询问I到I它连接点最后一个公司的距离 I I J:将I公司指向J公司,也就是J公司是I公 ...
随机推荐
- S2 第二章数据库的实现
实现增删改查代码 1 select * from student --增加数据 insert into student (name,banji,xuehao) values(,) --修改数据 upd ...
- SpringMVC 配置定时执行任务
1.在SpringMVC配置文件中添加 xmlns:task="http://www.springframework.org/schema/task" http://www.spr ...
- Play framework logging设置
play的logger是基于Log4j,Play 2.0 uses logback as its logging engine. 一.配置 1. 在conf/application.conf中设置lo ...
- 241. Different Ways to Add Parentheses——本质:DFS
Given a string of numbers and operators, return all possible results from computing all the differen ...
- java 面向对象编程 --第十二章 JDK常用类
1. 系统类 java.lang包 System类 sys.out;sys.exit;sys.gc; sys.currentTimeMillis();----得到从1970-01-01到当前时间 ...
- admob 广告Android不显示
我弄个了ane,iOS上好好的,Android上打死不显示, 最后发现是少在xml里面增加一个配置,百度半天没搜索到. <android> <manifestAddi ...
- ARM2440换lcd
将原来的3.5寸分辨率为240x320换为480x272所需要修改的地方 时序设置: CLKVAL=4 (VCLK =10) 5< VCLK <12 每个点扫描周期 ...
- nuget的小Tips
1.nuget常用命令 nuget spec -a bin\Debug\DllName.dll -f //根据dll生成.nuspec文件,这样会生成无用的默认标签,比如licenseUrl.tags ...
- wp8.1 Study14 FilePicker简单介绍
一.FileOpenPicker/FileSavePicker介绍 这个在使用手机中是十分经常的,如在朋友圈中你要发图片,首先点击添加图片,而后你会进入手机图片区,选择图片后自动返回朋友圈准备发图界面 ...
- ClassLoader相关内容
1.什么叫做bootstrap?作为形容词有依靠自己力量的:自己做的等意思,在我们计算机世界里,一般指的是自举,引导,引导程序. 那什么是bootstrapClassLoader呢?它是引导加载器,也 ...