kuangbin专题七 HDU3974 Assign the task (dfs时间戳建树)
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时间戳建树)的更多相关文章
- HDU3974 Assign the task —— dfs时间戳 + 线段树
题目链接:https://vjudge.net/problem/HDU-3974 There is a company that has N employees(numbered from 1 to ...
- 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 ...
- HDU3974 Assign the task
Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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 ...
- [Assign the task][dfs序+线段树]
http://acm.hdu.edu.cn/showproblem.php?pid=3974 Assign the task Time Limit: 15000/5000 MS (Java/Other ...
- 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 ...
- HDU-3974 Assign the task(多叉树DFS时间戳建线段树)
http://acm.hdu.edu.cn/showproblem.php?pid=3974 Time Limit: 15000/5000 MS (Java/Others) Memory Lim ...
- hdu3974 Assign the task线段树 dfs序
题意: 无序的给编号为1-n的员工安排上下级, 操作一:给一个员工任务C,则该员工以及他的下级任务都更换为任务C 操作二:询问一个员工,返回他的任务 题解: 给一个员工任务,则他所在组都要改变,联 ...
- 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 ...
随机推荐
- JAVA基础知识总结11(异常)
异常: 就是不正常.程序在运行时出现的不正常情况.其实就是程序中出现的问题.这个问题按照面向对象思想进行描述,并封装成了对象.因为问题的产生有产生的原因.有问题的名称.有问题的描述等多个属性信息存在. ...
- IE6中浮动双边距bug
想要创建出漂亮的网页设计, 除了要认真学习每一个html和CSS代码之外,不可能不去了解一下臭名昭著的IE6和更早的那些IE浏览器的坏脾气,因为你本来写出的规规矩矩的代码, 漂亮的设计就此就要完成了, ...
- contentvalue的探究(结构,用途)
contentvalue类似HASHMAP,但是KEY只能为STRING 该类用于数据库操作时对数据的封装,可以避免使用SQL语句,为后期创建CONTENTPROVIDER提供便利. 如果没有上述需求 ...
- auth 权限控制
一. 权限介绍所谓权限控制,大部分是在管理后台上使用.比如超级管理员登录,会得到所有操作的控制权:认证专员,只能给会员做认证操作:审核专员,只能操作内容的审核.删除.加精等操作,以此类推.那么 Thi ...
- cocos2dx帧动画
//帧动画的创建 //方式一,通过多张图片来创建 auto sprite1 = Sprite::create("grossini_dance_05.png"); sprite1-& ...
- Cocos2d-x 网络编程
主要介绍内容:Http协议,Socket协议,webSocket协议, Cocos2d-x中的相关类和方法 1 Http协议 HTTP协议也叫超文本传输协议.是互联网广泛使用的通信协议,常用于B/S架 ...
- 蓝桥杯【入门训练】 Fibonacci数列
问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少. 输入格式 输入包含一个整数n ...
- mysql join查询的on 与 where 的不同点
on总数以left join的左表为准,where会过滤掉,不符合where条件的数据
- STM32 C++编程 004 Adc (数模转换)类
使用 C++ 语言给 STM32 编写一个 Adc 类 我使用的STM32芯片:STM32F103ZET6 我们使用的STM32库版本:V3.5.0 注意: 想学习本套 STM32 C++编程 的专栏 ...
- Data Mining: SSE,MSE,RMSE,R-square指标讲解
转载自:http://blog.csdn.net/l18930738887/article/details/50629409 SSE(和方差.误差平方和):The sum of squares due ...