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.
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.
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)
1 5 4 3 3 2 1 3 5 2 5 C 3 T 2 1 C 3 T 3 2 C 3
Case #1: -1 1 2
在一个公司内有N个员工,他们都有自己的直接上司和直接下属,如果没有老板,那么他就是公司的大boss(董事长),
如果没有下属,那么他就最底层的劳动人民,对于一个中层干部来说,他的上司的上司也是自己的上司,他的下属的下属
也是自己的下属
对于被安排的任务,当领导的总是不愿意自己干,因此,要和自己的下属一起干,下属接到新任务,总是把手里的任务放下
来干新任务
有两种操作 T 操作 :输入a,b 把任务b交给a来干
C 操作 : 输入 a, 询问a当前干的任务,如果没有任务输出-1,如果有任务,输出任务编号
思路:
这道题,我是在做线段树的训练中遇到的,当时一看存在上下属关系,很明显用并查集
但是又有区间的更新,和点查找,想用线段树做,但是区间又不是连续的,没办法下手
最后,还是选择了并查集
代码:
#include<stdio.h>
#include<string.h>
#define MAX 50005
using namespace std;
struct node{
int tesk; //记录任务种类 没有任务为-1
int boss; //记录自己的上司
}tree[MAX];
int n,v,t;
int time[MAX]; //记录接到任务的员工,在整个公司是第几个接到的
void init(){ //初始化
int i;
for(i=0;i<=n;i++)
tree[i].boss=i;
}
void mix(int a,int b){ //a的上司是b
tree[a].boss=b;
}
int Find(int x){ //寻找x的最大上司(即公司的老板)
while(x!=tree[x].boss){ //在寻找过程中查看x的上司们谁接到的任务最晚
if(t<time[x]){ //最晚的任务也就是x要完成的新任务
t=time[x];
v=tree[x].tesk;
}
x=tree[x].boss;
}
if(t<time[x]){
t=time[x];
v=tree[x].tesk;
}
return x;
}
int main(){
int T;
scanf("%d",&T);
int cnt=0;
while(T--){
printf("Case #%d:\n",++cnt);
memset(tree,-1,sizeof(tree)); //初始化
memset(time,0,sizeof(time));
scanf("%d",&n);
init();
int i;
for(i=0;i<n-1;i++){
int a,b;
scanf("%d%d",&a,&b);
mix(a,b);
}
int k=0;//记录公司内总的任务数,k越大说明接到的任务越晚
int m;
scanf("%d",&m);
for(i=0;i<m;i++){
char op[5];
scanf("%s",op);
if(op[0]=='T'){ //把b任务交给a
int a,b;
scanf("%d%d",&a,&b);
tree[a].tesk=b;
time[a]=k++;
}
else{
int a;
scanf("%d",&a);
t=-1;//初始化为-1
v=tree[a].tesk;//将a的任务设为初始任务
Find(a);
printf("%d\n",v);
}
}
}
return 0;
}