题干:

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 1C 3
T 3 2
C 3

Sample Output

Case #1:
-1
1
2

题目大意:

先输入n个人,然后是n个人的关系(下属 上司),然后分配任务,每一个上司分到一个任务就把任务给他的所有下属做,即他的下属的任务都是这个任务,查询一个人该时刻做的任务。

解题报告:

可以建模一下:是给定一棵树,然后一种操作是指定一个点,这个点及这个点的的子树被染色,另一种操作是指定一个点,问这个点的颜色。dfs序的实质就是:通过dfs树将这棵树放在线段上,然后用线段树优化求解。因为是单点查询所以不需要pushup,因为是区间更新并且需要叶子结点的值,所以需要pushdown。

AC代码:

#include<bits/stdc++.h>using namespace std;
const int MAX = 50000 + 5;
int n;
int cnt,id;
int head[MAX*2];
int q[MAX*4],in[MAX*4],out[MAX*4],rudu[MAX*4];
struct Edge {int to;int ne;
} e[MAX*4];//要乘2 吗?
struct TREE {int l,r;int val;int laz;
} tree[MAX * 10];void dfs(int x,int root) {q[++id] = x;in[x] = id;for(int i = head[x]; i!=-1; i = e[i].ne) {dfs(e[i].to,x);}out[x] = id;
}
void add(int u,int v) {e[++cnt].to = v;e[cnt].ne = head[u];head[u] = cnt;
}
void build(int l,int r,int cur) {tree[cur].l = l;tree[cur].r = r;tree[cur].val = -1;tree[cur].laz = 0;//懒标记初始化成0 if(l == r) {return ;}int m = (l + r) /2;build(l,m,cur*2);build(m+1,r,cur*2+1);
}
void pushup(int cur) {if(tree[cur*2].val == tree[cur*2+1].val) tree[cur].val = tree[cur*2].val;else tree[cur].val = -2;//染色问题   通解?   但是此题是单点查询所以不需要pushup?
}
void pushdown(int cur) {if(tree[cur].laz == 0) return ;tree[cur*2].val =tree[cur*2].laz = tree[cur*2+1].laz = tree[cur*2+1].val = tree[cur].laz;tree[cur].laz = 0;
}
void update(int pl,int pr, int val,int cur) {if(pl <= tree[cur].l && pr >= tree[cur].r) {tree[cur].val = val;tree[cur].laz = val;return ;}pushdown(cur);if(pl <= tree[cur*2].r) update(pl,pr,val,2*cur);if(pr >= tree[cur*2+1].l) update(pl,pr,val,2*cur+1);//pushup(cur);
}
int query(int tar,int cur) {if(tree[cur].l == tree[cur].r) {return tree[cur].val;}pushdown(cur);if(tar <= tree[cur*2].r) return query(tar,cur*2);else return query(tar,cur*2+1);//pushup(cur);
}
int main()
{
//  freopen("in.txt","r",stdin);int t;int m,iCase = 0;int u,v;char op[10];cin>>t;while(t--) {printf("Case #%d:\n",++iCase);cnt = id = 0;memset(head,-1,sizeof(head));memset(rudu,0,sizeof rudu);memset(e,0,sizeof(e));scanf("%d",&n);for(int i = 1; i<n; i++) {//读入n-1次 scanf("%d%d",&u,&v);add(v,u);rudu[u]++;}   int st =1;for(int i = 1; i<=n; i++) {if(rudu[i] == 0) {st =  i;break;}}      dfs(st,0);build(1,n,1);scanf("%d",&m);while(m--) {scanf("%s",op);if(op[0] == 'C') {scanf("%d",&u);printf("%d\n",query(in[u],1));} else {scanf("%d%d",&u,&v);//把以u为根节点的子树更改为v update(in[u],out[u],v,1);}}}return 0 ;
}
//19:15 

总结:

注意dfs序,必须从根节点开始,所以还需要找根节点!!!这里是用拓扑排序中入度的思想找的根节点。

【POJ - 3321】 Apple Tree这道题也是dfs序,但是没有找根节点,因为题目告诉你了1号节点是根节点.

