Problem 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

思路:

在dfs序上建立线段树 可以把树上问题转化为区间问题 我们可以发现一个点的dfs序之间的数就是他的儿子节点 所以问题转化为 区间跟新+单点查询的基本问题

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int N = 5e4+7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
typedef long long ll;
const ll mod = 1e9+7;
struct edge{
int next,v;
};
edge e[N<<1];
int head[N],cnt,tot,L[N],flag[N],R[N];
void init(){
cnt=0;
tot=0;
memset(head,0,sizeof(head));
memset(flag,0,sizeof(flag));
}
void add(int u,int v){
e[++cnt]=edge{head[u],v};
head[u]=cnt;
}
void dfs(int u){
L[u]=++tot;
for(int i=head[u];i;i=e[i].next){
int v=e[i].v;
dfs(v);
}
R[u]=++tot;
}
struct tree{
int l,r,v,lazy;
}t[N<<2];
void build(int p,int l,int r){
t[p].l=l; t[p].r=r; t[p].v=-1; t[p].lazy=0;
if(l==r) return ;
int mid=(l+r)>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
}
void pushdown(int p){
if(t[p].lazy){
t[p<<1].v=t[p].lazy;
t[p<<1|1].v=t[p].lazy;
t[p<<1].lazy=t[p].lazy;
t[p<<1|1].lazy=t[p].lazy;
t[p].lazy=0;
}
}
void update(int p,int l,int r,int v){
if(l<=t[p].l&&t[p].r<=r){
t[p].lazy=v;
t[p].v=v;
return ;
}
pushdown(p);
int mid=(t[p].l+t[p].r)>>1;
if(l<=mid) update(p<<1,l,r,v);
if(r>mid) update(p<<1|1,l,r,v);
}
int query(int p,int x){
if(t[p].l==t[p].r&&t[p].l==x){
return t[p].v;
}
pushdown(p);
int mid=(t[p].l+t[p].r)>>1;
int res;
if(x<=mid) res=query(p<<1,x);
else res=query(p<<1|1,x);
return res;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t; cin>>t;
int w=0;
while(t--){
cout<<"Case #"<<++w<<":"<<endl;
init();
int n,m; cin>>n;
for(int i=1;i<n;i++){
int u,v; cin>>u>>v;
add(v,u);
flag[u]=1;
}
int s;
for(int i=1;i<=n;i++)
if(!flag[i]){
s=i; break;
}
dfs(s);
build(1,1,n<<1);
cin>>m;
for(int i=1;i<=m;i++){
char op; cin>>op;
if(op=='C'){
int x; cin>>x;
cout<<query(1,L[x])<<endl;
}else{
int x,y; cin>>x>>y;
update(1,L[x],R[x],y);
}
}
}
}

hdu 3974 Assign the task(dfs序上线段树)的更多相关文章

  1. HDU 3974 Assign the task (DFS序 + 线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 给你T组数据,n个节点,n-1对关系,右边的是左边的父节点,所有的值初始化为-1,然后给你q个操 ...

  2. 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 ...

  3. HDU 3974 Assign the task (DFS+线段树)

    题意:给定一棵树的公司职员管理图,有两种操作, 第一种是 T x y,把 x 及员工都变成 y, 第二种是 C x 询问 x 当前的数. 析:先把该树用dfs遍历,形成一个序列,然后再用线段树进行维护 ...

  4. HDU 3974 Assign the task(dfs建树+线段树)

    题目大意:公司里有一些员工及对应的上级,给出一些员工的关系,分配给某员工任务后,其和其所有下属都会进行这项任务.输入T表示分配新的任务, 输入C表示查询某员工的任务.本题的难度在于建树,一开始百思不得 ...

  5. bzoj3306: 树(dfs序+倍增+线段树)

    比较傻逼的一道题... 显然求子树最小值就是求出dfs序用线段树维护嘛 换根的时候树的形态不会改变,所以我们可以根据相对于根的位置分类讨论. 如果询问的x是根就直接输出整棵树的最小值. 如果询问的x是 ...

  6. HDU 3974 Assign the task(DFS序)题解

    题意:给出一棵树,改变树的一个节点的值,那么该节点及所有子节点都变为这个值.给出m个询问. 思路:DFS序,将树改为线性结构,用线段树维护.start[ ]记录每个节点的编号,End[ ]为该节点的最 ...

  7. [Assign the task][dfs序+线段树]

    http://acm.hdu.edu.cn/showproblem.php?pid=3974 Assign the task Time Limit: 15000/5000 MS (Java/Other ...

  8. bzoj2819 DFS序 + LCA + 线段树

    https://www.lydsy.com/JudgeOnline/problem.php?id=2819 题意:树上单点修改及区间异或和查询. 思维难度不高,但是题比较硬核. 整体思路是维护每一个结 ...

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

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

随机推荐

  1. C#扫盲篇(一):反射机制--情真意切的说

    在一线编码已有多年,积累了不少非常实用的技能,最近的更新会逐步的分享出来,希望能帮助到还有一丢丢喜欢.Net的朋友,当然这些都比较适合入门选手,虽然自己已是个精通抄代码的老猿,但技术造诣仍是渣渣. 犹 ...

  2. PHP jquer网页打印插件 PrintArea

    <!DOCTYPE html> <head> <meta charset="utf-8"> <meta http-equiv=" ...

  3. 【Flutter】可滚动组件之SingleChildScrollView

    前言 SingleChildScrollView类似于Android中的ScrollView,它只能接收一个子组件. 接口描述 const SingleChildScrollView({ Key ke ...

  4. 深入剖析setState同步异步机制

    关于 setState setState 的更新是同步还是异步,一直是人们津津乐道的话题.不过,实际上如果我们需要用到更新后的状态值,并不需要强依赖其同步/异步更新机制.在类组件中,我们可以通过thi ...

  5. 【Linux】使用grep快速比较两个文件不同

    两个文件的比较,会有同学说使用diff,和vimdiff就可以快速比较,为什么还要使用grep呢? 有些时候,diff和vimdiff的时候环境不符合,这样的情况,就可以使用grep来解决这个问题. ...

  6. 【Jboss】应用中缺少宋体怎么办

    环境jboss4.2.2 系统CentOS7.2 1.新搭建的环境,但是没有字符集,在windows上的电脑上复制了一份宋体,打成zip包 将zip包上传到服务器中,解压 2.在/usr/share/ ...

  7. 通过logmnr找到被修改前的存储过程

    1.找到存储过程被修改时的归档日志 SELECT NAME FROM V$ARCHIVED_LOG WHERE FIRST_TIME BETWEEN TO_DATE('20191118080000', ...

  8. Sentry(v20.12.1) K8S 云原生架构探索,1分钟上手 JavaScript 性能监控

    系列 Sentry-Go SDK 中文实践指南 一起来刷 Sentry For Go 官方文档之 Enriching Events Snuba:Sentry 新的搜索基础设施(基于 ClickHous ...

  9. Entity与Entity之间的相互转化

    一.两个实体类的属性名称对应之间的转化 1.两个实体类 public class Entity1 { private Integer id; private String name; private ...

  10. 4、剑指offer——从尾到头打印链表java实现

    **题目描述** **输入一个链表,按链表从尾到头的顺序返回一个ArrayList.** 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M 思路:   1.如果链 ...