有N个结点

一次 I u v 操作表示把结点u的父结点设为v,距离为|u-v|%1000.输入保证执行指令前u没有父结点

一次E u 操作表示询问u到根结点的距离

O操作表示结束

 #include<iostream>
 #include<cstring>
 #include<cmath>
 using namespace std;
 ;
 int T,n,level,d[maxn],fa[maxn];
 void init()
 {
     memset(d,,sizeof(d));
     ;i<=n;i++) fa[i]=i;
 }
 int find(int x){
     if(fa[x]==x) return x;
     int ans=find(fa[x]);
     level+=d[x];
     d[x]=level;
     fa[x]=ans;
     return ans;
 }
 int main()
 {
     char op;
     cin>>T;
     while(T--){
         cin>>n;
         init();
         while(cin>>op){
             if(op=='O') break;
             else if(op=='I'){
                 int u,v;
                 cin>>u>>v;
                 d[u]=abs(u-v)%,fa[u]=v;
             }else if(op=='E'){
                 int x;cin>>x;
                 if(fa[x]==x){
                     cout<<<<endl;continue;
                 }
                 level=;
                 find(x);
                 cout<<d[x]<<endl;
             }
         }
     }
     ;
 }

LA3027 合作网络-并查集压缩路径的更多相关文章

  1. POJ3728The merchant (倍增)(LCA)(DP)(经典)(||并查集压缩路径?)

    There are N cities in a country, and there is one and only one simple path between each pair of citi ...

  2. poj1703Find them, Catch them(并查集以及路径压缩)

    /* 题目大意:有两个不同的黑帮,开始的时候不清楚每个人是属于哪个的! 执行两个操作 A a, b回答a, b两个人是否在同一帮派,或者不确定 D a, b表示a, b两个人不在同一个帮派 思路:利用 ...

  3. [HDOJ2818]Building Block(带权并查集,路径压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2818 题意:有N个块,每次有两个操作: M x y表示把x所在的那一堆全部移到y所在的那一堆的下方. ...

  4. [HDOJ3635]Dragon Balls(并查集,路径压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3635 题意:有n个龙珠,n个城市.初始状态第i个龙珠在第i个城市里.接下来有两个操作: T A B:把 ...

  5. 关于并查集的路径压缩(Path Compress)优化

    之前在CSDN看到一篇很受欢迎的讲解并查集的博文,其中自然用到了路径压缩: int pre[1000]; int find(int x){ int root = x; while(pre[root]! ...

  6. BZOJ 3674 可持久化并查集加强版(路径压缩版本)

    /* bzoj 3674: 可持久化并查集加强版 http://www.lydsy.com/JudgeOnline/problem.php?id=3674 用可持久化线段树维护可持久化数组从而实现可持 ...

  7. LA 3027 合作网络 并查集

    题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  8. poj1456Supermarket——并查集压缩查找

    题目:http://poj.org/problem?id=1456 排序+贪心,每次选利润最大的,放在可能的最靠后的日期卖出,利用并查集快速找到下一个符合的日期. 代码如下: #include< ...

  9. codeforces #541 F Asya And Kittens(并查集+输出路径)

    F. Asya And Kittens Asya loves animals very much. Recently, she purchased nn kittens, enumerated the ...

随机推荐

  1. SVN学习之参数定义及设置

    http://jackyrong.iteye.com/blog/238835 conf目录中打开svnserve.conf ,去掉注释,搞成如下这样 [general] anon-access = r ...

  2. mousewheel滚轮事件 浏览器的写法

    鼠标的滚轮事件,在Jquery中有对应的一个插件:https://github.com/brandonaaron/jquery-mousewheel 原生的滚轮事件:火狐与其他浏览器使用了不同的事件 ...

  3. 黄聪:wordpress后台加载ajax.googleapis.com导致打开速度很慢的解决方案

    打开wordpress后台,发现很卡,通过开发者工具看到是因为加载http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothn ...

  4. butterknife 使用注意事项

    写了个demo,一直报错 Caused by: java.lang.IllegalStateException: Required view 'tv1' with ID 2131492943 for ...

  5. FindViewByMe插件

    FindViewByMe 是一个自动生成 FindViewById 代码的 IDEA/Android Studio 插件, 支持 Activity.Fragment 和 ViewHolder 中的 f ...

  6. cocopods的安装和使用

  7. bug_ _小心android-support-v4.jar版本混乱造成的NoClassDefFoundError

    当你的项目出现以下红色提示的时候,要小心了, 因为很可能因为这个错误而导致解释不通的异常出现. Found 2 versions of android-support-v4.jar in the de ...

  8. 转--C++学习笔记(原创)

    http://www.cnblogs.com/maowang1991/p/3290321.html 以下内容为自己一年多的C++学习心得,纯原创,转载请注明源地址. 一年多的C++学习过程中,自己阅读 ...

  9. jQuery图片延迟加载插件jQuery.lazyload

      插件描述:jQuery图片延迟加载插件jQuery.lazyload,使用延迟加载在可提高网页下载速度.在某些情况下,它也能帮助减轻服务器负载. 使用方法 引用jquery和jquery.lazy ...

  10. Coding.net 代码管理快速入门

    当项目创建好了之后,我们该如何上传代码到 coding 上呢? Coding 网站使用“ Git 仓库”(类似 github )来管理代码. 其操作原理在于:利用 git 服务,将本地的项目目录下的文 ...