HDU 3974 Assign the task(DFS序)题解
题意:给出一棵树,改变树的一个节点的值,那么该节点及所有子节点都变为这个值。给出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序)题解的更多相关文章
- 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 ...
- HDU 3974 Assign the task (DFS序 + 线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 给你T组数据,n个节点,n-1对关系,右边的是左边的父节点,所有的值初始化为-1,然后给你q个操 ...
- HDU 3974 Assign the task(dfs建树+线段树)
题目大意:公司里有一些员工及对应的上级,给出一些员工的关系,分配给某员工任务后,其和其所有下属都会进行这项任务.输入T表示分配新的任务, 输入C表示查询某员工的任务.本题的难度在于建树,一开始百思不得 ...
- HDU 3974 Assign the task (DFS+线段树)
题意:给定一棵树的公司职员管理图,有两种操作, 第一种是 T x y,把 x 及员工都变成 y, 第二种是 C x 询问 x 当前的数. 析:先把该树用dfs遍历,形成一个序列,然后再用线段树进行维护 ...
- [Assign the task][dfs序+线段树]
http://acm.hdu.edu.cn/showproblem.php?pid=3974 Assign the task Time Limit: 15000/5000 MS (Java/Other ...
- hdu 3974 Assign the task(dfs序上线段树)
Problem Description There is a company that has N employees(numbered from 1 to N),every employee in ...
- HDU 3974 Assign the task 暴力/线段树
题目链接: 题目 Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 3974 Assign the task 并查集/图论/线段树
Assign the task Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- 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 ...
随机推荐
- Ucenter社区服务搭建
1.搭建lamp环境yum –y install httpd php php-mysql mysql mysql-server 2启动服务 3.设置服务开机自动启动 4.上传UCEN ...
- python string 模块
标准库 python3 python2.7 都可以用 sting.ascii_letters是生成所有字母,从a-z和A-Z, string.digits是生成所有数字0-9. import stri ...
- unity3d之 C# WaitFOrSeconds()
学习unity3d不久.在使用WaitFOrSeconds()时,遇到了不少麻烦,故记录,以警示后人. 首先介绍C#和javascript 在使用它有非常大的差别. javascript能够直接使用 ...
- golang 的精髓--pipeline流水线,对现实世界的完美模拟
https://blog.golang.org/pipelines https://www.cnblogs.com/junneyang/p/6215785.html 简介 Go语言的并发原语允许开发者 ...
- 查看crontab的日志记录定位定时任务问题
1.linux 看 /var/log/cron这个文件就可以,可以用tail -f /var/log/cron观察 2.unix 在 /var/spool/cron/tmp文件中,有croutXXX0 ...
- shell date 获取昨天日期
使用date -d 选项: date +"%Y%m%d" -d "+n days" 今天的后n天日期 date +" ...
- Elasticsearch Date类型使用技巧
elasticsearch原生支持date类型.这里简单记录下使用的方法. 使用date类型可以用如下两种方式: 使用毫秒的时间戳,直接将毫秒值传入即可. 传入格式化的字符串,默认是ISO 8601标 ...
- 11.2.0.4 RAC测试环境修改时区
当前问题: 系统时区修改后,集群数据库各个日志发现显示的还是之前时区的时间. 依据Linux (RHEL)修改时区更改了系统的时区后,集群数据库的各个日志还是显示之前的时区时间. 查找MOS资料 Ho ...
- 非线性方程(组):MATLAB内置函数 solve, vpasolve, fsolve, fzero, roots [MATLAB]
MATLAB函数 solve, vpasolve, fsolve, fzero, roots 功能和信息概览 求解函数 多项式型 非多项式型 一维 高维 符号 数值 算法 solve 支持,得到全部符 ...
- 004-ubuntu安装配置SSH服务
一.ssh安装. 1.# sudo apt-get -y install openssh-server. 2.在/etc/ssh/sshd_config文件中添加一句:PermitRootLogin ...