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. Vue + Element UI 实现权限管理系统(动态加载菜单)

    动态加载菜单 之前我们的导航树都是写死在页面里的,而实际应用中是需要从后台服务器获取菜单数据之后动态生成的. 我们在这里就用上一篇准备好的数据格式Mock出模拟数据,然后动态生成我们的导航菜单. 接口 ...

  2. Resharper插件安装和破解

    1.首先在最下面的地址,下载Resharper安装包,进行解压安装,安装界面如下: a 2.安装后 解压下载好的 文件 会得到如下: 3.打开序列号 会看到如下所示: 4.然后  复制 %LocalA ...

  3. Spring、SpringMVC、Hibernate详细整合实例,包含所有步骤

    Eclipse完整工程如下 Jar包如下 CSDN下载地址:https://download.csdn.net/download/zhutouaizhuwxd/9721062 其中,整个工程主要可以分 ...

  4. csv,json格式数据的读写

    #!python3 # -*- coding:utf-8 -*- #CSV stands for "comma-separated values",and CSV files ar ...

  5. hMailServer 配置

    本例记录如何通过  [hMailServer] 在私有服务器中搭建邮件服务器 1.下载安装包 版本: hMailServer-5.6.7-B2425.exe (支持使用内置数据库) , 安装时,设置管 ...

  6. 重载的方式写Python的get请求

    #encoding=utf-8#__author__="Lanyangyang" import unittestimport requestsimport json # This ...

  7. centos7安装配置zabbix4.0

    zabbix01    198.8.8.211    zabbix-server4.0 zabbix02    198.8.8.212    zabbix-agent4.0 一:zabbix服务端环境 ...

  8. Convert the AScii to SAC file

    readtable *.txt w sac  filename.sac ch delta dela0 w over

  9. Maven下用MyBatis Generator生成文件

    使用Maven命令用MyBatis Generator生成MyBatis的文件步骤如下: 1.在mop文件内添加plugin <build> <finalName>KenShr ...

  10. SpringBatch Sample (二)(CSV文件操作)

    本文将通过一个完整的实例,与大家一起讨论运用Spring Batch对CSV文件的读写操作.此实例的流程是:读取一个含有四个字段的CSV文件(ID,Name,Age,Score),对读取的字段做简单的 ...