[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 ...
随机推荐
- Visual C++2010开发权威指南 中文高清PDF - VC.NET
第一部分 Visual C++ 2010开发与新特性第1章 Visual C++ 2010开发环境简介 11.1 Visual C++ 2010简介 11.2 Visual C++ 2010下 ...
- Python: tkinter实例改名小工具
#!/usr/bin/env python #coding=utf-8 # # 版权所有 2014 yao_yu (http://blog.csdn.net/yao_yu_126) # 本代码以MIT ...
- linux eval命令
eval 功能说明:重新运算求出参数的内容.语 法:eval [参数]补充说明:eval可读取一连串的参数,然后再依参数本身的特性来执行.参 数:参数不限数目,彼此之间用分号分开. 1.eval命令将 ...
- 零基础学Python 3之环境准备
一. 配置python 3 环境 1. Python 3 下载 64位 https://www.python.org/ftp/python/3.4.2/python-3.4.2.amd64.msi 3 ...
- (转)为什么adrl r2,mem_cfg_val这里不用ldr r2,=mem_cfg_val
网址:http://blog.csdn.net/glorin/article/details/6327083 memsetup:mov r1, #MEM_CTL_BASEadrl r2,mem_cfg ...
- CentOS安装crontab及使用方法
crontab命令常见于Unix和类Unix的操作系统之中,用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于 “crontab”文件中,以供之后读取和执行.通常,crontab储 ...
- PHP 5.3连接sql server 2008 R2
我的机器为: xp sp3 sql server 2008 developer apache 2.2.2 php 5.3 从5.3开始,php就不再提供mssql.dll了,所以要php连接sql ...
- hdu 4658 Integer Partition
五角数定理!!可以参考这个http://www.cnblogs.com/xin-hua/p/3242428.html 代码如下: #include<iostream> #include& ...
- hdu 3929 Big Coefficients 容斥原理
看懂题目,很容易想到容斥原理. 刚开始我用的是二进制表示法实现容斥原理,但是一直超时.后来改为dfs就过了…… 代码如下: #include<iostream> #include<s ...
- MySQL提示:The server quit without updating PID file问题的解决办法
错误如下: [root@snsgou mysql]# service mysql restartMySQL server PID file could not be found![失败]Startin ...