题目链接:http://acm.swust.edu.cn/problem/856/

Time limit(ms): 1000        Memory limit(kb): 10000

Description

  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).

Input

  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).

Output

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

Sample Input

  

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

1

5

 
 

题目大意:就是一个数组(下标从零开始),有对应的A,B操作,A a,b,是把a所在集合归并到b上,

若某一个集合已合并不进行操作,然B a,就是查询a集合中的最大值。

解题思路:运用并查集就是,注意a集合到b所在集合,并查集合并区分一下就可以了

代码如下:

 #include <stdio.h>
int n, m, maxn, t, f[], cur[]; void init(){
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++){
scanf("%d", &cur[i]);
f[i] = i;
}
} int findset(int x){
maxn = cur[x];
if (f[x] == x) return x;
int y = findset(f[x]);
if (maxn > cur[x]) cur[x] = maxn;
else maxn = cur[x];
return f[x] = y;
} void mergy(){
int i, x, y;
char k[];
for (i = ; i < m; i++){
scanf("%s", k);
if (k[] == 'A'){
scanf("%d%d", &x, &y);
int a = findset(x), b = findset(y);
if (a != b) f[a] = y;//注意和传统并查集的区别
}
else{
scanf("%d", &x);
findset(x);
printf("%d\n", cur[x]);
}
}
} int main(){
scanf("%d", &t);
while (t--){
init();
mergy();
}
return ;
}

[Swust OJ 856]--Huge Tree(并查集)的更多相关文章

  1. [swustoj 856] Huge Tree

    Huge Tree(0856) 问题描述 There are N trees in a forest. At first, each tree contains only one node as it ...

  2. 杭电OJ——1198 Farm Irrigation (并查集)

    畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...

  3. Hdu.1325.Is It A Tree?(并查集)

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  4. Is It A Tree?(并查集)

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26002   Accepted: 8879 De ...

  5. CF109 C. Lucky Tree 并查集

    Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...

  6. HDU 5606 tree 并查集

    tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ans​i​​=size[findset(i)],size表示每个并 ...

  7. Codeforces Round #363 (Div. 2)D. Fix a Tree(并查集)

    D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  8. Is It A Tree?(并查集)(dfs也可以解决)

    Is It A Tree? Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submi ...

  9. tree(并查集)

    tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

随机推荐

  1. SSL握手流程

    一.SSL是什么? 安全套接字(SSL)协议是Web浏览器和Web服务器之间安全交换信息的协议. SSL介于应用层和TCP层之间,应用层数据不再直接传递给传输层,而是传递给SSL层,SSL层对从应用层 ...

  2. C# Regex ignoring non-capturing group

    E.g I want match the keword "3398" after "red" from the string "This is red ...

  3. mapper分页排序指定字段查询模板

    StudentQuery: package Student; import java.util.ArrayList; import java.util.List; public class Stude ...

  4. java.lang.UnsupportedClassVersionError: Bad version number in .class file 解决方案

    在Myeclipse中运行小应用程序时出现如下异常的解决办法 java.lang.UnsupportedClassVersionError: Bad version number in .class ...

  5. Windows Azure 即将更名

     今天我们宣布自2014 年4 月3 日起,WindowsAzure 将更名为Microsoft Azure.这一更名反映了Microsoft 的战略,并显示了Microsoft 专注于将Azur ...

  6. Ubuntu系统下在Eclipse中使用真实手机运行调试Android应用

    本文是在Ubuntu下已搭建好JRE+Eclipse+AndroidSDK的环境中进行的. 如果从未进行过如下的操作的话,在运行Android应用时,在设备选择器那里你的手机会显示为一大串" ...

  7. Java对象序列化与反序列化一 JSON

    Java对象序列化与反序列化一 JSON 1. 依赖库 jackson-all-1.6.1.jar 2. 代码 public class Student {    private String nam ...

  8. 将从数据库中获取的数据 ,以HTML表格的形式显示

    1.HTML页面 <body> <form id="form1" runat="server"> <div id="di ...

  9. if语句之猜拳

    用计算机来生成随机数: Random rand = new Random();//做一个随机生成器,Random();后面的括号里面可以放一个随机生成器种子,这个种子只能为整数(int)int n = ...

  10. [译]Stairway to Integration Services Level 11 - 日志配置

    介绍 在前一个章节我们讨论了事先行为,分享了如何操作默认的行为和时间冒泡,并且介绍了父子模型. 本文中,我们会配置SSIS日志. 进行简单及高级日志配置,存储,和检索的实验.并且生成自定义日志信息. ...