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 代码如下:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int MAXN = ;
int n,Q,t,topboss,cnt;
int head[MAXN],tot;
int start[MAXN],endd[MAXN];
bool used[MAXN];
struct Edge
{
int to,next;
}edge[MAXN];
void init()
{
cnt=;
tot=;
memset(head,-,sizeof head);
memset(used,false,sizeof used);
}
void addedge (int u,int v)
{
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void dfs(int u)
{
++cnt;
start[u]=cnt;
for (int i=head[u];i!=-;i=edge[i].next)
{
dfs(edge[i].to);
}
endd[u]=cnt;
}
struct Node
{
int l,r,val,lazy;
}segTree[MAXN<<];
void upDate_Same (int r,int v)
{
if (r)
{
segTree[r].val=v;
segTree[r].lazy=;
}
}
void push_down (int r)
{
if (segTree[r].lazy)
{
upDate_Same(r<<,segTree[r].val);
upDate_Same(r<<|,segTree[r].val);
segTree[r].lazy=;
}
}
void buildTree (int i,int l,int r)
{
segTree[i].l=l;
segTree[i].r=r;
segTree[i].val=-;
segTree[i].lazy=;
if (l==r)
return ;
int mid =(l+r)>>;
buildTree(i<<,l,mid);
buildTree(i<<|,mid+,r);
}
void update (int i,int l,int r,int v)
{
if (segTree[i].l==l&&segTree[i].r==r)
{
upDate_Same(i,v);
return ;
}
push_down(i);
int mid =(segTree[i].l+segTree[i].r)/;
if (r<=mid) update(i<<,l,r,v);
else if (l>mid) update(i<<|,l,r,v);
else
{
update(i<<,l,mid,v);
update(i<<|,mid+,r,v);
}
}
int query (int i,int u)
{
if (segTree[i].l==u&&segTree[i].r==u)
return segTree[i].val;
push_down(i);
int mid =(segTree[i].l+segTree[i].r)/;
if (u<=mid)
return query(i<<,u);
else
return query(i<<|,u);
}
int main()
{
//freopen("de.txt","r",stdin);
cin>>t;
int casee=;
while (t--){
printf("Case #%d:\n",++casee);
int u,v;
init();
scanf("%d",&n);
for (int i=;i<n-;++i){
cin>>u>>v;
used[u]=true;
addedge(v,u);
}
for (int i=;i<=n;++i){
if (!used[i]){
dfs(i);
break;
}
}
cin>>Q;
buildTree(,,cnt);
char op[];
while (Q--){
scanf("%s",op);
if (op[]=='C'){
scanf("%d",&u);
printf("%d\n",query(,start[u]));
}
else{
scanf("%d%d",&u,&v);
update(,start[u],endd[u],v);
}
}
}
return ;
}

700ms,有人200ms过了,正在研究。http://vjudge.net/contest/source/7108995            http://vjudge.net/contest/source/6308790

 

hdu 3974 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(线段树)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3974 题意:给定一棵树,50000个节点,50000个操作,C x表示查询x节点的值,T x y表示更 ...

  5. HDU 3974 Assign the task (DFS+线段树)

    题意:给定一棵树的公司职员管理图,有两种操作, 第一种是 T x y,把 x 及员工都变成 y, 第二种是 C x 询问 x 当前的数. 析:先把该树用dfs遍历,形成一个序列,然后再用线段树进行维护 ...

  6. HDU 3974 Assign the task

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

  7. HDU 3974 Assign the task (DFS序 + 线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 给你T组数据,n个节点,n-1对关系,右边的是左边的父节点,所有的值初始化为-1,然后给你q个操 ...

  8. 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 ...

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

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

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

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

随机推荐

  1. php ord()函数 语法

    php ord()函数 语法 作用:返回字符串的首个字符的 ASCII 值.直线电机生产厂家 语法:ord(string) 参数: 参数 描述 string 必须,要从中获得ASCII值的字符串 说明 ...

  2. 【FPGA】 007 --Verilog中 case,casez,casex的区别

    贴一个链接:http://www.cnblogs.com/poiu-elab/archive/2012/11/02/2751323.html Verilog中  case,casez,casex的区别 ...

  3. AGC033 D~F——[ 值放到角标的DP ][ 思路+DP ][ 思路 ]

    地址:https://atcoder.jp/contests/agc033/ D Complexity dp[ i ][ j ][ k ][ l ] 表示左上角是 ( i , j ) .右下角是 ( ...

  4. sql查询语句得到返回值fetchone()

    需求: 现在mysql中有一张表,表名是info,我想通过报案号4201820330114401021497在这张表里查询出它对应的id. sql = "select claim_id fr ...

  5. linux设备驱动第二篇:构造和运行模块

      上一篇介绍了Linux驱动的概念,以及linux下设备驱动的基本分类情况及其各个分类的依据和差异,这一篇我们来描述如何写一个类似hello world的简单测试驱动程序.而这个驱动的唯一功能就是输 ...

  6. (二)linux内核准备及编译

    1. 内核下载地址 linux内核网站,可以拿到最新的和最近的稳定版本内核: https://www.kernel.org/ 通过网站下载压缩包后解压或者使用git下载到本地: git clone h ...

  7. Chrome-逆向分析JS-2获取发送请求位置(以datatables获取表格数据为例)

    剧透:就是使用了一下 Chrome Source 的 XHR/fetch Breakpoints 功能,在发送请求时在该行进入断点调试. # 一:不认识一下 XHR/fetch Breakpoints ...

  8. 测开之路四十:jQuery基本用法

    从cdn引入jQuery库:https://www.bootcdn.cn/,搜索jQuery 在html里面(使用之前计算器的脚本),把复制的标签粘贴到引入js标签的前面:<script src ...

  9. cortable 使用方法

    星期一到星期六,早上六点到晚上六点.每隔两个小时 执行语句 0 6-18/2  * * 1-6 commond

  10. python学习笔记之数据类型、字符编码、文件处理

    1.数据类型 1.数字(int,float) 整形(int):定义 age=20  #本质age=int(20) 浮点类型:salary=3000.3 #本质salary=float(3000.3) ...