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. 使用SharePoint App-Only获得访问权限

    目前在开发SharePoint Online的过程中,主要使用通过Azure AD的方式获得应用的访问权限,但是SharePoint App-Only的方式依旧被保留了.使用这种方式进行CSOM开发比 ...

  2. 如何使用 VS Code开发.NET Core应用程序

    Visual Studio Code(VS Code)是Microsoft为Windows,Linux和Mac操作系统开发的免费,跨平台,轻量级的源代码编辑器,它是源代码编辑器,而Visual Stu ...

  3. python的默认参数的一个坑

    前言 pass 正文 在 https://docs.python.org/3/tutorial/controlflow.html#default-argument-values 中,有这样一段话 Im ...

  4. Flutter 布局类组件:层叠布局(Stack和Positioned)

    前言 层叠布局,即子组件可以根据距父容器四个角的位置来确定自身的位置.绝对定位运行子组件堆叠起来,即按照代码中声明的顺序. Flutter中使用Stack和Positioned这两个组件来配合实现绝对 ...

  5. rename 表名

    rename table 旧表名1 to 新表名1,旧表名2 to 新表名2;

  6. 使用yaml来实现ingress-nginx

    创建一个ingress-nginx [root@k8s-master ingress]# cat ingress-nginx.yaml apiVersion: v1 kind: Namespace m ...

  7. 1.5V升3.3V芯片电路图,稳压3.3V供电MCU模块等

    干电池1.5V可以升到3.3V,通过PW5100干电池升压IC,于外围3个元件:2个电容和一个电感即可组成1.5V升3.3V的电路系统. 干电池属于低能量的电池产品,不过一般使用到干电池的产品也是输出 ...

  8. js千分位分隔,数字货币化方法学习记录

    js千分位分隔,数字货币化-4种方法(含正则) 方法1-整数货币化 // 整数货币化 function intCurrency(num) { var reg = new RegExp("^[ ...

  9. winform 扫码识别二维码

    因为公司业务需求,需要在Windows系统下调用摄像头识别二维码需求,就有了这个功能. 我根据网上网友提供的一些资料,自己整合应用到项目中,效果还不错(就是感觉像素不是太好) 现在将调用摄像头+识别二 ...

  10. ArchLinux安装后所需要的环境和工具

    ArchLinux安装后所需要的环境和工具 工具: Dolphin 文件管理器 ntfs-3G 移动硬盘挂载 octopi 实时检查更新 KDE Connect 手机电脑远程连接 DBeaver Co ...