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 ...
随机推荐
- linux command mktemp
Linux command mktemp [Purpose] Learning linux command mktemp to create a temporary file or di ...
- 尚学堂java 答案解析 第五章
本答案为本人个人编辑,仅供参考,如果读者发现,请私信本人或在下方评论,提醒本人修改 一.选择题 1.AB 解析:A可以被所有类访问,B可以被当前包的所有类访问,也可以被所有子类访问 2.A 解析:所有 ...
- ssd物体检测模型训练和测试总结
参考网址:github:https://github.com/naisy/realtime_object_detection 2018.10.16ssd物体检测总结:切记粗略地看一遍备注就开始训练模型 ...
- UVa 11384 - Help is needed for Dexter 分析, 树状数组 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- .net core Asp.net Mvc Ef 网站搭建 vs2017 1)
1)开发环境搭建 首先下载安装vs2017 地址 :https://www.visualstudio.com/zh-hans/downloads/ 安装勾选几项如下图 ,注意点在单个组件时.net ...
- 总结5条对学习Linux系统有帮助的经验心得
作为国产手机中的代表厂商,OPPO一直走在国内的前沿.不仅手机出货量在国内遥遥领先,而且在国外也抢占不少的市场份额.前段时间,OPPO在台湾地区签下田馥甄和林宥嘉担任OPPO R9s的代言人外,在东南 ...
- Yii2.0 数据库查询 [ 2.0 版本 ]
下面介绍一下 Yii2.0 对数据库 查询的一些简单的操作 User::find()->all(); 此方法返回所有数据: User::findOne($id); 此方法返回 主键 id=1 的 ...
- 【转载】OpenCV 摄像头控制
参考:[OpenCV] -- 简单摄像头操作 - 代码人生 - 博客频道 - CSDN.NET http://blog.csdn.net/qiurisuixiang/article/details/8 ...
- oracle sqlserver mysql 通过sql查看表及字段注释
oracle: SELECT A.TABLE_NAME,A.COMMENTS,B.COLUMN_NAME,B.COMMENTS FROM USER_TAB_COMMENTS A,USER_COL_CO ...
- 每天CSS学习之letter-spacing
letter-spacing是CSS的一个属性,其作用是设置字符之间的距离.letter意为字符. 1.normal:规定字符之间没有额外的空间.该值是默认值.如下示例: p{ letter-spac ...