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;
}
时间: 2024-07-28 23:03:44

Marked Ancestor (AOJ 2170 并查集)的相关文章

AOJ 2170 Marked Ancestor (基础并查集)

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=45522 给定一棵树的n个节点,每个节点标号在1到n之间,1是树的根节点,有如下两种操作: M v :把编号为v的节点标记. Q v :查询与v节点距离最近的被标记的点的编号.最初只有根节点被标记. 输入n-1个数表示,每一个数是当前第i个数的父节点,输出对于每个查询得到标号的总和. 并查集与树的结合,在输入的时候就可以根据父节点的关系建立并查集,1的父亲是1. 还有把编号为

Farm Irrigation_深搜_并查集

Farm Irrigation TimeLimit: 2000/1000 MS (Java/Others)  MemoryLimit: 65536/32768 K (Java/Others) 64-bit integer IO format:%I64d Problem Description Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot of

【简单并查集】Farm Irrigation

Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 6   Accepted Submission(s) : 3 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Benny has a spacious fa

HDU 1213 How Many Tables (并查集)

How Many Tables Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1213 Appoint description:  System Crawler  (2015-05-25) Description Today is Ignatius' birthday. He invites a lot of friends. Now

Hdoj 1213 How Many Tables 【并查集】

How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16590 Accepted Submission(s): 8117 Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner tim

hdu 1198 Farm Irrigation (搜索或并查集)

Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5818    Accepted Submission(s): 2521 Problem Description Benny has a spacious farm land to irrigate. The farm land is a rectangle

杭电1213题解:一道最基础的并查集问题

Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want

ZOJ 3261 - Connections in Galaxy War ,并查集删边

In order to strengthen the defense ability, many stars in galaxy allied together and built many bidirectional tunnels to exchange messages. However, when the Galaxy War began, some tunnels were destroyed by the monsters from another dimension. Then m

HDU1198水管并查集Farm Irrigation

Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot of samll squares. Water pipes are placed in these squares. Different square has a different type of pipe. There are 11 types of pipes, which is marked