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 ...
随机推荐
- 基于Qt的类QQ气泡聊天的界面开发
近期在写IM 聊天界面,想设计出一个类似QQ气泡聊天的样式 使用了几种办法 1:使用Qt以下的QListview来实现QQ类似效果.差强人意 2:使用QWebview载入html css样式来完毕.发 ...
- PCI 总线学习笔记
转载请注明出处:http://blog.csdn.net/lg2lh/article/details/8042008 PCI的基本协议这里就不介绍了,由于一般的芯片协议都是集成好的,我仅仅须要大体了解 ...
- poj 1872 A Dicey Problem WA的代码,望各位指教!!!
A Dicey Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 832 Accepted: 278 Des ...
- Android开发 更改返回button的图标
非常多的Android应用左上角都有返回button 在默认的情况下 ADT会默认给一个返回图标 而作为开发需求 非常多都要求定制一个新的图标 在Android的站点上 发现了2种能够更改的方法 1. ...
- java多线程控制台聊天室(转)
用java多线程实现一个控制台聊天室,呵呵,好玩! 聊天室服务器端 package tf.thread; import java.io.BufferedReader; import java.io.I ...
- hdu5618 (三维偏序,cdq分治)
给定空间中的n个点,问每个点有多少个点小于等于自己. 先来分析简单的二维的情况,那么只要将x坐标排序,那么这样的问题就可以划分为两个子问题,,这样的分治有一个特点,即前一个子问题的解决是独立的,而后一 ...
- java中取得上下文路径的方法
1.request.getContextPath(); 获得web根的上下文环境 如 /tree tree是web项目的root context 2.可以在servlet的init方法里 String ...
- hdu4585 & BestCoder Round #1 项目管理(vector应用)
主题链接:pid=4858">http://acm.hdu.edu.cn/showproblem.php?pid=4858 项目管理 Time Limit: 2000/1000 MS ...
- 微软 Build 2016
微软 Build 2016年开发者大会发布多项功能升级 微软Build 2016开发者大会在美国旧金山的莫斯康展览中心开幕.本次大会对一些重点功能进行了完善.如手写笔支持技术Windows Ink.语 ...
- 创建ASPState数据库
原文:创建ASPState数据库 在C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727下找到生成ASPState的sql:InstallSqlState.sql ...