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 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.、
题意:
某公司员工上下级关系呈现树形结构,现在有任务需要分配,某个员工获得任务后,他的所有下属都会转为做该任务。现在需要分配任务以及查询某个员工正在做的任务。
将树形结构用dfs序的方法转变为线性结构,因此他的所有子树内部节点的编号均会在他的进入以及离开编号之间。这样就可以进行线段树区间修改和单点查询了。
#include<stdio.h>
#include<string.h>
const int maxm=5e4+; int head[maxm],nxt[maxm],point[maxm],size;
bool f[maxm];
int t,stx[maxm],edx[maxm];
int st[maxm<<],ch[maxm<<]; void add(int a,int b){
point[size]=a;
nxt[size]=head[b];
head[b]=size++;
} void dfs(int s){
stx[s]=++t;
for(int i=head[s];~i;i=nxt[i]){
int j=point[i];
dfs(j);
}
edx[s]=t;
} void pushdown(int o){
if(ch[o]!=-){
ch[o<<]=ch[o];
ch[o<<|]=ch[o];
st[o<<]=ch[o];
st[o<<|]=ch[o];
ch[o]=-;
}
} void pushup(int o){
if(st[o<<]==st[o<<|])st[o]=st[o<<];
else st[o]=-;
} void update(int o,int l,int r,int ql,int qr,int c){
if(ql<=l&&qr>=r){
ch[o]=c;
st[o]=c;
return;
}
pushdown(o);
int m=l+((r-l)>>);
if(ql<=m)update(o<<,l,m,ql,qr,c);
if(qr>=m+)update(o<<|,m+,r,ql,qr,c);
pushup(o);
} int query(int o,int l,int r,int ind){
if(st[o]!=-)return st[o];
if(l==r)return st[o];
pushdown(o);
int m=l+((r-l)>>);
if(ind<=m)return query(o<<,l,m,ind);
return query(o<<|,m+,r,ind);
} char s[]; int main(){
int T,cnt=;
scanf("%d",&T);
while(T--){
memset(head,-,sizeof(head));
size=;
memset(f,,sizeof(f));
t=;
int n;
scanf("%d",&n);
for(int i=;i<n;++i){
int a,b;
scanf("%d%d",&a,&b);
f[a]=;
add(a,b);
}
for(int i=;i<=n;++i){
if(!f[i]){
dfs(i);
break;
}
}
memset(st,-,sizeof(st));
memset(ch,-,sizeof(ch));
printf("Case #%d:\n",++cnt);
int m;
scanf("%d",&m);
for(int i=;i<=m;++i){
scanf("%s",s);
if(s[]=='C'){
int a;
scanf("%d",&a);
printf("%d\n",query(,,t,stx[a]));
}
else if(s[]=='T'){
int a,b;
scanf("%d%d",&a,&b);
update(,,t,stx[a],edx[a],b);
}
}
}
return ;
}
hdu3974 Assign the task dfs序+线段树的更多相关文章
- HDU3974 Assign the task —— dfs时间戳 + 线段树
题目链接:https://vjudge.net/problem/HDU-3974 There is a company that has N employees(numbered from 1 to ...
- 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 ...
- [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序 + 线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 给你T组数据,n个节点,n-1对关系,右边的是左边的父节点,所有的值初始化为-1,然后给你q个操 ...
- Assign the task-HDU3974 dfs序+线段树
题意: 一个公司有n个员工,每个员工都有一个上司,一个人下属的下属也是这个人的下属,因此可将他们的关系看成一棵树, 然后给定两种操作,C操作是查询当前员工的工作,T操作是将y工作分配给x员工,当一个人 ...
- HDU 3974 Assign the task(dfs建树+线段树)
题目大意:公司里有一些员工及对应的上级,给出一些员工的关系,分配给某员工任务后,其和其所有下属都会进行这项任务.输入T表示分配新的任务, 输入C表示查询某员工的任务.本题的难度在于建树,一开始百思不得 ...
- Codeforces Round #442 (Div. 2)A,B,C,D,E(STL,dp,贪心,bfs,dfs序+线段树)
A. Alex and broken contest time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- CodeForces 877E Danil and a Part-time Job(dfs序+线段树)
Danil decided to earn some money, so he had found a part-time job. The interview have went well, so ...
- Educational Codeforces Round 6 E dfs序+线段树
题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...
随机推荐
- MAVEN 创建项目
使用archetype生成项目骨架 MAVEN 创建项目JAR 和 MAVEN创建项目WAR中是使用特定的acrchetype来进行创建项目,如果使用其他的archetype来创建项目或是使用 mvn ...
- Vue + Element UI 实现权限管理系统(工具模块封装)
封装 axios 模块 封装背景 使用axios发起一个请求是比较简单的事情,但是axios没有进行封装复用,项目越来越大,会引起越来越多的代码冗余,让代码变得越来越难维护.所以我们在这里先对 axi ...
- day04流程控制之while循环
流程控制之while循环 1.什么是while循环 循环指的是一个重复做某件事的过程 2.为何有循环 为了让计算机能像人一样重复 做某件事 3.如何用循环 ''' # while循环的语法:while ...
- WPF 基于Adorner实现类似Popup效果
1. 什么是Adorner 装饰器是一种特殊类型的FrameworkElement,可用来向用户提供可视提示. 装饰器有很多用途,可用来向元素添加功能句柄,或者提供有关某个控件的状态信息. 2. ...
- day31 锁 队列 前面课程重点总结
今日内容: 1.进程的其他方法 2.僵尸进程和孤儿进程(了解) 3.验证进程之间是空间隔离的 4.守护进程 5.进程锁 重点(又叫同步锁,互斥锁) 6.进程队列(重点) Queue 7.生产者消费者 ...
- JavaScrip(三)JavaScrip变量高级操作(字符串,数组,日期)
一:字符串 charAt() 返回指定位置的字符 indexof() 返回指定字符串首次出现的位置 replace() 替换指定的字符 concat() 连接两个或多个字符串 substr(start ...
- svn服务器搭建及使用(三)
接下来,试试用TortoiseSVN修改文件,添加文件,删除文件,以及如何解决冲突等. 添加文件 在检出的工作副本中添加一个Readme.txt文本文件,这时候这个文本文件会显示为没有版本控制的状态, ...
- RabbitMQ direct类型的Exchange
就目前来说,Exchange是与消息发送端有关的,因为它可以指定将消息发送到哪个或哪些队列中. 本篇文章介绍的direct类型就是指定将消息定向发送到哪个队列中. direct,顾名思义,就是直接的意 ...
- 第二篇 界面开发 (Android学习笔记)
第二篇 界面开发 第5章 探索界面UI元素 ●The Android View Class ●△Widget设计步骤 需要修改三个XML,以及一个class: 1)第一个xml是布局XML文件 ...
- Cracking The Coding Interview 2.2
#include <iostream> #include <string> using namespace std; class linklist { private: cla ...