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建树。有一个结论,如果v是u的祖先,那么dfs序st[v]<st[u]&&ed[v]>ed[u] 于是就可以建树了。然后就是打标记查标记
 #include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
#define FO freopen("in.txt","r",stdin);
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define debug(x) cout << "&&" << x << "&&" << endl;
#define lowbit(x) (x&-x)
#define mem(a,b) memset(a, b, sizeof(a));
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=;
const int inf = 0x3f3f3f3f;
ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
//head const int maxn=;
int _,lazy[maxn<<],st[maxn],ed[maxn],cur,m,vis[maxn],n;
vector<int> boss[maxn]; void dfs(int rt) {//建树
st[rt]=++cur;
for(int i=;i<boss[rt].size();i++) {
dfs(boss[rt][i]);
}
ed[rt]=cur;
} void pushdown(int rt) {
if(lazy[rt]!=-) {
lazy[rt<<]=lazy[rt];
lazy[rt<<|]=lazy[rt];
lazy[rt]=-;
}
} void build(int rt,int L,int R) {
lazy[rt]=-;
if(L==R) return;
int mid=(L+R)>>;
build(rt<<,L,mid);
build(rt<<|,mid+,R);
} void updata(int rt,int L,int R,int l,int r,int zhi) {
if(L>=l&&R<=r) {
lazy[rt]=zhi;
return;
}
pushdown(rt);
int mid=(L+R)>>;
if(l<=mid) updata(rt<<,L,mid,l,r,zhi);
if(r>mid) updata(rt<<|,mid+,R,l,r,zhi);
} int query(int rt,int L,int R,int pos) {
if(L==R) return lazy[rt];//单点查
pushdown(rt);
int mid=(L+R)>>;
if(pos<=mid) query(rt<<,L,mid,pos);
else query(rt<<|,mid+,R,pos);
} int curr=;
int main() {
for(scanf("%d",&_);_;_--) {
printf("Case #%d:\n",curr++);
cur=;
mem(boss,);
mem(vis,);
scanf("%d",&n);
int u,v;
rep(i,,n) {//存关系
scanf("%d%d",&u,&v);
boss[v].push_back(u);
vis[u]=;
}
rep(i,,n+) {//找到根
if(!vis[i]) {
dfs(i);
break;
}
}
build(,,cur);//建树
scanf("%d",&m);
char s[];
int pos,zhi;
while(m--) {
scanf("%s",s);
if(s[]=='T') {
scanf("%d%d",&pos,&zhi);
updata(,,cur,st[pos],ed[pos],zhi);//区间st[pos]-ed[pos]是pos的员工
} else {
scanf("%d",&pos);
printf("%d\n",query(,,cur,st[pos]));//查pos的任务(ed[pos]就不是了)
}
}
}
}

kuangbin专题七 HDU3974 Assign the task (dfs时间戳建树)的更多相关文章

  1. HDU3974 Assign the task —— dfs时间戳 + 线段树

    题目链接:https://vjudge.net/problem/HDU-3974 There is a company that has N employees(numbered from 1 to ...

  2. hdu3974 Assign the task dfs序+线段树

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

  3. HDU3974 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(DFS序+线段树单点查询,区间修改)

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

  5. [Assign the task][dfs序+线段树]

    http://acm.hdu.edu.cn/showproblem.php?pid=3974 Assign the task Time Limit: 15000/5000 MS (Java/Other ...

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

  7. HDU-3974 Assign the task(多叉树DFS时间戳建线段树)

    http://acm.hdu.edu.cn/showproblem.php?pid=3974 Time Limit: 15000/5000 MS (Java/Others)    Memory Lim ...

  8. hdu3974 Assign the task线段树 dfs序

    题意: 无序的给编号为1-n的员工安排上下级, 操作一:给一个员工任务C,则该员工以及他的下级任务都更换为任务C 操作二:询问一个员工,返回他的任务   题解: 给一个员工任务,则他所在组都要改变,联 ...

  9. hdu3974 Assign the task【线段树】

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

随机推荐

  1. Win7无法访问Windows共享文件夹

    解决方法如下 On the Windows 7 machine: Run secpol.msc Drill down through Local Policies | Security Options ...

  2. 视频直播技术-视频-编码-传输-秒开等<转>

    转载地址:http://mp.weixin.qq.com/s?__biz=MzAwMDU1MTE1OQ==&mid=2653547042&idx=1&sn=26d8728548 ...

  3. Winform状态栏控件中Label靠右显示的方法

    设计器:   代码: 在Form_Load事件中添加 : statusStripMain.LayoutStyle= ToolStripLayoutStyle.HorizontalStackWithOv ...

  4. web网页 页面布局的几种方式(转)

    web网页 页面布局的几种方式 转载 2017年06月16日 12:19:40 2218 网页基本布局方式: (1)流式布局 Fluid 流布局与固定宽度布局基本不同点 就在于对网站尺寸的侧量单位不同 ...

  5. Python03 字符串类型、强制类型转化、列表、元组、字典、集合

    1 字符串类型 在python中字符串类型用str表示,字符串的连接用 + 1.1 创建字符串对象 ·创建一个字符串对象有两种方式,一种方式是直接用字符串进行赋值,另外一种是利用str类实例化对象:具 ...

  6. 旋转矩阵(Rotate Matrix)的性质分析

    博客转载自:http://www.cnblogs.com/caster99/p/4703033.html 学过矩阵理论或者线性代数的肯定知道正交矩阵(orthogonal matrix)是一个非常好的 ...

  7. Halcon将裁剪后的图像还原为原始大小

    ************************************************************* * Halcon将裁剪后的图像还原为原始大小 * Author: LiGua ...

  8. C#调用C++类库的几种方式

    1.  直接调用C++类库中的公共方法 使用DllImport特性对方法进行调用,比如一个C++类库SampleCppWrapper.dll中的公共方法: extern "C" _ ...

  9. 前端学习笔记2017.6.12 DIV布局网页

    DIV的功能就是把网页划分成逻辑块的. 看下豆瓣东西页面的布局,我们来分析下. 按照先从上到下的原则,把这个页面分成几个块: 首先是最顶端的这个条,这是一个DIV,我们给它起个名字,叫banner 然 ...

  10. R: 正则表达式

    正则表达式: 例:sub("a","",c("abcd","dcba")):   [1] "bcd" ...