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. Catalyst揭秘 Day7 SQL转为RDD的具体实现

    Catalyst揭秘 Day7 SQL转为RDD的具体实现 从技术角度,越底层和硬件偶尔越高,可动弹的空间越小,而越高层,可动用的智慧是更多.Catalyst就是个高层的智慧. Catalyst已经逐 ...

  2. Mooncake (排序+贪心)

    Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types ...

  3. TextBox控件

    1.通过设置Multiline属性(bool)来控制文本框是否为多行显示 txt_Change.Location = , );//设置文本框位置 txt_Change.Multiline = true ...

  4. 云主机上搭建squid3代理服务器

    目录 目录 具体流程 修改配置文件 问题 维基整理 代理服务器 Squid (软件) SOCKS SOCKS代理 参考:http://raysmond.com/node/79 具体流程 在服务器上安装 ...

  5. ubuntu find方法

    通用格式:find pathname -options [-print -exec -ok]例子:find / -name filename 再根目录里面搜索文件名为filename的文件find / ...

  6. .Net IE10 _doPostBack 未定义

    问题描述:用.Net写的LinkButton触发后台是js报错:_doPostBack 未定义 网上资料显示这种情况是当前framework不能识别IE10版本,把该浏览器做降级处理导致JS错误,解决 ...

  7. Elasticsearch从0.90(0.90.x)到1.2(1.x)API的变化-二

    本文为官方文档的译文加个人理解.作者翻译时,elasticsearch(下面简称es)的版本为1.2.2. 请支持原创:http://www.cnblogs.com/donlianli/p/38367 ...

  8. 【linux程序设计4th】第三章1

    makefile .PHONY:clean all CC=gcc CFLAGS=-Wall -g ###replace your bin BIN=simple_write simple_read co ...

  9. REST Design Concerns

    Less Requests, More data; one of the core RESTful API design paradigms is the concept of less API re ...

  10. 1191: [HNOI2006]超级英雄Hero - BZOJ

    Description 现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问题,然后根据回答问题的多少获得不同数目的奖品或奖金.主持人问题准备了若干道题目,只有当选手正确回 ...