http://acm.hdu.edu.cn/showproblem.php?pid=3974

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem Description

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.

Input

The first line contains a single positive integer T( T <= 10 ), indicates the number of test cases.
For each test case:
The first line contains an integer N (N ≤ 50,000) , which is the number of the employees.
The following N - 1 lines each contain two integers u and v, which means the employee v is the immediate boss of employee u(1<=u,v<=N).
The next line contains an integer M (M ≤ 50,000).
The following M lines each contain a message which is either
"C x" which means an inquiry for the current task of employee x
or
"T x y"which means the company assign task y to employee x.
(1<=x<=N,0<=y<=10^9)

Output

For each test case, print the test case number (beginning with 1) in the first line and then for every inquiry, output the correspond answer per line.

Sample Input


C
T
C
T
C

Sample Output

Case #:
-

题意:

公司中有N个职员,只有一个老板,其余每个职员对应一个上司,这N个职员刚好组成一棵树。然后有M个操作。

当给一个职员x分配任务y时(即 T x y),x及其下属及其下属的下属都会放下手头的工作去完成任务y。对于每次询问(即C x),程序输出职员x正在进行什么任务。

简而言之:给出一个有向多叉树,初始所有节点为-1.有两种操作,一种为(T,x,y),表示将x节点及其的所有子树中的节点变为y,另一种为(C,x),为求x节点的值。

题解:

这道题的难点就在于如何将题中的对应关系转化为线段树的模型,在于如何处理好多叉树和线段树间的映射关系。

线段树的本质就是对一整段连续区间进行操作。所以这道题的关键就在于清楚怎么将对多叉树的操作转化成对一整段连续区间的操作。

很明显该公司的所有员工间的关系可以用一颗多叉树来表示。然后从公司老板(也就是入度为0的点)出发,用dfs给这棵树打上时间戳,确定每个点的影响范围并记录下来。

根据新分配的id号码将其节点对应映射到线段树上面。这样分配任务就相当于更新一段连续的节点,查询任务相当于单点查询。

代码如下:

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <math.h>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
//const double PI=acos(-1);
const int maxn=1e5+;
using namespace std;
//ios::sync_with_stdio(false);
// cin.tie(NULL); struct Edge_node
{
int v;
int next;
}Edge[*]; struct SegTree_node
{
int l;
int r;
int num;
}SegTree[<<];
int n,m;
int cnt;//边的计数器,从0开始
int num;//线段树序号的计数器,从1开始
int head[];
int vis[];//是否有上司,即入度为0
int st[];//开始遍历时的编号,同时作为线段树中的序号
int ed[];//遍历结束后的序号,表示该点在线段树中可以影响的最后一个序号(即上司) void init()//初始化
{
cnt=;
num=;
memset(head,-,sizeof(head));
memset(st,,sizeof(st));
memset(ed,,sizeof(ed));
memset(Edge,,sizeof(Edge));
memset(vis,,sizeof(vis));
} void add_edge(int u,int v)
{
Edge[cnt].v=v;
Edge[cnt].next=head[u];
head[u]=cnt++;
} void DFS(int u)
{
num++;
st[u]=num;
for(int i=head[u];i!=-;i=Edge[i].next)
{
DFS(Edge[i].v);
}
ed[u]=num;
} void PushDown(int rt)
{
if(SegTree[rt].num!=-)
{
SegTree[rt<<].num=SegTree[rt<<|].num=SegTree[rt].num;
SegTree[rt].num=-;
}
} void Build(int l,int r,int rt)
{
SegTree[rt].l=l;
SegTree[rt].r=r;
SegTree[rt].num=-;
if(l==r)
return ;
int mid=(l+r)>>;
Build(l,mid,rt<<);
Build(mid+,r,rt<<|);
} void Update(int L,int R,int C,int rt)
{
int l=SegTree[rt].l;
int r=SegTree[rt].r;
if(L<=l&&R>=r)
{
SegTree[rt].num=C;
return ;
}
PushDown(rt);
int mid=(l+r)>>;
if(L<=mid)
Update(L,R,C,rt<<);
if(R>mid)
Update(L,R,C,rt<<|);
} int Query(int L,int rt)
{
int l=SegTree[rt].l;
int r=SegTree[rt].r;
if(l==r)
return SegTree[rt].num;
PushDown(rt);
int mid=(l+r)>>;
if(L<=mid)
Query(L,rt<<);
else if(L>mid)
Query(L,rt<<|);
} int main()
{
int T;
scanf("%d",&T);
for(int k=;k<=T;k++)
{
printf("Case #%d:\n",k);
init();
scanf("%d",&n);
for(int i=;i<n;i++)
{
int u,v;
scanf("%d %d",&u,&v);
vis[u]=;
add_edge(v,u);
}
for(int i=;i<=n;i++)
{
if(!vis[i])
{
DFS(i);
break;
}
}
Build(,num,);
scanf("%d",&m);
for(int i=;i<=m;i++)
{
char c[];
int x;
scanf("%s %d",c,&x);
if(c[]=='C')
{
printf("%d\n",Query(st[x],));
}
else if(c[]=='T')
{
int t;
scanf("%d",&t);
//st[x]->ed[x]代表x及x的下属在线段树中的区间范围
Update(st[x],ed[x],t,);
}
}
}
return ;
}

