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.

InputThe 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)OutputFor 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 用dfs序来把这棵树化成线性的结构,然后线段树的单点修改和区间查询即可 用Start和End记录每个点所代表区间的起点和终点
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int head[maxn], Start[maxn], End[maxn], flag[maxn];
int a, b, x, y, ans, cnt, ret; struct node{
int l, r, w, f;
}Node[maxn*]; struct dot{
int v, next;
}Dot[maxn]; void add(int u, int v)
{
Dot[ret].v = v;
Dot[ret].next = head[u];
head[u] = ret++;
} void dfs(int u)
{
++cnt;
Start[u] = cnt;
for(int i=head[u]; i!=-; i=Dot[i].next)
dfs(Dot[i].v);
End[u] = cnt;
} void build(int k, int ll, int rr)
{
Node[k].l = ll, Node[k].r = rr;
Node[k].w = -;
Node[k].f = ;
if(ll == rr) return;
int m = (ll + rr) / ;
build(k*, ll, m);
build(k*+, m+, rr);
} void down(int k)
{
Node[k*].f = Node[k].f;
Node[k*+].f = Node[k].f;
Node[k*].w = Node[k].f;
Node[k*+].w = Node[k].f;
Node[k].f = ;
} void qp(int k)
{
if(Node[k].l == Node[k].r)
{
ans = Node[k].w;
return;
}
if(Node[k].f) down(k);
int m = (Node[k].l + Node[k].r) / ;
if(a <= m) qp(k*);
else qp(k*+);
} void chinter(int k)
{
if(Node[k].l >= a && Node[k].r <= b)
{
Node[k].w = y;
Node[k].f = y;
return;
}
if(Node[k].f) down(k);
int m = (Node[k].l + Node[k].r) / ;
if(a <= m) chinter(k*);
if(b > m) chinter(k*+);
} int main()
{
int T, kase = ;
scanf("%d",&T);
while(T--)
{
mem(head, -);
mem(flag, -);
mem(Start, -);
mem(End, -);
mem(Dot, );
mem(flag, -);
ret = ;
cnt = ;
int n, m, se;
printf("Case #%d:\n",++kase);
scanf("%d",&n);
for(int i=; i<n; i++)
{
int u, v;
scanf("%d%d", &u, &v);
flag[u] = v;
se = v;
add(v, u);
}
while(flag[se] != -)
se = flag[se];
dfs(se);
build(, , cnt);
scanf("%d",&m);
char str[];
getchar();
for(int i=; i<m; i++)
{
scanf("%s",str);
if(strcmp(str, "C") == )
{
int p;
ans = -;
scanf("%d",&p);
a = Start[p];
qp();
printf("%d\n",ans);
}
else if(strcmp(str, "T") == )
{
scanf("%d%d", &x, &y);
a = Start[x];
b = End[x];
chinter();
}
} } return ;
}

Assign the task HDU - 3974(dfs序+线段树)的更多相关文章

  1. Assign the task HDU - 3974 (dfs序 + 线段树)

    有一家公司有N个员工(从1到N),公司里每个员工都有一个直接的老板(除了整个公司的领导).如果你是某人的直接老板,那个人就是你的下属,他的所有下属也都是你的下属.如果你是没有人的老板,那么你就没有下属 ...

  2. hdu 5692(dfs序+线段树,好题)

    Snacks Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  3. HDU 5877 [dfs序][线段树][序]

    /* 题意: n个点的树,每个点给定一个权值,给定一个k,求任意一点的子树中,权值小于k/该点权值的点共有多少个. 思路: 1.很明显的子树的操作,应用dfs序. 2.比赛的时候傻逼了,一直在调划分树 ...

  4. hdu 3974 dfs时间戳+线段树

    题意: 一个公司里面每个员工都有一个顶头上司,一旦给某个员工分配任务后,这个员工以及该员工的所有下属都在做该任务. 有若干操作,分配给员工任务以及查询该员工正在执行的任务. 题解: 典型的更新字树的操 ...

  5. HDU 5692 (DFS序+线段树)

    DFS获得从0到每一个顶点的距离,同时获得L和R数组.两数组为遍历时从i进入再从i出来的序列. #pragma comment(linker, "/STACK:1024000000,1024 ...

  6. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  7. Codeforces Round #442 (Div. 2)A,B,C,D,E(STL,dp,贪心,bfs,dfs序+线段树)

    A. Alex and broken contest time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  8. CodeForces 877E Danil and a Part-time Job(dfs序+线段树)

    Danil decided to earn some money, so he had found a part-time job. The interview have went well, so ...

  9. Snacks HDU 5692 dfs序列+线段树

    Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...

  10. Educational Codeforces Round 6 E dfs序+线段树

    题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...

随机推荐

  1. 一次学生时代的经历,利用Python在机房杀红蜘蛛,脱离老师控制!

    这个为什么说是一次学生时代的经历呢,我的出发点并没有是为了吊胃口.确实,这个Python小应用,只能在学生时代用得着吧,尤其是高中和大学,如果你没有想到也没关系,看完我下面说的就会明白了.   在这里 ...

  2. python3通过gevent.pool限制协程并发数量

    协程虽然是轻量级的线程,但到达一定数量后,仍然会造成服务器崩溃出错.最好的方法通过限制协程并发数量来解决此类问题. server代码: #!/usr/bin/env python # -*- codi ...

  3. C#一些常用的图片操作方法:生成文字图片 合并图片等

    生成文字图片: /// <summary> /// 生成文字图片 /// </summary> /// <param name="text">& ...

  4. LeetCode Pow(x, n) (快速幂)

    题意 Implement pow(x, n). 求X的N次方. 解法 用正常的办法来做是会超时的,因为可能有21亿次方的情况,所以需要优化一下.这里用到了快速幂算法,简单来说就是将指数分解成二进制的形 ...

  5. [UWP 自定义控件]了解模板化控件(2.1):理解ContentControl

    UWP的UI主要由布局容器和内容控件(ContentControl)组成.布局容器是指Grid.StackPanel等继承自Panel,可以拥有多个子元素的类.与此相对,ContentControl则 ...

  6. python-scapy学习笔记-(1)

    主要功能函数sniff sniff(filter="",iface="any",prn=function,count=N) filter参数允许我们对Scapy ...

  7. shell脚本之特殊符号总结性梳理

    # 井号 (comments) 这几乎是个满场都有的符号#!/bin/bash 井号也常出现在一行的开头,或者位于完整指令之后,这类情况表示符号后面的是注解文字,不会被执行. # This line ...

  8. Proxy基础---------获取collection接口的构造跟方法

    1----查看proxy api 2------测试代码 package cn.proxy01; import java.lang.reflect.Constructor; import java.l ...

  9. 第三个sprint冲刺第一阶段

  10. Magazine Ad CodeForces - 803D (二分+贪心)

    The main city magazine offers its readers an opportunity to publish their ads. The format of the ad ...