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 ...
随机推荐
- Win10系列:UWP界面布局基础1
随着技术的不断发展,使用者对应用程序的界面体验提出了更高的要求,为了应对越来越复杂的界面设计需求和有效的简化界面开发过程,微软公司在其应用程序的开发技术当中引入一套新的应用程序界面描述语言,这就是XA ...
- fzu1977
题解: 和前两题差不多 只不过变成了有些一定走,有些不一定 代码: #include<cstdio> #include<cmath> #include<algorithm ...
- Python3 线程/进程池 concurrent.futures
python3之concurrent.futures一个多线程多进程的直接对接模块,python3.2有线程池了 Python标准库为我们提供了threading和multiprocessing模块编 ...
- PC/FORTH 数字类型
body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...
- eclipse项目环境搭建(做了好多遍,老是忘记,以此文帮助记忆)
今天把eclipse的环境搭建好(不能用myeclipse好忧伤). 要求: 1.在svn上管理项目,要下载svn插件. 2.是web所以要用到tomcat插件. 3.将项目运行起来. 流程: ecl ...
- ajax返回数据定义为全局变量
var result; //定义全局变量 $(document).ready(function(){ $.ajax({ type:'PO ...
- vivado对task和function的可综合支持
手册UG901,对vivado可综合的语句支持进行了描述,HDL包括:verilog-2001,system-verilog,VHDL: verilog-2001扩展了对task和function的支 ...
- HihoCoder - 1483 区间最值
给定n个数A1...An,小Ho想了解AL..AR中有多少对元素值相同.小Ho把这个数目定义为区间[L,R]的价值,用v[L,R]表示. 例如1 1 1 2 2这五个数所组成的区间的价值为4. 现在小 ...
- VS2012里面使用EF框架的增删改查和分页的方法
public class BaseRepository<T> where T : class { //实例化EF框架 DataModelContainer ...
- nginx的相关配置记录和总结
前言 本文旨在对nginx的各项配置文件和参数做一个记录和总结. 原因是在配置框架和虚拟目录,web语言解析的nginx环境的时候遇到各种问题和参数,有时百度可以解决,有时直接复制粘贴,大都当时有些记 ...