Marked Ancestor (AOJ 2170 并查集)
来源:互联网 发布:小心眼表现 知乎 编辑:程序博客网 时间:2024/05/16 00:43
Marked Ancestor
Time Limit : 8 sec, Memory Limit : 65536 KBProblem F: Marked Ancestor
You are given a tree T that consists of N nodes. Each node is numbered from 1 to N, and node 1 is always the root node of T. Consider the following two operations on T:
- M v: (Mark) Mark node v.
- Q v: (Query) Print the index of the nearest marked ancestor of node v which is nearest to it. Initially, only the root node is marked.
Your job is to write a program that performs a sequence of these operations on a given tree and calculates the value that each Q operation will print. To avoid too large output file, your program is requested to print the sum of the outputs of all query operations. Note that the judges confirmed that it is possible to calculate every output of query operations in a given sequence.
Input
The input consists of multiple datasets. Each dataset has the following format:
The first line of the input contains two integers N and Q, which denotes the number of nodes in the tree Tand the number of operations, respectively. These numbers meet the following conditions: 1 ≤ N ≤ 100000 and 1 ≤ Q ≤ 100000.
The following N - 1 lines describe the configuration of the tree T. Each line contains a single integer pi (i = 2, ... , N), which represents the index of the parent of i-th node.
The next Q lines contain operations in order. Each operation is formatted as "M v" or "Q v", where v is the index of a node.
The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print the sum of the outputs of all query operations in one line.
Sample Input
6 311233Q 5M 3Q 50 0
Output for the Sample Input
4
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 100005#define MAXN 2005#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE(i,a,b) for(i = a; i <= b; i++)#define FREE(i,a,b) for(i = a; i >= b; i--)#define FRL(i,a,b) for(i = a; i < b; i++)#define FRLL(i,a,b) for(i = a; i > b; i--)#define mem(t, v) memset ((t) , v, sizeof(t))#define sf(n) scanf("%d", &n)#define sff(a,b) scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pf printf#define DBG pf("Hi\n")typedef long long ll;using namespace std;ll father[maxn];int n,q;ll find_father(ll x){ if (x==father[x]) return father[x]; return find_father(father[x]);}int main(){ int i,j; ll x; char s[2]; while (sff(n,q)) { if (n==0&&q==0) break; father[1]=1; FRE(i,2,n) { sf(x); father[i]=x; } ll ans=0; while (q--) { scanf("%s%lld",s,&x); if (s[0]=='M') father[x]=x; else ans+=find_father(x); } pf("%lld\n",ans); } return 0;}
- AOJ 2170 Marked Ancestor (并查集)
- Marked Ancestor (AOJ 2170 并查集)
- [AOJ 2170]Marked Ancestor[并查集][离线][路径压缩]or[线段树]
- Aizu 2170 Marked Ancestor【并查集】
- AOJ2170 Marked Ancestor 并查集
- 并查集 AOJ 2170
- 并查集aoj夺宝奇兵
- Aizu 2170 Marked Ancestor
- Marked Ancestor
- 简单的并查集aoj连通图
- Aoj2170 Marked Ancestor
- 【解题报告】aoj夺宝奇兵——并查集&&某魔法的数据链
- aoj2170 Marked Ancestor 简单题
- 并查集(集并查)
- 并查集(含关系并查集)
- 【并查集】食物链(关系并查集)
- 【并查集】并查集详解(转)
- 分组并查集(种类并查集)
- Android中的设计模式
- boost thread
- 二叉树小结
- BZOJ-3171-循环格-TJOI2013-费用流
- boost 字符串,json
- Marked Ancestor (AOJ 2170 并查集)
- 杭电 HDU 1038 Biker's Trip Odometer
- boost 指针
- MySQL的JDBC驱动源码解析
- mfc中关于画图操作而导致ASSERT(m_hObject == NULL);中断问题
- HNU13247Connected Caves (拓扑)
- Java中getResourceAsStream的用法
- jdk的安装与配置
- 循序渐进做项目系列(1):最简单的C/S程序——让服务器来做加法