HDU3974(树的遍历)

Assign the task



Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

There is a company that has N employees(numbered from 1 to N),every employee in the company has a immediate boss (except for the leader of whole company).If you are the immediate boss of someone,that person is your subordinate, and all his subordinates are your subordinates as well. If you are nobody‘s boss, then you have no subordinates,the employee who has no immediate boss is the leader of whole company.So it means the N employees form a tree.

The company usually assigns some tasks to some employees to finish.When a task is assigned to someone,He/She will assigned it to all his/her subordinates.In other words,the person and all his/her subordinates received a task in the same time. Furthermore,whenever a employee received a task,he/she will stop the current task(if he/she has) and start the new one.

Write a program that will help in figuring out some employee’s current task after the company assign some tasks to some employee.

Input

The first line contains a single positive integer T( T <= 10 ), indicates the number of test cases.

For each test case:

The first line contains an integer N (N ≤ 50,000) , which is the number of the employees.

The following N - 1 lines each contain two integers u and v, which means the employee v is the immediate boss of employee u(1<=u,v<=N).

The next line contains an integer M (M ≤ 50,000).

The following M lines each contain a message which is either

"C x" which means an inquiry for the current task of employee x

or

"T x y"which means the company assign task y to employee x.

(1<=x<=N,0<=y<=10^9)

Output

For each test case, print the test case number (beginning with 1) in the first line and then for every inquiry, output the correspond answer per line.

Sample Input

1

5

4 3

3 2

1 3

5 2

5

C 3

T 2 1

C 3

T 3 2

C 3

Sample Output

Case #1:

-1

1

2

线段树?我没看出来。根据我的解法,还是分到图论吧。

#include"cstdio"
#include"vector"
#include"cstring"
using namespace std;
const int MAXN=50005;
int task[MAXN];
vector<int> boss[MAXN];
void assign(int em,int tas)
{
    task[em]=tas;
    for(int i=0;i<boss[em].size();i++)
    {
        int sub=boss[em][i];
        task[sub]=tas;
        assign(sub,tas);
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++)
    {
        memset(task,-1,sizeof(task));
        int n;
        scanf("%d",&n);
        for(int i=0;i<=n;i++)
        {
            boss[i].clear();
        }
        for(int i=1;i<=n-1;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            boss[v].push_back(u);
        }
        printf("Case #%d:\n",cas);
        int m;
        scanf("%d",&m);
        while(m--)
        {
            scanf("%*c");
            char op;
            scanf("%c",&op);
            if(op==‘C‘)
            {
                int em;
                scanf("%d",&em);
                printf("%d\n",task[em]);
            }
            else
            {
                int x,y;
                scanf("%d%d",&x,&y);
                assign(x,y);
            }
        }
    }
}
时间: 2024-08-24 23:49:14

HDU3974(树的遍历)的相关文章

pat L2-006. 树的遍历

L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(<=30),是二叉树中结点的个数.第二行给出其后序遍历序列.第三行给出其中序遍历序列.数字间以空格分隔. 输出格式: 在一行中输出该树的层序遍历的序列.数字间以1个空格分隔,行首尾不得有多余空格. 输入样例: 7 2

一步两步学算法之树的遍历 非递归实现

递归的程序其实我觉得可读性较高  但是执行效率低下 为了做一道PAT的题 去理解了下非递归实现树的遍历 用一个栈来实现 先序遍历 先访问节点 再把节点push进栈 再访问 再push 直到next=NULL 然后pop出一个节点 也就是弹出一个节点 访问它的右边 再弹出 在访问 中序遍历 把左边节点全部push进栈 然后弹出 访问中间 再访问右边  再弹出 一直循环 后序遍历 比较难理解  要入两次栈才能访问 先左边全部入栈  栈顶是左边的元素 此书不能访问 因为右边还没入栈 下面给出先序和后序

树及遍历

节点深度:从根到节点的路径长度,d(root)=0 节点高度:从节点到树叶的最长路径的长,h(leaf)=0 树高为根高,树的深度=树的高度 树的遍历: 递归的前.中.后序还是蛮简单的: 1 //树的遍历 2 void preorder_recursive(PtrToBiNode T){ //二叉树递归先序遍历 3 if (T){ //这句不要忘记 4 printf("%d ", T->Element); 5 preorder_recursive(T->left); 6 p

leetcode404-----简单的树的遍历

Find the sum of all left leaves in a given binary tree. Example: 3 / 9 20 / 15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. 题目给出的意思很简单.就只是单纯的树的遍历而已.意思是计算出所有左叶子节点的值的和. 我采用递归的方式表示我的遍历顺序,其实主要的是要理解题目的意思,这里

树的遍历 | Tree Traversal

树的遍历方式总体上有两种:DFS和BFS: 其中DFS包含了前序.中序和后序遍历,而BFS则为层次遍历. DFS的实现方式: (1) 递归: (2) 非递归,使用辅助栈: 递归程序 public class Recursion { public void preorderRec(TreeNode root) { if (root == null) { return; } System.out.println(root.val); // visit the node preorderRec(roo

Codeforces 29D Ant on the Tree 树的遍历 dfs序

题目链接:点击打开链接 题意: 给定n个节点的树 1为根 则此时叶子节点已经确定 最后一行给出叶子节点的顺序 目标: 遍历树并输出路径,要求遍历叶子节点时按照给定叶子节点的先后顺序访问. 思路: 给每个节点加一个优先级. 把最后一个叶子节点到父节点的路径上的点优先级改为1 把倒数第二个叶子节点到父节点的路径上的点优先级改为2 如此每个点就有一个优先级,每个访问儿子节点时先访问优先级大的即可 对于无解的判断:得到的欧拉序列不满足输入的叶子节点顺序即是无解. #include <cstdio> #

那些妖术——树的遍历

这个方法有点邪门,和大家在课堂上学的有点不一样,所以blog的名字取得有点邪乎. 一般的程序员应聘技术类的笔试都会有一道题目,那就是树的遍历(前序遍历,中序遍历和后续遍历).这里教大家玩点新鲜的, 可能和平时大家学的有点不一样.但是绝对是在考场上解决问题的神器,因为一个字快,可以帮你节省时间做其他的题目. 喜欢的小伙伴记得点赞啊.(*^__^*) 一.中序遍历 这个是最简单的,什么是中序遍历,那就是把我们的树压扁了就可以得到我们的中序遍历,所以中序遍历就是DBGEHACIJF. 什么是把树压扁了

分针网——每日分享:MySQL实现树的遍历

更多文章:www.f-z.cn 经常在一个表中有父子关系的两个字段,比如empno与manager,这种结构中需要用到树的遍历.在Oracle 中可以使用connect by简单解决问题,参见http://blog.csdn.net/wzy0623/archive/2007/06/18/1656345.aspx,但MySQL 5.1中还不支持(据说已纳入to do中),要自己写过程或函数来实现. 一.建立测试表和数据 [c-sharp] view plain copy DROP TABLE IF

如果Google面试让你用python写一个树的遍历程序

前几天忽然对python很感兴趣,学了几天也感觉它非常的简洁实用.打破了我这么长时间对java C# C 和vb的审美疲劳,让我眼前一亮."就像读英文一样简单"这句话评价python说的很合理. 我对python的好感很大部分是因为听说google很多程序用python,而且在google app engine里面和支持python.如果你去google面试或者笔试,很可能就会考到这个题:用python实现树的遍历. 自己试着写了一下,不过毕竟是菜鸟,有问题请多多指教. 运行效果如下: