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. 《毛毛虫组》【Alpha】Scrum meeting 4

    第二天 日期:2019/6/17 1.1 今日完成任务情况以及遇到的问题. 今日完成任务情况: 货物入库管理模块设计: (1)对数据库表--tb_OutStore进行修改并完善: (2)学习trig_ ...

  2. checkbox点击选中,再点击取消,并显示在文本框中

    function checkItem(e,itemId) { var item = document.getElementById(itemId); var $items = $(item); if ...

  3. 一步一步构建iOS持续集成:Jenkins+GitLab+蒲公英+FTP

    什么是持续集成 持续集成是一种软件开发实践,即团队开发成员经常集成它们的工作,通过每个成员每天至少集成一次,也就意味着每天可能会发生多次集成.每次集成都通过自动化的构建(包括编译,发布,自动化测试)来 ...

  4. STM32三种启动模式 boot0 boot1

    STM32三种启动模式对应的存储介质均是芯片内置的,它们是: 1)用户闪存=芯片内置的Flash.2)SRAM=芯片内置的RAM区,就是内存啦.3)系统存储器=芯片内部一块特定的区域,芯片出厂时在这个 ...

  5. 【HIHOCODER 1589】回文子串的数量(Manacher)

    描述 给定一个字符串S,请统计S的所有|S| * (|S| + 1) / 2个子串中(首尾位置不同就算作不同的子串),有多少个是回文字符串? 输入 一个只包含小写字母的字符串S. 对于30%的数据,S ...

  6. JS中如何操作数组

    背景:随笔中所应用到的代码来自于上一篇随笔,MVC&JQuery如何根据List动态生成表格,部分代码不再重复. 代码如下: $("#btnTan").click(func ...

  7. Linux学习-Tarball 的管理与建议

    使用原始码管理软件所需要的基础软件 从原始码的说明我们晓得要制作一个 binary program 需要很多咚咚的呢!这包括底下这些基础的软件: gcc 或 cc 等 C 语言编译程序 (compil ...

  8. Linux学习-核心与核心模块

    谈完了整个开机的流程,您应该会知道,在整个开机的过程当中,是否能够成功的驱动我们主机的硬 件配备, 是核心 (kernel) 的工作!而核心一般都是压缩文件,因此在使用核心之前,就得要将他解 压缩后, ...

  9. hbase问题总结

    一.客户端访问hbase时出现no further information 使用java api访问hbase时,一直连不上,查看日志发现以下错误: java.net.ConnectException ...

  10. 基于百度OCR的图片文字识别

    先上图,有图有真相 首先在百度开通ORC服务,目前是免费的,普通识别每天50000次免费,非常棒! 百度文档:http://ai.baidu.com/docs#/OCR-API/top 下载百度SDK ...