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(并查集)的更多相关文章

  1. AOJ 2170 Marked Ancestor[并查集][离线]

    题意: 给你一颗N个节点的树,节点编号1到N.1总是节点的根.现在有两种操作: M v: 标记节点v Q v: 求出离v最近的标记的相邻节点.根节点一开始就标记好了. 现在给一系列操作,求出所有Q操作 ...

  2. AOJ 2170 Marked Ancestor (基础并查集)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=45522 给定一棵树的n个节点,每个节点标号在1到n之间,1是树的根节点,有如 ...

  3. Marked Ancestor [AOJ2170] [并查集]

    题意: 有一个树,有些节点染色,每次有两种操作,第一,统计该节点到离它最近的染色父亲结点的的号码(Q),第二,为某一个节点染色(M),求第一种操作和. 输入: 输入由多个数据集组成.每个数据集都有以下 ...

  4. zoj3261 并查集离线处理

    Connections in Galaxy War Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & ...

  5. HDU1198水管并查集Farm Irrigation

    Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot ...

  6. HDOJ并查集题目 HDOJ 1213 HDOJ 1242

    Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...

  7. LCA(最近公共祖先)离线算法Tarjan+并查集

    本文来自:http://www.cnblogs.com/Findxiaoxun/p/3428516.html 写得很好,一看就懂了. 在这里就复制了一份. LCA问题: 给出一棵有根树T,对于任意两个 ...

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

  9. HDU 1213 How Many Tables (并查集)

    How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...

随机推荐

  1. [转]maven全局配置文件settings.xml详解

    概要 settings.xml有什么用? 如果在Eclipse中使用过Maven插件,想必会有这个经验:配置settings.xml文件的路径. Paste_Image.png settings.xm ...

  2. WinRAR 0day漏洞

    WinRAR 0day漏洞(附利用过程) 英国安全机构Mohammad Reza Espargham的漏洞实验室发现,流行压缩工具WinRAR 5.21最新版里存在一个安全漏洞,目前该漏洞还属于零日漏 ...

  3. ibatis的queyrForList和queryForMap区别

    https://blog.csdn.net/z69183787/article/details/47360825 https://blog.csdn.net/zyq527758142/article/ ...

  4. P1182 数列分段`Section II` P1316 丢瓶盖 二分答案

    题目描述 对于给定的一个长度为N的正整数数列A-iA−i,现要将其分成M(M≤N)M(M≤N)段,并要求每段连续,且每段和的最大值最小. 关于最大值最小: 例如一数列4 2 4 5 142451要分成 ...

  5. Python中GIL

    GIL(global interpreter lock)全局解释器锁 python中GIL使得同一个时刻只有一个线程在一个cpu上执行,无法将多个线程映射到多个cpu上执行,但GIL并不会一直占有,它 ...

  6. 无向图的 DFS 和 BFS实现 (以邻接表存储的图)

    #include <iostream> #include <queue> using namespace std; #define MaxVertexNum 10 typede ...

  7. html-div+span-4

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 前端之HTML(一)

    最近学到前端的一些知识,感觉挺有意思的.总结一下常用的知识.这些只是最简单的东西. 一 HTML,CSS,JS的关系 一个基本的网站包含很多网页,一个网页又有html,css,js组成. html 是 ...

  9. c++简单学习

    在c++中我们很容易遇到字符串的分割处理问题,这种问题通常比较容易,但由于我比较菜,花费了一定时间去思考一个和字符串相关的题,该题的大概思路是利用取模运算后,将得到的单个字符进行分析,主要考察到了字符 ...

  10. 向cmd中添加字体的方法

    首先下载字体到C:\Windows\Fonts中,然后参考 http://blog.csdn.net/bbirdsky/article/details/38495661 中所讲的方法进行添加.