本题的数组都是佛系开的,其实只是tree需要4倍,其他的都开MAX就够用。RE了一次是因为,发现dfs需要找根节点而不能直接用1号节点之后,加了rudu数组,但是rudu没有每次初始化,所以re了。。(话说不应该wa吗,搞不懂。。

【HDU - 3974】 Assign the task (dfs序 + 线段树维护 区间更新+ 单点查询)相关推荐

  1. HDU 3974 Assign the task(DFS序+线段树单点查询,区间修改)

    描述 There is a company that has N employees(numbered from 1 to N),every employee in the company has a ...

  2. 线段树 ---- D. Power Tree(离线dfs序+线段树维护树上多条路径和的技巧)

    题目链接 题目大意: 一开始给你只有一个点111的树,有qqq次询问.每次询问有两种操作 1pv1\;p\;v1pv 就是把最小的没加入的点,加入这个树,它的父亲是ppp,权值是vvv 2u2\;u2 ...

  3. 瓜瓜的时空旅行,第三次模拟赛,dfs序+线段树维护最小值

    题目描述 西瓜们生活在编号 1⋯n 的 n个平行时空中,2n−2 台时光机将这些平行时空联系在一起.一台时光机有 3个整数参数 u,v,t 表示从时空 u 可以花费 t 的时间穿梭到时空 v.为了确保 ...

  4. 【POJ - 3321】 Apple Tree(dfs序 + 线段树维护 或 dfs序 + 树状数组维护)

    题干: There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the t ...

  5. 【HDU - 5649】DZY Loves Sorting(线段树,区间更新区间查询,思维,01缩数变换,线段树分割)

    题干: DZY has a sequence a[1..n]a[1..n]. It is a permutation of integers 1∼n1∼n. Now he wants to perfo ...

  6. Wikioi 1081 线段树成段更新单点查询

    线段树练习飘逸的写法,自从自己改成这样的写法之后,线段树就没再练过,如今最终练得上了. 由于这里查询仅仅是查询了叶子结点,所以pushUp函数就用不上了,只是我没去掉之前是3ms.去掉之后反而变成4m ...

  7. Jittery Roads Gym - 100889J (虚树 + DP + dfs 序, + 线段树)

    每次给一个点集, 求每个点到其他所有点的最大距离: 会修改边权; 修改边权之后, 我们可以用 dfs 序 + 线段树维护 当前点到根节点的距离. 还可以用树状数组 + 差分思想 维护. { dfs 序 ...

  8. HDU - 3974 Assign the task (DFS建树+区间覆盖+单点查询)

    题意:一共有n名员工, n-1条关系, 每次给一个人分配任务的时候,(如果他有)给他的所有下属也分配这个任务, 下属的下属也算自己的下属, 每次查询的时候都输出这个人最新的任务(如果他有), 没有就输 ...

  9. New Year Tree(dfs序+线段树+二进制)

    题意: 给出一棵 n个节点的树,根节点为 1.每个节点上有一种颜色 ci.m次操作.操作有两种: 1 u c:将以 u为根的子树上的所有节点的颜色改为c. 2 u:询问以 u为根的子树上的所有节点的颜 ...

最新文章

  1. WebServiceHost 在ConsoleApplication中使用时添加命名空间添加不上,报错
  2. controller配对与接触配对
  3. mysql和web文件夹_Linux使用记录---自动备份MySQL和web文件夹到windows共享路径
  4. 源码编译安装gcc-5.3.0
  5. supersocket缓冲区_关于supersocker的数据传输中遇到的问题
  6. web网页简繁汉字转换
  7. aria2使用rpc下载百度云
  8. 数据分析-常用分析方法-(1)描述性分析-用Excel实现
  9. topcoder客户端
  10. 分治法 —— 循环比赛日程安排表
  11. dns劫持是什么 dns被劫持了怎么办、dns被劫持怎么解决
  12. C#--解析DataMatrix二维码
  13. 记录某大门户网站自动跳转不良网站,团队通宵排查病毒木马全过程
  14. 使用js获得26个英文字母
  15. snmpwalk之timeout问题
  16. 泽塔云:用超融合撑起软件定义数据中心的梦想
  17. 自动备份Hexo博客源文件
  18. java 邮件 客户端_JAVA编写ESMTP客户端发送邮件代码
  19. 清洁机器人APP开发制作步骤
  20. python网络爬虫之初识网络爬虫

热门文章

  1. [Leetcode][第315题][JAVA][计算右侧小于当前元素的个数][暴力][归并排序+索引数组]
  2. [剑指offer]面试题第[55-1]题[JAVA][二叉树的深度][BFS][DFS]
  3. [Leedcode][JAVA][面试题 01.07][找规律][旋转数组]
  4. Java学习笔记7-1——注解与反射
  5. jsp空白页面传html代码,echarts在HTML里测试一般,在jsp页面不显示,而且还把整个页面变成空白...
  6. 1634C. OKEA
  7. csp真题202112-1 序列查询 (100分)
  8. Django框架里的MVC思想
  9. A20 文件系统预装APK
  10. 网络编程模型综述 之 成熟的IO框架介绍