HDU3974-Assign the task

来源:互联网 发布:如何用ps制作淘宝首页 编辑:程序博客网 时间:2024/05/04 18:38

Assign the task

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


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 1C 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,每个员工都有自己的上司,自己下属的下属也是自己的下属。当给一个员工分配任务时,这个员工会把任务也分配到自己的所有下属。每个员工都只做最后一个被分配的任务,对于每个C x输出员工x正在做的任务,没有就输出-1。

解题思路:把员工的关系数建成并查集的结构,把每个直接分配任务的员工的任务和任务分配时间保存起来,查询时只要找这个员工所有父节点中最晚分配的任务


#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <cmath>#include <queue>#include <vector>#include <set>#include <stack>#include <map>#include <climits>using namespace std;#define LL long longconst int INF=0x3f3f3f3f;const int MAXN=50003;struct node{    int task,t;}x[MAXN];int f[MAXN];int main(){    int T,cas=0,n,m;    scanf("%d",&T);    while(T--)    {        printf("Case #%d:\n",++cas);        scanf("%d",&n);        int a,b;        for(int i=1;i<=n;i++) f[i]=i;        for(int i=1;i<n;i++)        {            x[i].t=x[i].task=0;            scanf("%d %d",&a,&b);            f[a]=b;        }        x[n].t=x[n].task=0;        scanf("%d",&m);        int tt,t=0,ans;        char ch[5];        while(m--)        {            scanf("%s %d",ch,&a);            if(ch[0]=='C')            {                tt=0;                while(a!=f[a])                {                    if(x[a].t>tt)                        tt=x[a].t,ans=x[a].task;                    a=f[a];                }                if(x[a].t>tt) ans=x[a].task,tt=x[a].t;                printf("%d\n",tt?ans:-1);            }            else            {                scanf("%d",&b);                x[a].task=b,x[a].t=++t;            }        }    }    return 0;}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 预售健身卡合法吗怎么办 被浓硫酸泼到怎么办 婴儿误喝了生水怎么办 宝宝喝了生水拉肚子怎么办 因妈妈喝生水宝宝拉肚子怎么办 喝了几口生水怎么办 不小心吃到蟑螂怎么办 吃了有蛆的樱桃怎么办 不小心误食了蛆怎么办 吃了有蟑螂的汤怎么办 调节天平时指针向右怎么办 香薰蜡烛融化了怎么办 香薰蜡烛挂壁怎么办y 粗蜡烛只烧中间怎么办 紫薯馒头变绿怎么办 小孩手开水烫了怎么办 被油烫伤了怎么办才不留疤 烫伤水泡蹭破了怎么办 烧伤的水泡破了怎么办 烧伤后水泡破了怎么办 烫伤泡破了红肿怎么办 烧伤第二天水泡破了怎么办? 烧伤后换药特别疼怎么办 盐酸溅到皮肤上怎么办 磷性磷酸酶高440怎么办 浓硫酸沾到皮肤上怎么办 浓硫酸溅到皮肤上怎么办 浓硫酸滴到皮肤上怎么办 浓硫酸洒在皮肤上怎么办 浓硫酸溅到眼睛里怎么办 盐酸弄到眼睛了怎么办 稀硫酸进眼睛里怎么办 草酸弄到皮肤上怎么办 大理石被盐酸烧发白怎么办 香薰蜡烛化了怎么办 吸入了大量燃烧纸气体怎么办 狗链条上锈了怎么办 思维迟钝反应慢嘴笨怎么办 小孩思维慢反应迟钝怎么办 苹果4g网络慢怎么办 医院没有号了怎么办啊