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序+线段树的更多相关文章

  1. HDU3974 Assign the task —— dfs时间戳 + 线段树

    题目链接:https://vjudge.net/problem/HDU-3974 There is a company that has N employees(numbered from 1 to ...

  2. 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 ...

  3. [Assign the task][dfs序+线段树]

    http://acm.hdu.edu.cn/showproblem.php?pid=3974 Assign the task Time Limit: 15000/5000 MS (Java/Other ...

  4. HDU 3974 Assign the task (DFS序 + 线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 给你T组数据,n个节点,n-1对关系,右边的是左边的父节点,所有的值初始化为-1,然后给你q个操 ...

  5. Assign the task-HDU3974 dfs序+线段树

    题意: 一个公司有n个员工,每个员工都有一个上司,一个人下属的下属也是这个人的下属,因此可将他们的关系看成一棵树, 然后给定两种操作,C操作是查询当前员工的工作,T操作是将y工作分配给x员工,当一个人 ...

  6. HDU 3974 Assign the task(dfs建树+线段树)

    题目大意:公司里有一些员工及对应的上级,给出一些员工的关系,分配给某员工任务后,其和其所有下属都会进行这项任务.输入T表示分配新的任务, 输入C表示查询某员工的任务.本题的难度在于建树,一开始百思不得 ...

  7. 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 ...

  8. 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 ...

  9. Educational Codeforces Round 6 E dfs序+线段树

    题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...

随机推荐

  1. Win10系列:UWP界面布局基础5

    (2)编写后台代码访问资源 下面通过一个例子来演示如何编写后台代码引用资源.新建一个Windows应用商店的空白应用程序项目,将其命名为AccessResourceApplication,打开项目下的 ...

  2. Java 实现倒计时(由秒计算天、小时、分钟、秒)

    public class Countdown4 { private static long day = 0; private static long hour = 0; private static ...

  3. jenkins部署java项目在本地(三)

    (1)新建maven构建的java项目 pom.xml的配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns ...

  4. TreeMap - 源代码学习笔记

    TreeMap 实现了 NavigableMap 接口,而NavigableMap 接口继承于 SortedMap接口. 所有本文还会记录 SortedMap 和 NavigableMap 的阅读笔记 ...

  5. [Leetcode 376]摇摆序列 Wiggle Subsequence

    [题目] A sequence of numbers is called a wiggle sequence if the differences between successive numbers ...

  6. turtle

    画一组同切圆 输入 import turtle turtle.color('red') turtle.circle(30) turtle.circle(60) turtle.circle(90) tu ...

  7. 传统应用迁移到kubernetes(Hadoop YARN)

    spark-on-yarn-with-kubernetes 该例子仅用来说明具体的步骤划分和复杂性,在生产环境应用还有待验证,请谨慎使用. 过程中可能用到的概念和术语初步整理如下: 整个迁移过程分为如 ...

  8. DevExpress WPF v18.2新版亮点(七)

    行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍新版本新功能.本文将介绍了DevExpress WPF v18.2的新功 ...

  9. C++类构造函数初始化列表(转)

    构造函数初始化列表以一个冒号开始,接着是以逗号分隔的数据成员列表,每个数据成员后面跟一个放在括号中的初始化式.例如: { public:     int a;     float b;     //构 ...

  10. poj3080(kmp+枚举)

    Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20163   Accepted: 8948 Descr ...