【LA 3027 Corporative Network】
·一些很可爱的询问和修改,放松地去用并查集解决。
·英文题,述大意:
输入n(5<=n<=20000)表示树有n个节点,并且会EOF结束地读入不超过 20000个操作,一共有两种:
①I v u:表示将v的父亲节点设置为u(在这之前v没有爸爸),边权设置为abs(v-u)%1000。
②E u:表示询问u到当前它所在树的根节点的距离。
·分析:
为了记录当前一系列加边操作后所有的点的位置情况(因为你随时可能回答询问啊),根据这道题的点关系特点[只在乎点和其根节点的信息],我们选择并查集来加以维护。
然后我们只需要在标准的FindFather函数的回溯过程里面加入边权的累积,这样一次函数就可以既完成路径压缩,又维护了沿途所有点各自到根节点的距离(就是边权和)。
然后这么短的题解让我想起了网络上的人们常常使用的一句题解推托之词:“哎呀,其他的搞一搞就出来了”。但是这道题真是这么单纯。OK。
#include<stdio.h>
#define go(i,a,b) for(int i=a;i<=b;i++)
int T,n,fa[],d[];char c;
int A(int a){return a>?a:-a;}
int find(int u)
{
if(u==fa[u])return u;int Fa=find(fa[u]);
d[u]+=d[fa[u]];return fa[u]=Fa;
}
int main()
{
scanf("%d",&T);while(T--&&scanf("%d",&n))
{
go(u,,n)d[fa[u]=u]=;int u,v;
while(scanf(" %c",&c),c!='O')
{
if(c=='I')scanf("%d%d",&v,&u),fa[v]=u,d[v]=A(v-u)%;
if(c=='E')scanf("%d",&u),fa[u]=find(u),printf("%d\n",d[u]);
}
}
return ;
}//Paul_Guderian
明天当孤独袭来时我不会再流一滴泪,
我会用歌声抹去那创痛的灰烬。—————汪峰《明天》
【LA 3027 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 ...
- 并查集(路径更新) LA 3027 Corporative Network
题目传送门 题意:训练指南P192 分析:主要就是一个在路径压缩的过程中,更新点i到根的距离 #include <bits/stdc++.h> using namespace std; c ...
- LA 3027 Corporative Network
这题感觉和 POJ 1988 Cube Stacking 很像,在路径压缩的同时递归出来的时候跟新distant数组 我发现我一直WA的原因是,命令结束是以字母o结束的,而不是数字0!! //#def ...
- 【暑假】[实用数据结构]UVAlive 3027 Corporative Network
UVAlive 3027 Corporative Network 题目: Corporative Network Time Limit: 3000MS Memory Limit: 30000K ...
- 3027 - Corporative Network(并差集)
3027 - Corporative Network A very big corporation is developing its corporative network. In the begi ...
- 3027 - Corporative Network
3027 - Corporative Network 思路:并查集: cost记录当前点到根节点的距离,每次合并时路径压缩将cost更新. 1 #include<stdio.h> 2 #i ...
- UVALive 3027 Corporative Network
---恢复内容开始--- Corporative Network Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld ...
- UVALive 3027 Corporative Network 带权并查集
Corporative Network A very big corporation is developing its corporative networ ...
随机推荐
- animation & @keyframes 实现loading效果
效果图截图如下: 直接上代码: html <!DOCTYPE html> <html> <head> <meta charset="utf-8&qu ...
- 数据结构与算法 —— 链表linked list(02)
我们继续来看链表的第二道题,来自于leetcode: 两数相加 给定两个非空链表来代表两个非负整数,位数按照逆序方式存储,它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表. 你可以假设除了 ...
- MySQL/MariaDB中游标的使用
本文目录:1.游标说明2.使用游标3.游标使用示例 1.游标说明 游标,有些地方也称为光标.它的作用是在一个结果集中逐条逐条地获取记录行并操作它们. 例如: 其中select是游标所操作的结果集,游标 ...
- php_类的定义
此文章为原创见解,例子各方面也是东拼西凑.如果有错请留言.谢谢 在面向对象的思维中提出了两个概念,类和对象. 类是对某一类实物的抽象描述,而对象用于表示现实中该类事物的个体, 例子:老虎是父类,东北虎 ...
- django报错invalid literal for int() with base 10: ''
这种错误是因为模型类中某个字段的条件约束为int类型,但是给了一个字符串类型,所以报错,找到那个模型类的字段,并对应修改就好了.
- python hashlib、hmac模块
一.hashlib模块 import hashlib m = hashlib.md5() m.update(b"Hello") print(m.hexdigest()) m.upd ...
- BizTalk Server 2016配置 WCF SAP Adapter
BizTalk Server 2016配置 WCF SAP Adapter 最近公司内部需要使用BizTalk与SAP 系统进行对接,虽然SAP/PI可以以发布WebService 的方式实现与外部系 ...
- Python内置函数(41)——id
英文文档: id(object) Return the "identity" of an object. This is an integer which is guarantee ...
- leetcode算法:Two Sum II - Input array is sorted
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- spring mvc跨域(post)--filter方案
import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.Http ...