Huge Tree(0856)

问题描述

There are N trees in a forest. At first, each tree contains only one node as its root. And each node is marked with a number.

You're asked to do the following two operations:

A X Y, you need to link X's root to Y as a direct child. If X and Y have already been in the same tree, ignore this operation.

B X, you need to output the maximum mark in the chain from X to its root (inclusively).

输入

The first line contains an integer T, indicating the number of followed cases. (1 <= T <= 20)

For each case, the first line contains two integers N and M, indicating the number of trees at beginning, and the number of operations follows, respectively. (1 <= N, M <= 100,000)

And the following line contains N integers, which are the marks of the N trees. (0 <= Mark <= 100,000)

And the rest lines contain the operations, in format A X Y, or B X, (0 <= X, Y < N).

输出

For each 'B X' operation, output the maximum mark.

样例输入

1
5 5
5 4 2 9 1
A 1 2
A 0 4
B 4
A 1 0
B 1

样例输出

1
5

简单并查集、

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 100010 int n,m;
int val[N];
int mx[N];
int f[N]; void init()
{
for(int i=;i<=n;i++){
f[i]=i;
mx[i]=val[i];
}
}
int Find(int x)
{
if(x==f[x]) return x;
int t=f[x];
f[x]=Find(t);
mx[x]=max(mx[x],mx[t]);
return f[x];
}
void UN(int x,int y)
{
x=Find(x);
//y=Find(y);
if(x==y) return;
f[x]=y;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&val[i]);
init();
while(m--){
char op;
int a,b;
scanf(" %c",&op);
if(op=='A'){
scanf("%d%d",&a,&b);
a++;
b++;
UN(a,b);
}
else{
scanf("%d",&a);
a++;
Find(a);
printf("%d\n",mx[a]);
}
}
}
return ;
}

[swustoj 856] Huge Tree的更多相关文章

  1. [Swust OJ 856]--Huge Tree(并查集)

    题目链接:http://acm.swust.edu.cn/problem/856/ Time limit(ms): 1000 Memory limit(kb): 10000 Description T ...

  2. [swustoj 785] Divide Tree

    Divide Tree(0785) 问题描述 As we all know that we can consider a tree as a graph. Now give you a tree wi ...

  3. Inside of Jemalloc

    INSIDE OF JEMALLOCThe Algorithm and Implementation of Jemalloc author: vector03mail:   mmzsmm@163.co ...

  4. bzoj 5418

    这是拓展crt的典型应用 在你开始做之前,我一定要告诉你一件事情:虽然这道题看着和拓展crt模板很像,但他俩是有巨大的区别的!不要直接把板子改吧改吧扔上去! 题目模型:求解模线性方程组 其中p1,p2 ...

  5. SPLAY,LCT学习笔记(三)

    前两篇讲述了SPLAY模板操作,这一篇稍微介绍一下SPLAY的实际应用 (其实只有一道题,因为本蒟蒻就写了这一个) 例:bzoj 1014火星人prefix 由于本蒟蒻不会后缀数组,所以题目中给的提示 ...

  6. SPLAY,LCT学习笔记(二)

    能够看到,上一篇的代码中有一段叫做find我没有提到,感觉起来也没有什么用,那么他的存在意义是什么呢? 接下来我们来填一下这个坑 回到我们的主题:NOI 2005维修数列 我们刚刚讨论了区间翻转的操作 ...

  7. SPLAY,LCT学习笔记(一)

    写了两周数据结构,感觉要死掉了,赶紧总结一下,要不都没学明白. SPLAY专题: 例:NOI2005 维修数列 典型的SPLAY问题,而且综合了SPLAY常见的所有操作,特别适合新手入门学习(比如我这 ...

  8. bzoj 1112 poi 2008 砖块

    这滞胀题调了两天了... 好愚蠢的错误啊... 其实这道题思维比较简单,就是利用treap进行维护(有人说线段树好写,表示treap真心很模板) 就是枚举所有长度为k的区间,查出中位数,计算代价即可. ...

  9. HDU5909 Tree Cutting(树形DP + FWT)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5909 Description Byteasar has a tree T with n ve ...

随机推荐

  1. Spark Streaming揭秘 Day23 启动关闭源码图解

    Spark Streaming揭秘 Day23 启动关闭源码图解 今天主要分析一下SparkStreaming的启动和关闭过程. 从Demo程序出发,主要聚焦在两段代码: 启动代码: 关闭代码: 启动 ...

  2. JavaScript 权威指南第6版 - [阅读笔记]

    JavaScript 基础   Page 13 (1)<script> 的属性:async,charset,defer='defer',language已废,src,type   (2)n ...

  3. POJ 2151 概率DP

    主要的子问题是每一个队伍有一个做出题目的概率,求做出k个题目的概率.简单的简单的组合数DP.想清楚即可. 1: #include <iostream> 2: #include <cs ...

  4. (转载)Cocos2dx-OpenGL ES 2.0教程:你的第一个三角形(1)

    前言 在本系列教程中,我会以当下最流行的2D引擎Cocos2D-X为基础,介绍OpenGL ES 2.0的一些基本用法.本系列教程的宗旨是OpenGL扫盲,让大家在使用Cocos2D-X过程中,知其然 ...

  5. Solr + Hadoop = Big Data Love

    FROM:http://architects.dzone.com/articles/solr-hadoop-big-data-love 许多人使用Hadoop的开源项目来处理大数据的大数据集,因为它是 ...

  6. eclipse 书签

    虽然eclipse有back to和forward两个功能帮助我们阅读代码,但有时候代码一层一层看下去后,会忘了自己最初的起点. 因此想到了eclipse的书签bookmark功能. 首先,添加书签. ...

  7. python 开发webService

    最近在学习用python 开发webservice,费了半天时间把环境搭好,记录下具体过程,以备后用. 首先系统上要有python.其次要用python进行webservice开发,还需要一些库: 1 ...

  8. 相似元素存在的意义---HTML&CSS

    1.<q> 效果: 告诉浏览器这是一段短引用,让浏览器以合适的方法来显示 注: 不能直接以双引号直接代替<q>,因为有些浏览器<q>的效果不是双引号. 不要忘了移动 ...

  9. 通过PLSQL Developer导入SQL文件

    1.点击“工具” 2.选中其中的“导入表(L)”,在按下图所示操作: PLSQL Developer会自动运行相关命令,在Tables中刷新即可看到新导入的表.

  10. OSUnMapTbl[]的原理

    问题描述:     ucos任务队列中优先级获取 问题解决: uCOS-II是一个多任务的操作系统,每个任务都是一个应用程序,它有自己的寄存器和堆栈空间,即任务控制块TCB(task control ...