Assign the task

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

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
题意:一棵树,父亲节点是其子节点boss,现在要分配工作,分配给树的某个节点,若分配给该节点,该节点的所有儿子节点都会分配到这个任务,若之前这些节点有其他的任务,那么放下以前的任务,马上开始做当前分配下来的任务。
现在给出两种操作,一种就是分配任务给某个节点,还有一种操作是查询某个节点当前在做的任务并输出之。
思路:每次的修改操作可以是层序遍历进行树的局部修改即可。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
const int N_MAX = +;
vector<int>G[N_MAX];
queue<int>que;
int cur_work[N_MAX];
int print[N_MAX];
int N,M; void ceng_xu(int emp,int y) {
que.push(emp);
while (!que.empty()) {
int emp = que.front();
que.pop();
cur_work[emp] = y;
for (int i = ; i < G[emp].size(); i++) {
que.push(G[emp][i]);
}
}
} int main() {
int t,k=;
scanf("%d",&t);
while (t--) {
int num = ;//用于记录print赋值的次数
k++;//测试样本数
memset(cur_work,-,sizeof(cur_work));
scanf("%d",&N);
for (int i = ; i < N - ; i++) {
int emp, boss;//下标都从1开始
scanf("%d%d",&emp,&boss);
G[boss].push_back(emp);
}
scanf("%d",&M);
for (int i = ; i < M; i++) {
char c;
scanf(" %c",&c);
if (c == 'C') {
int a;
scanf("%d",&a);
print[num++]=cur_work[a];
}
else {
int emp, y;
scanf("%d%d",&emp,&y);
ceng_xu(emp, y);
}
}
for (int i = ; i < N; i++) {
G[i].clear();
} printf("Case #%d:\n",k);
for (int i = ; i < num; i++)
printf("%d\n",print[i]);
}
return ;
}

hdu 3874 Assign the task的更多相关文章

  1. HDU 3974 Assign the task 并查集/图论/线段树

    Assign the task Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  2. HDU 3974 Assign the task 暴力/线段树

    题目链接: 题目 Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...

  3. HDU 3974 Assign the task(简单线段树)

    Assign the task Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. HDU 3974 Assign the task

    Assign the task Problem Description There is a company that has N employees(numbered from 1 to N),ev ...

  5. HDU 3947 Assign the task

    http://acm.hdu.edu.cn/showproblem.php?pid=3974 Problem Description There is a company that has N emp ...

  6. HDU 3974 Assign the task(DFS序+线段树单点查询,区间修改)

    描述There is a company that has N employees(numbered from 1 to N),every employee in the company has a ...

  7. hdu 3974 Assign the task (线段树+树的遍历)

    Description There is a company that has N employees(numbered from 1 to N),every employee in the comp ...

  8. hdu 3974 Assign the task(dfs序上线段树)

    Problem Description There is a company that has N employees(numbered from 1 to N),every employee in ...

  9. HDU 3974 Assign the task(dfs建树+线段树)

    题目大意:公司里有一些员工及对应的上级,给出一些员工的关系,分配给某员工任务后,其和其所有下属都会进行这项任务.输入T表示分配新的任务, 输入C表示查询某员工的任务.本题的难度在于建树,一开始百思不得 ...

随机推荐

  1. SQLServer死锁

    死锁的四个必要条件:互斥条件(Mutual exclusion):资源不能被共享,只能由一个进程使用.请求与保持条件(Hold and wait):已经得到资源的进程可以再次申请新的资源.非剥夺条件( ...

  2. 解读express框架

    #解读Express 框架 1. package.json文件:express工程的配置文件 2. 为什么可以执行npm start?相当于执行 node ./bin/www "script ...

  3. string 空值

    string str; string mystr = ""; 则 str == mystr;

  4. Cordova 本地项目创建方法

    l  创建项目 需要在终端上输入:cordova create [目录][项目ID][APP名称] 运行:cordova create hello com.example.hello hello 将在 ...

  5. bootstrap 超大屏幕(Jumbotron)

    本章将讲解Bootstrap的一个特性:超大屏幕(Jumbonron),顾名思义该组件可以增加标题的大小,并为登录页面的内容添加更多的外边矩. 使用超大屏幕的步骤如下: 1.创建一个还有class.j ...

  6. OC和C++的混用1

    //Objective-C类 /*在混用之前需要做一步非常重要的事:不是代码而是编译器选项,在做混合编译之前一定要把编译器的Compile Sources As选项改为Objective C++. 修 ...

  7. iOS 绘制1像素的线

    一.Point Vs Pixel iOS中当我们使用Quartz,UIKit,CoreAnimation等框架时,所有的坐标系统采用Point来衡量.系统在实际渲染到设置时会帮助我们处理Point到P ...

  8. 2019.5.18-5.19 ACM-ICPC 全国邀请赛(西安)赛后总结

    第一次出去比赛经验太少了!!!果然最大目的是长见识和受刺激Orz 以下流水账: 背了本两千两百页的牛津高阶英汉双解词典,背了吃的,背了衣服……以后这些东西统统不带,买本口袋词典即可.上述物品这次比赛全 ...

  9. Docker 容器的数据管理

    docker 容器的数据卷 什么是数据卷(DataVolume) 数据卷是经过特殊计的目录,可以绕过联合文件系统(UFS),为一个或多个容器提供访问. 数据卷设计的目的,在于数据的永久化,它完全独立与 ...

  10. Java-JFrame可视化开发

    Java-JFrame可视化开发的一般步骤 JFrame可以做出类似于QQ登录功能的窗体,通过JFrame可以利用Java代码实现窗体功能,一般用于CS项目的C(客户端)的开发: 利用JFrame可以 ...