题意:给出一棵树,改变树的一个节点的值,那么该节点及所有子节点都变为这个值。给出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. cookie的封装写法

    设置cookie 三个参数分别代表:键,值,过期时间,这个封装方法可以完成cookie的储存   以及cookie的删除(过期时间设为赋值) function setCookie(cname,cval ...

  2. 【Python】web.py-简单轻量级网页框架python

    简单轻量级网页框架python web.py的安装 python 3.x中安装web.py 最近决定从python2.7转移到3.x上工作. 使用数据库的时候,依然选用了之前比较感兴趣的web.py ...

  3. CentOS工作内容(一)CentOS6.4的安装 hwclock和date

    CentOS工作内容(一)CentOS6.4的安装 hwclock和date 光碟安装 分配20G磁盘空间 插入光碟 选择第一项安装 如果要急救的话请选择第三项 启动安装进程 跳过光碟检测 选择nex ...

  4. (转载)【cocos2dx 3.x Lua] 注册事件函数详解

    出处: http://www.2cto.com/kf/201409/338235.html coocs2dx 版本 3.1.1 registerScriptTouchHandler 注册触屏事件 re ...

  5. git 下载单个文件 已经添加多个远程服务器

    175down vote It is possible to do (in the deployed repository) git fetch followed by git checkout or ...

  6. 神经网络前向后向传播(理论推导+代码) 单层神经网络相当于logistic regression

    建立神经网络的主要步骤是: 1. 定义模型结构(例如输入特征的数量) 2. 初始化模型的参数 3. 循环: # 3.1 计算当前损失(正向传播) # 3.2 计算当前梯度(反向传播) # 3.3 更新 ...

  7. http协议基础(四)http状态码

    一:http状态码 表示客户端http请求的返回结果.标记服务器端的处理是否正常.通知出现的错误等工作 状态码的类别如下: http状态码种类繁多,大概有60多种,实际上经常使用的只有14种,下面为一 ...

  8. string.Format字符串格式说明(转)

    转自:http://blog.csdn.net/dl020840504/article/details/8921875   先举几个简单的应用案例: 1.格式化货币(跟系统的环境有关,中文系统默认格式 ...

  9. Python Socket编程基础篇

    Socket网络编程 socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. ...

  10. rpgmakermv(6) YEP_ItemSynthesis.js物品合成插件

    物品合成插件. 用途?太多了呢. 低级宝石合成高级,还有装备,武器,药水等等. ============================================================ ...