码迷,mamicode.com
首页 > 其他好文 > 详细

Marked Ancestor (AOJ 2170 并查集)

时间:2015-03-22 09:21:12      阅读:423      评论:0      收藏:0      [点我收藏+]

标签:marked ancestor   aoj 2170   并查集   

Marked Ancestor

Time Limit : 8 sec, Memory Limit : 65536 KB

Problem 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 3
1
1
2
3
3
Q 5
M 3
Q 5
0 0

Output for the Sample Input

4
题意:给出包含n个节点的树,初始时节点1的父节点为1并被标记,再给出其他n-1个节点的父节点,Q次询问,M x表示把节点x标记;Q x表示找出离x最近的被标记的父节点的编号,最后输出所有标号之和。
思路:基础的并查集了,只不过这里要注意的是:寻找父节点时不要压缩路径。还要使用long long 。
代码:
#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;
}




Marked Ancestor (AOJ 2170 并查集)

标签:marked ancestor   aoj 2170   并查集   

原文地址:http://blog.csdn.net/u014422052/article/details/44517239

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!