题意:给出一棵树,改变树的一个节点的值,那么该节点及所有子节点都变为这个值。给出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. 第十八篇:融汇贯通--谈USB Video Class驱动

    USB Video Class驱动是WINDOWS系统包含的一个针对于USB VIDEO 类的驱动程序. 好多project师都做过USB VIDEO设备端的开发, 基本的工作内容为: 使用FIRMW ...

  2. react.js 教程之 Installation 安装

    react.js 教程之 Installation 安装 运行方法 运行react有三种方式 1.如果你只是学习react,可以在http://codepen.io/gaearon/pen/rrpgN ...

  3. Vue 命令

    vue是数据渲染使用:axios,官网:https://www.kancloud.cn/yunye/axios/234845     ||  https://www.npmjs.com/search? ...

  4. POJ3096:Surprising Strings(map)

    http://poj.org/problem?id=3096 for循环真是奇妙! #include <string.h> #include <stdio.h> #includ ...

  5. libSVM简介及核函数模型选择

    1. libSVM简介 训练模型的结构体 struct svm_problem //储存参加计算的所有样本 { int l; //记录样本总数 double *y; //指向样本类别的组数 struc ...

  6. Postman + newman + jenkins 的API自动化测试应用

    一.环境配置 Postman postman 的具体使用可以参考另外一篇文章:postman 做接口测试之学习笔记 Newman 第一步,安装nodejs. 第二步,在nodejs命令行安装newma ...

  7. AspxGridView点滴

    1:页码设置 1>: <SettingsPager Summary-Text="当前第 {0} 页 总共 {1} 页 ({2} 条记录)"></Settin ...

  8. liferay增删改简单小练习

    liferay简单增删改 大家都知道,我们每学习一项技能的时候,都会做一些简单的小实例,来检验我们学习成果,这个也不例外. 我建议大家学习完三大框架之后再来看这个demo. 首先:先说一下,零碎的知识 ...

  9. Typecho博客让文章列表页只显示摘要的方法

    在当前主题的 index.php 文件中找到代码 <?php $this->content('阅读剩余部分...'); ?> 将其替换为 <?php $this->exc ...

  10. sql 各种锁

    SELECT * FROM table WITH (HOLDLOCK) 注意: 锁定数据库的一个表的区别 SELECT * FROM table WITH (HOLDLOCK) 其他事务可以读取表,但 ...