hdu 3974 Assign the task
2015-08-03 17:00
369 查看
http://acm.hdu.edu.cn/showproblem.php?pid=3974
Total Submission(s): 1113 Accepted Submission(s): 527
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
Sample Output
首先你要理解题意,这道题就是说在一个公司里,有领导和下属之分,给某一个员工派发任务之后,领导就会将任务派发给他的下属,知道任务被派发到最下层,这样一级一级的下发,问题是,对于任意一个员工的话,请你输出他现在在做的任务,如果没做就输出-1。这道题从题中需要注意的是从老板到下属是一个树,也就是说对于一个员工来说的话,他不可能有多个直接上司,当然可以有间接的,比如A是B的上司,C是A的上司,可以推出C也是B的上司,这一点儿很关键的,形成的是树,所以我们首先解决的是你的数据怎么来存的问题?我们可以用一维数组来存领导和下属的关系,来记住每个员工的直接领导是谁,这样其实这个数组就是一个树了,然后怎样解决任务的更新,因为题中说,对于每个员工当他接到新的任务时就会停下手中的任务而做新的任务,这样我想到用结构体了,用task来记住任务的名字,num来标记当前的任务数,num始终是需要更新的,这样才能保证输出的值是我们想要的当前的任务;
Assign the task
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1113 Accepted Submission(s): 527
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
首先你要理解题意,这道题就是说在一个公司里,有领导和下属之分,给某一个员工派发任务之后,领导就会将任务派发给他的下属,知道任务被派发到最下层,这样一级一级的下发,问题是,对于任意一个员工的话,请你输出他现在在做的任务,如果没做就输出-1。这道题从题中需要注意的是从老板到下属是一个树,也就是说对于一个员工来说的话,他不可能有多个直接上司,当然可以有间接的,比如A是B的上司,C是A的上司,可以推出C也是B的上司,这一点儿很关键的,形成的是树,所以我们首先解决的是你的数据怎么来存的问题?我们可以用一维数组来存领导和下属的关系,来记住每个员工的直接领导是谁,这样其实这个数组就是一个树了,然后怎样解决任务的更新,因为题中说,对于每个员工当他接到新的任务时就会停下手中的任务而做新的任务,这样我想到用结构体了,用task来记住任务的名字,num来标记当前的任务数,num始终是需要更新的,这样才能保证输出的值是我们想要的当前的任务;
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <cstdlib> #include <limits> #include <queue> #include <stack> #include <vector> #include <map> using namespace std; #define N 50005 #define INF 0xfffffff #define PI acos (-1.0) #define EPS 1e-8 #define Lson rt<<1, l, tree[rt].mid () #define Rson rt<<1|1, tree[rt].mid () + 1, r struct node { int task, num; }stu ; int main () { int t, a , flag = 1; scanf ("%d", &t); while (t--) { int n, m; scanf ("%d", &n); memset (a, -1, sizeof (a)); int x, y; for (int i=1; i<n; i++) { scanf ("%d%d", &x, &y); a[x] = y; } for (int i=1; i<=n; i++) stu[i].task = stu[i].num = -1; char s[10]; int cnt = 0; printf ("Case #%d:\n", flag++); scanf ("%d", &m); while (m--) { scanf ("%s", s); if (s[0] == 'T') { scanf ("%d%d", &x, &y); stu[x].task = y; stu[x].num = ++cnt; } else { scanf ("%d", &x); y = stu[x].task; int tt = stu[x].num; while (x != -1) { if (stu[x].num > tt) { y = stu[x].task; tt = stu[x].num; } x = a[x]; } printf ("%d\n", y); } } } return 0; }
相关文章推荐
- 47.关于gradle的解疑
- 总结CSS3新特性(Animation篇)
- hdu 3917 Road constructions 最大权闭合子图
- php构造函数连接数据库
- LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- Java学习笔记------如何为文本输入框设置下凹效果?
- 查看linux版本的命令
- hdu 2102 A计划 BFS
- 解决Nginx: [error] open() "/usr/local/Nginx/logs/Nginx.pid
- SpringMVC学习笔记
- Android之MVC模式
- 使用Xcode7非美刀购买开发者帐号,非越狱安装IOS ipa
- js调用oc方法 UIWebView 跳 到另一个UIWebView
- Asp.net 5学习
- node.js学习笔记
- Maven - error in opening zip file
- PHP-FPM之Chroot执行环境详解
- spring mvc 环境搭建
- 传说中的网络流24题
- Hibernate、Mybait,Mysql、Postgresql适用场景