Aizu 2170 Marked Ancestor

题意:出一颗树,有两种操作:
1. mark  u  标记结点u
2.query  u  询问离u最近的且被标记的祖先结点是哪个
让你输出所有询问的和。

思路:数据量太小,直接暴力dfs就可以了

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<vector>
#include<set>
#include<queue>
#include<string>
#include<algorithm>
#define MAXSIZE 100005
#define LL long long
using namespace std;

int vis[MAXSIZE],pre[MAXSIZE];

LL dfs(int p)
{
    if(vis[p])
        return p;
    return dfs(pre[p]);
}

int main()
{
    int n,m,num;
    LL sum;
    char op[5];
    while(scanf("%d%d",&n,&m),n+m)
    {
        sum = 0;
        memset(vis,0,sizeof(vis));
        vis[1] = 1;
        for(int i=2;i<=n;i++)
        {
            scanf("%d",&num);
            pre[i] = num;
        }

        while(m--)
        {
            scanf("%s%d",op,&num);
            if(op[0] == ‘Q‘)
            {
                sum += dfs(num);
            }

            else
            {
                vis[num] = 1;
            }
        }

        printf("%lld\n",sum);
    }
    return 0;
}

时间: 2024-09-28 06:48:40

Aizu 2170 Marked Ancestor的相关文章

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. 还有把编号为

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

挑战程序设计竞赛 2.4 加工并存储数据的数据结构

[Summarize] 1.求满足条件的情况下最大化中位数可以枚举中位数再验证条件 2.对于种类并查集,可以利用拆点的方式,用x-A表示x属于A类,将种类归属关系作为节点进行运算 POJ 3614:Sunscreen /* 每个奶牛各自能够忍受的阳光强度有一个最小值和一个最大值 防晒霜的作用是让阳光照在身上的阳光强度固定为某个值 每瓶防晒霜给出固定的阳光量和防晒霜数量 每头奶牛只能用一瓶防晒霜 问最多能晒太阳的奶牛数量 */ #include <cstdio> #include <que

235. Lowest Common Ancestor of a Binary Search Tree

1. 问题描述 Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T th

Lowest Common Ancestor of a Binary Search Tree

1. Title 235. Lowest Common Ancestor of a Binary Search Tree 2. Http address https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ 3. The question Given a binary search tree (BST), find the lowest common ancestor (LCA) of two

Lowest Common Ancestor of a Binary Tree

题目连接 https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Common Ancestor of a Binary Tree Description Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on

【mysql】 is marked as crashed and should be repaired

-备份数据库时报错 [ERROR] /usr/sbin/mysqld: Table './daifans/wp_options' is marked as crashed and should be repaired - 在网上查了文档,说是表损坏,所以 WordPress 的文章都显示不出来,需要修复,修复前需要备份 我备份时也出错了,说是损坏的那个表备份不了的意思,我就用排除废表备份其他的 # mysqldump -uroot -p  dbname --ignore-table=dbname

数据结构与算法(1)支线任务4——Lowest Common Ancestor of a Binary Tree

题目如下:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is def

LeetCode (236):Lowest Common Ancestor of a Binary Search Tree

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has