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. FastApi学习(一)

    前言 学习不止 正文 介绍 FastApi是PythonWeb框架的'新晋干员',虽然年轻但是很能打 目前已有 12k start GitHub 官网 为什么说他能打呢?它内部使用了 Python 的 ...

  2. 【Java】变量

    变量 文章目录 变量 1.变量的概念 2.变量的三要素 3.变量的使用应该注意什么? 4.变量的声明和赋值.使用的语法格式? 5.code 1.变量的概念 变量的作用:变量用来存储数据. 变量的本质: ...

  3. OpenID协议

    背景 当我们要使用一个网站的功能时,一般都需要注册想用的账号.现在的互联网应用很多,一段时间之后你会发现你注册了一堆账号密码,根本记不住. 你可能会想到所有的网站都用同一套用户名和密码,这样虽然能解决 ...

  4. kubernets之ReplicaSet

    一   介绍RS 1.1   RS与RC在功能上基本上是一摸一样的,因为两者的功能都是用来管控集群内部的pod,并且 两者都具备模版,副本数量以及标签选择器等三要素,区别点在于,RS拥有着更为强大的标 ...

  5. Netty学习:ChannelHandler执行顺序详解,附源码分析

    近日学习Netty,在看书和实践的时候对于书上只言片语的那些话不是十分懂,导致尝试写例子的时候遭遇各种不顺,比如decoder和encoder还有HttpObjectAggregator的添加顺序,研 ...

  6. 创建Django REST framework工程

    1.创建工程虚拟环境 2.创建工程目录和调整目录结构: 创建Django的项目 创建docs 用于存放一些说明文档资料 创建scripts 用于存放管理脚本文件 创建logs 用于存在日志 在与项目同 ...

  7. 24V转5V芯片,高输入电压LDO线性稳压器

    PW6206系列是一个高精度,高输入电压低静态电流,高速,低功耗降线性稳压器具有高纹波抑制.输入电压高达40V,负载电流为在VOUT=5V和VIN=7V时高达300mA.该设备采用BCD工艺制造.PW ...

  8. USB充电限流IC,可调到4.8A,输入 6V关闭

    随着手机充电电流的提升,和设备的多样化,USB限流芯片就随着需求的增加而越来越多,同时为了更好的保护电子设备,需要进行一路或者多路的负载进行限流. 一般说明 PW1503,PW1502是超低RDS(O ...

  9. mysqldump导出数据库导入数据库

    使用mysqldump命令导出数据库,格式如下,请按实际要求对参数进行替换: mysqldump -u 用户名 -p 数据库名 > 导出的文件名 比如导出数据库business_db: mysq ...

  10. GRASP职责分配模式

    https://mp.weixin.qq.com/s/IaxAnWfVqe3mM0bHFVV5Gg 软件开发必修课:你该知道的GRASP职责分配模式 原创 悟真 阿里技术 今天 收录于话题 #设计模式 ...