[swustoj 856] Huge Tree
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的更多相关文章
- [Swust OJ 856]--Huge Tree(并查集)
题目链接:http://acm.swust.edu.cn/problem/856/ Time limit(ms): 1000 Memory limit(kb): 10000 Description T ...
- [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 ...
- Inside of Jemalloc
INSIDE OF JEMALLOCThe Algorithm and Implementation of Jemalloc author: vector03mail: mmzsmm@163.co ...
- bzoj 5418
这是拓展crt的典型应用 在你开始做之前,我一定要告诉你一件事情:虽然这道题看着和拓展crt模板很像,但他俩是有巨大的区别的!不要直接把板子改吧改吧扔上去! 题目模型:求解模线性方程组 其中p1,p2 ...
- SPLAY,LCT学习笔记(三)
前两篇讲述了SPLAY模板操作,这一篇稍微介绍一下SPLAY的实际应用 (其实只有一道题,因为本蒟蒻就写了这一个) 例:bzoj 1014火星人prefix 由于本蒟蒻不会后缀数组,所以题目中给的提示 ...
- SPLAY,LCT学习笔记(二)
能够看到,上一篇的代码中有一段叫做find我没有提到,感觉起来也没有什么用,那么他的存在意义是什么呢? 接下来我们来填一下这个坑 回到我们的主题:NOI 2005维修数列 我们刚刚讨论了区间翻转的操作 ...
- SPLAY,LCT学习笔记(一)
写了两周数据结构,感觉要死掉了,赶紧总结一下,要不都没学明白. SPLAY专题: 例:NOI2005 维修数列 典型的SPLAY问题,而且综合了SPLAY常见的所有操作,特别适合新手入门学习(比如我这 ...
- bzoj 1112 poi 2008 砖块
这滞胀题调了两天了... 好愚蠢的错误啊... 其实这道题思维比较简单,就是利用treap进行维护(有人说线段树好写,表示treap真心很模板) 就是枚举所有长度为k的区间,查出中位数,计算代价即可. ...
- HDU5909 Tree Cutting(树形DP + FWT)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5909 Description Byteasar has a tree T with n ve ...
随机推荐
- 小课堂Week8 例外处理设计的逆袭Part1
小课堂Week8 例外处理设计的逆袭Part1 今天和大家讲一本书,书名是<例外处理设计的逆袭>. 为什么想讲这本书,是因为,例外处理在程序代码中到处存在,但是这些到底该如何写好,总觉得有 ...
- 以a为变量名,给出下列描述的定义
a)一个整型数(An integer) b) 一个指向整型数的指针(A pointer to an integer) c) 一个指向指针的的指针,它指向的指针是指向一个整型数(A pointer to ...
- Java创建Oracle数据库表
我们通常只用java执行DML(即:insert, update, delete, select)操作,很少用来执行DDL(create, drop, alert)操作.今天试了下如何用java来创建 ...
- Import和SQL*Loader这2个工具的异同
问:请讲述Import和SQL*Loader这2个工具的异同? 解答: 相同点:这两个ORACLE工具都是用来将数据导入数据库的. 区别是: IMPORT工具只能处理由另一个ORACLE工具EXPOR ...
- mybatis显示sql语句 log4j.properties配置文件
log4j.properties配置如下: 将ibatis log4j运行级别调到DEBUG可以在控制台打印出ibatis运行的sql语句,方便调试: ### 设置Logger输出级别和输出目的地 # ...
- Linux必学的60个命令
inux必学的60个命令Linux提供了大量的命令,利用它可以有效地完成大量的工作,如磁盘操作.文件存取.目录操作.进程管理.文件权限设定等.所以,在Linux系统上工作离不开使用系统提供的命令.要想 ...
- log4j日志输出到web项目指定文件夹
感谢 eric2500 的这篇文章:http://www.cxyclub.cn/n/27860/ 摘要:尝试将log4j的文件日志输出到web工程制定目录,遇到了很多问题,最终在eric2500的指导 ...
- 可以继承的C++ Singleton基类
单例模式(Singleton Pattern)是设计模式中的一种,它用来保证系统中最多只能存在一个它的实例,其做法是由类自身来创建和持有它的对象实例,把对实例的创建权和管理权都控制在自己手中,以便控制 ...
- POJ2527+多项式除法
模拟一遍即可. 注意一些特殊情况,见代码. #include<stdio.h> #include<stdlib.h> #include<math.h> #inclu ...
- POJ3283+字典树
简单的字典树 /* 字典树 构造字典树.注意初始化! */ #include<stdio.h> #include<string.h> #include<stdlib.h& ...