hdu3974

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.

InputThe 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)OutputFor 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

1
5
4 3
3 2
1 3
5 2
5
C 3
T 2 1
 C 3
T 3 2
C 3

Sample Output

Case #1:
-1
1
2

译文:

有一个公司有N个员工(从1到N),公司里的每个员工都有一个直接的老板(除了整个公司的领导),如果你是某人的顶头上司,那个人就是你的下属,他的下属也都是你的下属。如果你不是任何人的老板,那么你就没有下属,没有直接上司的员工是整个公司的领导,这就意味着N员工组成了一棵树。
公司通常会指派一些任务给一些员工完成,当分配给某人的任务时,他/她会把它分配给所有的下属,换句话说,这个人和他/她的下属同时接受一项任务。此外,每当员工收到任务时,他/她将停止当前任务(如果他/她有)并启动新任务。
写,有助于在公司给某个员工分配任务后,计算出员工的当前任务。

题解:这道题不知道为什么归类为线段树这一类里面,感觉没什么关系,就是模拟吧,好像就过了,向上找直到最终祖先为止,

然后找时间戳最迟

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<cstring>
 6 const int MAXN=50007;
 7 char s[20];
 8 int num,fa[MAXN],zhi[MAXN],now[MAXN],u,v,t,ans,mm,x,y,n,q,fzy=0;
 9 using namespace std;
10 void query(int x)
11 {
12     if (x==-1) return;
13     if (zhi[x]!=-1&&now[x]>mm)
14     {
15         mm=now[x];
16         ans=zhi[x];
17     }
18     query(fa[x]);
19 }
20 int main()
21 {
22     scanf("%d",&t);
23     while (t--)
24     {
25         printf("Case #%d:\n",++fzy);
26         memset(fa,-1,sizeof(fa));
27         memset(zhi,-1,sizeof(zhi));
28         memset(now,-1,sizeof(now));
29         num=0;
30         scanf("%d",&n);
31         for (int i=1,v,u;i<n;i++)
32         {
33             scanf("%d%d",&v,&u);
34             fa[v]=u;
35         }
36         scanf("%d",&q);
37         for (int i=1;i<=q;i++)
38         {
39             scanf("%s",&s);
40             if (s[0]==‘C‘)
41             {
42                 scanf("%d",&x);
43                 ans=-1;mm=-10;
44                 query(x);
45                 printf("%d\n",ans);
46             }
47             else
48             {
49                 scanf("%d%d",&x,&y);
50                 zhi[x]=y;
51                 now[x]=++num;
52             }
53         }
54     }
55 }

的。

时间: 2024-11-03 20:45:57

hdu3974的相关文章

HDU3974 Assign the task(多叉树转换为线段+线段树区间染色)

题目大意:有n个人,给你他们的关系(老板和员工),没有直属上司的人就是整个公司的领导者,这意味着n个人形成一棵树(多叉树).当一个人被分配工作时他会让他的下属也做同样的工作(并且立即停止手头正在做的工作),题目会询问你其中某个人正在做的工作. 解题思路:其实从"一个人分配他的下属做一样的工作"这里就可以看出来了,这相当于让一块区间的人都做一样的事,就是线段树区间染色问题.但不能使用线段树,要先将多叉树铺展开,将节点映射到线段上.把每个人的管理区段找出来(把属于同一个人管的放一起,上司放

hdu3974(线段树+dfs)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 题意:给定点的上下级关系,规定如果给i分配任务a,那么他的所有下属.都停下手上的工作,开始做a. 操作 T x y 分配x任务y,C x询问x的当前任务: 分析:dfs将每个节点以下的子孙节点重新编号映射到一条线段上,再相应地区间修改,单点查询. #pragma comment(linker,"/STACK:102400000,102400000") #include <cst

HDU3974(树的遍历)

Assign the task Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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

kb-07线段树--10--dfs序建树

1 /* 2 hdu3974 3 dfs序建树,然后区间修改查询: 4 */ 5 #include<iostream> 6 #include<cstdio> 7 #include<cstring> 8 #include<algorithm> 9 #define MAX_N 50005 10 using namespace std; 11 12 int N,M; 13 struct Node 14 { 15 int to,next; 16 }edge[MAX_

hdu3974----Assign the task

Assign the task Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 780    Accepted Submission(s): 392 Problem Description There is a company that has N employees(numbered from 1 to N),every emplo

线段树+dfs序(Apple Tree )(Assign the task )

线段树+dfs序 给定一棵n个节点的树,m次查询,每次查询需要求出某个节点深度为h的所有子节点. 作为预处理,首先将树的所有节点按深度保存起来,每个深度的所有节点用一个线性结构保存,每个深度的节点相对顺序要和前序遍历一致. 然后从树的根节点进行dfs,对于每个节点记录两个信息,一个是dfs进入该节点的时间戳in[id],另一个是dfs离开该节点的时间戳out[id]. 最后对于每次查询,求节点v在深度h的所有子节点,只需将深度为h并且dfs进入时间戳在in[v]和out[v]之间的所有节点都求出

HDU - 3974 Assign the task (线段树区间修改+构建模型)

https://cn.vjudge.net/problem/HDU-3974 题意 有一棵树,给一个结点分配任务时,其子树的所有结点都能接受到此任务.有两个操作,C x表示查询x结点此时任务编号,T x y表示给x结点分配编号为y的任务. 分析 题目读起来就很有区间修改的味道,将一个区间变为一个值.问题在于怎么把这棵树对应到区间上. 对于一个结点,其控制的范围是它的子树,对应区间范围可以看作是以dfs序表示的区间.好像有点绕..就是给每个结点再对应一个dfs序,然后在dfs时把这个点控制的子树看