Aizu2170 Marked Ancestor(并查集)
https://vjudge.net/problem/Aizu-2170
并查集用于管理元素分组情况。
建树pre[]记录父节点,一开始只有结点1被标记了,所以find()最终得到的根都是1.
如果遇到M操作,即将树断开(很神奇的操作)。
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define INF 0x3f3f3f3f
typedef unsigned long long ll;
using namespace std;
int n, q, m, pre[];
char c;
int find(int x)
{
while(x != pre[x]){
x = pre[x];
}
return x;
}
int main()
{
IO;
while(cin >> n >> q){
if(!n&&!q) break;
ll sum=;
pre[] = ;
for(int i = ; i <= n; i++){
cin >> pre[i];
}
for(int i = ; i < q; i++){
cin >> c >> m;
if(c == 'Q')
sum += find(m);
else {
pre[m] = m;//断开
}
}
cout << sum << endl;
}
return ;
}
Aizu2170 Marked Ancestor(并查集)的更多相关文章
- AOJ 2170 Marked Ancestor[并查集][离线]
题意: 给你一颗N个节点的树,节点编号1到N.1总是节点的根.现在有两种操作: M v: 标记节点v Q v: 求出离v最近的标记的相邻节点.根节点一开始就标记好了. 现在给一系列操作,求出所有Q操作 ...
- AOJ 2170 Marked Ancestor (基础并查集)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=45522 给定一棵树的n个节点,每个节点标号在1到n之间,1是树的根节点,有如 ...
- Marked Ancestor [AOJ2170] [并查集]
题意: 有一个树,有些节点染色,每次有两种操作,第一,统计该节点到离它最近的染色父亲结点的的号码(Q),第二,为某一个节点染色(M),求第一种操作和. 输入: 输入由多个数据集组成.每个数据集都有以下 ...
- zoj3261 并查集离线处理
Connections in Galaxy War Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & ...
- HDU1198水管并查集Farm Irrigation
Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot ...
- HDOJ并查集题目 HDOJ 1213 HDOJ 1242
Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...
- LCA(最近公共祖先)离线算法Tarjan+并查集
本文来自:http://www.cnblogs.com/Findxiaoxun/p/3428516.html 写得很好,一看就懂了. 在这里就复制了一份. LCA问题: 给出一棵有根树T,对于任意两个 ...
- Codeforces Educational Codeforces Round 5 C. The Labyrinth 带权并查集
C. The Labyrinth 题目连接: http://www.codeforces.com/contest/616/problem/C Description You are given a r ...
- HDU 1213 How Many Tables (并查集)
How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...
随机推荐
- One point compactification
Theorem (One point compactification) Any locally compact space \(X\) can be embedded in another comp ...
- 分布式系统的BASE理论
一.BASE理论 eBay的架构师Dan Pritchett源于对大规模分布式系统的实践总结,在ACM上发表文章提出BASE理论,BASE理论是对CAP理论的延伸,核心思想是即使无法做到强一致性(St ...
- Python select解析
一.首先列一下,sellect.poll.epoll三者的区别 1.select a.select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监控多个文件描述符的数组,当 ...
- selenium WebDriver 对浏览器标签页的切换
关于selenium WebDriver 对浏览器标签页的切换,现在的市面上最新的浏览器,当点击一个链接打开一个新的页面都是在浏览器中打开一个标签页,而selenium只能对窗口进行切换的方法,只能操 ...
- web.xml中使用web前缀配置无法访问controller
<web:context-param> <web:param-name>contextConfigLocation</web:param-name> <web ...
- 实现判断条件中有in的判断
如果是简单的写sql,在where中写死就可以了,但是如果是不确定的参数呢,这个时候就需要一点处理方式了. 1.后台的写法 String[] operateResult=new String[]{&q ...
- Python多继承之MRO算法
MRO即Method Resolution Order 方法解析顺序,它的提出主要是为了解决Python中多继承时,当父类存在同名函数时,二义性的问题 下面先看一个例子: import inspe ...
- .net面试问答
转载自:https://www.cnblogs.com/dingfangbo/p/5768991.html .net面试问答(大汇总) 原文://http://blog.csdn.net/weny ...
- Python traceback 模块,追踪错误
Python traceback 模块,追踪错误 import traceback try: your code except: traceback.print_exc()
- scrapy 代码调试用 shell
在虚拟机里CD到你的scrapy某个项目的目录,再 1. scrapy shell + '网址'(注意引号) 2. response.xpath(' ')来提取 如: response.xpath(' ...