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. 获取kafka的lag, offset, logsize的shell和python脚本

    python脚本 #!/usr/bin/env python import os import re import sys group_id=sys.argv[1] pn=sys.argv[2] ka ...

  2. Bootstrap历练实例:导航内的下拉菜单

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  3. (转发)IOS高级开发~Runtime(四)

    用C代替OC: #import <objc/runtime.h> #import <objc/message.h> #import <stdio.h> extern ...

  4. HDU 5025 Saving Tang Monk(状态转移, 广搜)

    #include<bits/stdc++.h> using namespace std; ; ; char G[maxN][maxN], snake[maxN][maxN]; ]; int ...

  5. jmeter throughput controller

    工作方式:可以按规定次数执行,也可以选择按百分比执行,其中的百分比必须是10,20,30类似的整数. 使用场景:可以随机的去按百分比浏览网址. 以下是具体脚本:

  6. noip2017行记

    前奏 写了所有的变量名在linux下测,结果发现并没什么用...听说将所有的变量加上下划线,加上自己的名字作为前缀.. lgj,“感觉今年有网络流,今年要立个flag”,zjr“你咋不上天儿” 在车上 ...

  7. Solr 7 部署与使用踩坑全记录

    前言 Solr 是一种可供企业使用的.基于 Lucene 的搜索服务器,它支持层面搜索.命中醒目显示和多种输出格式.在这篇文章中,我将介绍 Solr 的部署和使用的基本操作,希望能让初次使用的朋友们少 ...

  8. 大数据学习——SparkStreaming整合Kafka完成网站点击流实时统计

    1.安装并配置zk 2.安装并配置Kafka 3.启动zk 4.启动Kafka 5.创建topic [root@mini3 kafka]# bin/kafka-console-producer. -- ...

  9. dependency or constituency

    what's dependenct or constituency involved in a sentence? In linguistics, when it comes to sentence ...

  10. 2018省赛赛第一次训练题解和ac代码

    第一次就去拉了点思维很神奇的CF题目 2018省赛赛第一次训练 # Origin Title     A CodeForces 607A Chain Reaction     B CodeForces ...