题意:给出一棵树,改变树的一个节点的值,那么该节点及所有子节点都变为这个值。给出m个询问。

思路:DFS序,将树改为线性结构,用线段树维护。start[ ]记录每个节点的编号,End[ ]为该节点的最小子节点的编号,维护线段树时,即是维护start[x] 到End[x]。

代码:

#include<queue>
#include<cstring>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<vector>
#include<cstdio>
#include<iostream>
#include<algorithm>
#define ll long long
const int N = 50000+5;
const int MOD = 20071027;
using namespace std;
vector<int> g[N];
int tot,start[N],End[N],a[N<<2],vis[N];
void init(){
for(int i = 0; i < N; i++) g[i].clear();
memset(vis,0,sizeof(vis));
tot = 0;
}
void add(int u,int v){ // u is boss
g[u].push_back(v);
vis[v] = 1;
}
void dfs(int x){
start[x] = ++tot;
int len = g[x].size();
for(int i = 0; i < len; i++){
int v = g[x][i];
dfs(v);
}
End[x] = tot;
}
void build(int rt,int l,int r){
if(l == r){
a[rt] = -1;
return;
}
int m = (l + r) >> 1;
build(rt<<1,l,m);
build(rt<<1|1,m+1,r);
a[rt] = -1;
}
void push_down(int rt){
if(a[rt] != -1){
a[rt<<1] = a[rt<<1|1] = a[rt];
a[rt] = -1;
}
}
void update(int rt,int l,int r,int L,int R,int v){
if(L <= l && R >=r){
a[rt] = v;
return;
}
push_down(rt);
int m = (l + r) >> 1;
if(L <= m) update(rt<<1,l,m,L,R,v);
if(R > m) update(rt<<1|1,m+1,r,L,R,v);
}
int query(int rt,int l,int r,int x){
if(l == r){
return a[rt];
}
if(a[rt] != -1){
return a[rt];
}
int ans;
int m = (l + r) >> 1;
if(x <= m) ans = query(rt<<1,l,m,x);
else ans = query(rt<<1|1,m+1,r,x);
return ans;
}
int main(){
int T,n,u,v,m,num = 1;
char order[2];
scanf("%d",&T);
while(T--){
scanf("%d",&n);
init();
for(int i = 0; i < n-1; i++){
scanf("%d%d",&v,&u);
add(u,v);
}
for(int i = 1; i <= n; i++){
if(!vis[i]){
dfs(i);
break;
}
}
build(1,1,n);
scanf("%d",&m);
printf("Case #%d:\n",num++);
while(m--){
scanf("%s",order);
if(order[0] == 'C'){
scanf("%d",&u);
printf("%d\n",query(1,1,n,start[u]));
}
else{
scanf("%d%d",&u,&v);
update(1,1,n,start[u],End[u],v);
}
}
}
return 0;
}

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. HDU 3974 Assign the task (DFS序 + 线段树)

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

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

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

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

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

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

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

  6. hdu 3974 Assign the task(dfs序上线段树)

    Problem Description There is a company that has N employees(numbered from 1 to N),every employee in ...

  7. HDU 3974 Assign the task 暴力/线段树

    题目链接: 题目 Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...

  8. HDU 3974 Assign the task 并查集/图论/线段树

    Assign the task Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  9. hdu3974 Assign the task dfs序+线段树

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

随机推荐

  1. Alignment--POJ1836

    Description In the army, a platoon is composed by n soldiers. During the morning inspection, the sol ...

  2. 优云软件应邀出席 ITSS 数据中心运营管理工作组 2017 年春季研讨会

    2017 年 4 月 15 日,中国电子工业标准化技术协会信息技术服务分会(以下称 ITSS 分会)数据中心运营管理工作组(以下简称 DCMG)在江苏省启东市召开春季研讨会. DCMG 工作组组长肖建 ...

  3. noip做题记录+挑战一句话题解?

    因为灵巧实在太弱辽不得不做点noip续下命QQAQQQ 2018 积木大赛/铺设道路 傻逼原题? 然后傻逼的我居然检查了半天是不是有陷阱最后花了差不多一个小时才做掉我做过的原题...真的傻逼了我:( ...

  4. Linux输入输出重定向和文件查找值grep命令

    Linux输入输出重定向和文件查找值grep命令 一.文件描述符Linux 的shell命令,可以通过文件描述符来引用一些文件,通常使用到的文件描述符为0,1,2.Linux系统实际上有12个文件描述 ...

  5. android奔溃日期一闪而过

    Android Studio日期崩溃了一闪而过,看不到原因:可以设置No Filters就可以了

  6. Windows操作系统上各种服务使用的端口号, 以及它们使用的协议的列表

    Windows操作系统上各种服务使用的端口号, 以及它们使用的协议的列表 列表如下 Port Protocol Network Service System Service System Servic ...

  7. InnoDB: Operating system error number 87 in a file operation. 错误87的解决方法

    InnoDB: Operating system error number 87 in a file operation. 错误87的解决方法 140628  8:10:48 [Note] Plugi ...

  8. string、const char*、 char* 、char[]相互转换(待整理)

    string.const char*. char* .char[]相互转换(全) https://blog.csdn.net/rongrongyaofeiqi/article/details/5244 ...

  9. auto类型-现代C++新特性

    auto类型 C++11中引入的auto主要用于类型推导.auto在C++98中"存储类型指示符"的语义,由于使用极少且多余,该语义从C++11开始被删除. auto类型推导用于从 ...

  10. ppt插入声音

    1:点击插入>音频>文件中的音频 2:插入成功后,会出现一个声音的图表 3:对播放格式进行设置,设置循环播放等. 4:双击对声音进行编辑 ,会出现右边的各个组件, 5:点击下拉框>效 ...