HDU-3974 Assign the task(多叉树DFS时间戳建线段树)的更多相关文章

  1. HDU 3974 Assign the task 暴力/线段树

    题目链接: 题目 Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...

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

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

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

  4. hdu 3974 Assign the task(dfs序上线段树)

    Problem Description There is a company that has N employees(numbered from 1 to N),every employee in ...

  5. HDU 3974 Assign the task(DFS序)题解

    题意:给出一棵树,改变树的一个节点的值,那么该节点及所有子节点都变为这个值.给出m个询问. 思路:DFS序,将树改为线性结构,用线段树维护.start[ ]记录每个节点的编号,End[ ]为该节点的最 ...

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

    题意:给定一棵树的公司职员管理图,有两种操作, 第一种是 T x y,把 x 及员工都变成 y, 第二种是 C x 询问 x 当前的数. 析:先把该树用dfs遍历,形成一个序列,然后再用线段树进行维护 ...

  7. HDU - 3974 Assign the task (DFS建树+区间覆盖+单点查询)

    题意:一共有n名员工, n-1条关系, 每次给一个人分配任务的时候,(如果他有)给他的所有下属也分配这个任务, 下属的下属也算自己的下属, 每次查询的时候都输出这个人最新的任务(如果他有), 没有就输 ...

  8. HDU 3974 Assign the task 并查集/图论/线段树

    Assign the task Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  9. HDU 3974 Assign the task(简单线段树)

    Assign the task Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. [DDCTF 2019]homebrew event loop

    0x00 知识点 逻辑漏洞: 异步处理导致可以先调用增加钻石,再调用计算价钱的.也就是先货后款. eval函数存在注入,可以通过#注释,我们可以传入路由action:eval#;arg1#arg2#a ...

  2. MySQL 表查询语句练习题

    MySQL 表查询语句练习题: 一.  设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表 ...

  3. 记录 TypeError: render() got an unexpected keyword argument 'renderer' 错误

    在网上看到MXShop这个项目,适合Python, Django + drf 进阶的,其中遇到 TypeError: render() got an unexpected keyword argume ...

  4. UVA 12663 第九届省赛 高桥与低桥 线段树

    题意很简单,n个桥的高度是事先给出来的,然后有m次涨水与落水的高度,问有多少座桥在这m次涨落之后 被淹超过了k次,如果某桥本身被水淹了,此时再涨水,就不能算多淹一次 看下数据10的五次方,10的五次方 ...

  5. 代码杂谈-or符号

    看到别人的代码里用了 or, 有点巧用. 记录一下. def func(a,b, context=None): # .... ctx = context or global_context() # . ...

  6. css选择器,选择指定属性的值

    选择属性为href的值: <a class='test' href='www.baidu.com' >test</a> response.css('.test::attr(hr ...

  7. one_day_one_linuxCmd---sz命令

    <坚持每天学习一个 linux 命令,今天我们来学习 sz && rz 命令> 前言:我们一般通过 ssh 客户端来进行远程登录和管理的,windows主机使用 ssh 登 ...

  8. 用Emoji和照片挑战大众点评,YOBO玩转新点评方式能引领潮流吗?

    对于一家企业来说,要想获得长久生命力的必备元素是什么?是技术底蕴和海量资金?但诺基亚.摩托罗拉和黑莓等巨头的崩塌,已经证明再稳固的基础都有可能只是沙子做的.是让人工智能.云计算.大数据等前沿技术赋能于 ...

  9. nginx_tcp_proxy代理酸酸乳

    一.安装低版本的nginx(高版本不支持tcp代理模块:nginx_tcp_proxy_module)Nginx默认只支持http反向代理,要支持tcp反向代理,需在编译时增加tcp代理模块[ngin ...

  10. Java - 记录String中intern()方法的学习与理解

    intern()方法:把堆中的引用丢入常量池中,然后返回这个引用.当常量池中已经存在这个引用,就直接返回这个引用.(jdk1.8) 由于jdk1.7中将字符串常量池改为存放在堆中,因此intern() ...