[Swust OJ 856]--Huge Tree(并查集)
题目链接:http://acm.swust.edu.cn/problem/856/
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
|
|
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(并查集)的更多相关文章
- [swustoj 856] Huge Tree
Huge Tree(0856) 问题描述 There are N trees in a forest. At first, each tree contains only one node as it ...
- 杭电OJ——1198 Farm Irrigation (并查集)
畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...
- 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 ...
- Is It A Tree?(并查集)
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26002 Accepted: 8879 De ...
- CF109 C. Lucky Tree 并查集
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...
- HDU 5606 tree 并查集
tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ansi=size[findset(i)],size表示每个并 ...
- 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 ...
- Is It A Tree?(并查集)(dfs也可以解决)
Is It A Tree? Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submi ...
- tree(并查集)
tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
随机推荐
- js表格排序 & 去除字符串空格
// a:列数 bool:排序升序判断参数 true false Str:支持多列 function newUnitSort(a, bool, str) { var oTable = document ...
- 嵌入式C开发人员的最好的0x10道笔试题
嵌入式C开发人员的最好的0x10道笔试题 2006-11-22 15:53 约定: 1) 下面的测试题中,认为所有必须的头文件都已经正确的包含了 2)数据类型 char 一个字节 1 byte int ...
- RFID标签天线的三种制作方法
在RFID标签中,天线层是主要的功能层,其目标是传输最大的能量进出标签芯片.RFID天线是按照射频识别所要求的功能而设计的电子线路,将导电银浆或导电碳浆网印在PVC.PC或PET上,再与面层.保护层和 ...
- Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
2016-07-18 16:08:20 [main:53] - [WARN] Exception encountered during context initialization - cancell ...
- Objective-c 数据类型
这里列出Objective-c中独有数据类型: 一.字符串 在Objective-c中,字符串常量是由@和一对从引号括起的字符串序列.比如:@"China".@"obje ...
- 法方总经理用的笔记本电脑&一体机拆开图。
键盘上有三个字符,
- iPhone 真机调试应用程序
原文:http://blog.sina.com.cn/s/blog_68e753f70100r3w5.html 真机调试iphone应用程序 1.真机调试流程概述 1) 真机调试应用程序, ...
- 解决Jedis数据读取乱码问题
现象 同一套代码,同一个数据源,不同的操作系统,在OSX上数据提取编码正常,而Ubuntu上拉取数据乱码,数据拉取代码如下. @Override public List<String> m ...
- 浅谈JDBC(一)
一.JDBC技术引言 1.什么是JDBC技术 提供了一套接口规范,利用java代码进行数据库操作. 2.JDBC技术的核心思想 对于程序员来说,代码访问数据库分为三个步骤:1.通过数据库的账号密码.2 ...
- web测试 结果存储类型为“Database”,但尚未指定结果储存库连接字符串
vs2010 Ultimate版带有web测试功能,可以对网站的性能以及负载进行测试. 在进行负载测试时提示“异常 LoadTestConnectStringMissingException 1 Lo ...