HDU 3974——Assign the task

来源:互联网 发布:衣服批发软件哪个好 编辑:程序博客网 时间:2024/06/06 02:50

Assign the task

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3489    Accepted Submission(s): 1434


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

Source

2011 Multi-University Training Contest 14 - Host by FZU 


题意:

n个人,每个人都有上级下级。(最底层员工没有下级,大boss没有上级。。)

m个操作:

T x  y 让x以及所有他的下属做工作y,如果在做其他工作马上改变做此工作

C x 查询第x个人在做什么工作。


分析:

首先员工之间关系成树形,一个根结点工作,则所有叶结点工作,但做的工作永远是最晚做的。我的做法是建立树,每个叶子结点记录他的父结点。

t x y时就记录x在i个输入做y。

c x就从x往根结点查找,寻找最后一项工作即可。


代码:

#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<cstdlib>#include<algorithm>#include<queue>#include<vector>#include<cmath>#define INF 0x3f3f3f3f#define EXP 0.00000001#define MOD 1e9+7#define MAXN 50005#define ltree 2*id,ll,mid#define rtree 2*id+1,mid+1,rr#define FO(i,n,m) for(int i=n;i<=m;++i)#define mem(a) memset(a,0,sizeof(a))typedef long long LL;using namespace std;struct node{    int pre;   //记录父结点    int lazy,time;}tree[MAXN];int n,m;int Query(int x){    int ans=tree[x].lazy;    int mt=tree[x].time;    while(tree[x].pre!=-1)    {        x=tree[x].pre;        if(tree[x].time>mt)        {            mt=tree[x].time;            ans=tree[x].lazy;        }    }    return ans;}int main(){    int t;    scanf("%d",&t);    FO(tt,1,t)    {        printf("Case #%d:\n",tt);        scanf("%d",&n);        FO(i,1,n)        {            tree[i].pre=-1;            tree[i].lazy=-1;            tree[i].time=0;        }        FO(i,1,n-1)        {            int u,v;            scanf("%d%d",&u,&v);            tree[u].pre=v;        }        int m;        scanf("%d",&m);        FO(i,1,m)        {            getchar();            char s;            scanf("%c",&s);            if(s=='C')            {                int x;                scanf("%d",&x);                printf("%d\n",Query(x));            }            else            {                int x,y;                scanf("%d%d",&x,&y);                tree[x].lazy=y;                tree[x].time=i;            }        }    }    return 0;}


原创粉丝点击