题目有2种操作, 一种是查询,一种是设置。

设置为将u的父亲设置为v,然后他们之间的距离为|u-v|%1000

查询为该点到根点的距离

用并查集做,做的时候注意维护即可,注意取余操作。

代码:

 #include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
const int N = ;
int d[N],F[N];
int init(int x)
{
for(int i =;i<=x;i++)
{
F[i] = i;
d[i]=;
}
}
int findset(int x)
{
if(F[x]!=x)
{
int root = findset(F[x]);
d[x] = d[x]+d[F[x]];
return F[x] = root;
}
else return x;
}
int main()
{
int T,n;
char s[];
scanf("%d",&T);
while(T--)
{
int n,u,v;
scanf("%d",&n);
init(n);
while(scanf("%s",s) && s[]!='O')
{
if(s[] == 'E')
{
scanf("%d",&u);
findset(u);
printf("%d\n",d[u]);
}
else
{
scanf("%d%d",&u,&v);
F[u] = v;
d[u] = abs(u-v)%;
}
}
}
return ;
}

POJ 1962-Corporative Network (并查集)的更多相关文章

  1. [LA] 3027 - Corporative Network [并查集]

    A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...

  2. LA 3027 Corporative Network 并查集记录点到根的距离

    Corporative Network Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [S ...

  3. POJ 2236 Wireless Network (并查集)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 18066   Accepted: 761 ...

  4. poj 2236 Wireless Network (并查集)

    链接:http://poj.org/problem?id=2236 题意: 有一个计算机网络,n台计算机全部坏了,给你两种操作: 1.O x 修复第x台计算机 2.S x,y 判断两台计算机是否联通 ...

  5. poj 1962 Corporative Network

    主题链接:http://poj.org/problem?id=1962 思路:每一个集合中用根节点标记这个集合,每一个点到根节点的距离. code: <span style="font ...

  6. POJ 2236 Wireless Network [并查集+几何坐标 ]

    An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wi ...

  7. UVALive - 3027 Corporative Network (并查集)

    这题比较简单,注意路径压缩即可. AC代码 //#define LOCAL #include <stdio.h> #include <algorithm> using name ...

  8. Network POJ - 3694(lca并查集+连通图求桥)

    就是求出原先图中的桥的数量,在每一次询问时加入一条新边,求加入当前边后图中剩余的桥的数量 求出原先图中的桥的数量,然后减去新加入边的两端点之间的桥的数量,就是剩余桥的数量.. 用并查集把属于同一集合的 ...

  9. Corporative Network_并查集

    Description A very big corporation is developing its corporative network. In the beginning each of t ...

  10. poj 1733(带权并查集+离散化)

    题目链接:http://poj.org/problem?id=1733 思路:这题一看就想到要用并查集做了,不过一看数据这么大,感觉有点棘手,其实,我们仔细一想可以发现,我们需要记录的是出现过的节点到 ...

随机推荐

  1. (判断url文件大小)关于inputStream.available()方法获取下载文件的总大小

    转自:http://hold-on.iteye.com/blog/1017449 如果用inputStream对象的available()方法获取流中可读取的数据大小,通常我们调用这个函数是在下载文件 ...

  2. 项目中简单使用ztree,简单数据。

    由于公司架构较旧,使用的jdk版本为1.4,页面上也没有el表达式. 加入 js 文件 <% String context = request.getContextPath(); %> & ...

  3. 二十四种设计模式:抽象工厂模式(Abstract Factory Pattern)

    抽象工厂模式(Abstract Factory Pattern) 介绍提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 示例有Message和MessageModel,Messag ...

  4. Codeforces Round #313 (Div. 1) Gerald&#39;s Hexagon

    http://codeforces.com/contest/559/problem/A 题目大意:按顺序给出一个各内角均为120°的六边形的六条边长,求该六边形能分解成多少个边长为1的单位三角形. 解 ...

  5. request.startAsync()不支持异步操作

    Servlet3.0使用异步处理时,后台报错: java.lang.IllegalStateException: A filter or servlet of the current chain do ...

  6. NetBiosDomainNamesEnabled与SharePoint User Profile Service Application

    本文中的内容转自参考资料中的文章.   如果域的NetBios名字跟Fully Qualified Domain Name不同的话, 那么就需要开启UPA的NetBiosDomainNamesEnab ...

  7. [Android Pro] 注册 Google Play 开发者帐户

    官网地址: https://support.google.com/googleplay/android-developer/answer/6112435?hl=zh-Hans 博客地址: http:/ ...

  8. zabbix_zatree第三方插件

    1.下载zatree第三方插件https://github.com/spide4k/zatree.git 2.检查PHP环境需要支持php-xml.php-gd.php-mysql 3.先备份当前za ...

  9. Asp.Net 之 二维码生成

    首先,引用 ThoughtWorks.QRCode.dll . 简单二维码生成及解码代码: //生成二维码方法一 private void CreateCode_Simple(string nr) { ...

  10. centos下安装pyspider

    环境: * CentOS release 6.4 (Final) * Python 2.6.6 pip install pyspider   ,发现lxml没有安装好 直接sudo yum insta ...