【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 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 1C 3 
T 3 2 
C 3

Sample Output

Case #1:
-1 
1 
2

题目大意:

先输入n个人,然后是n个人的关系(下属 上司),然后分配任务,每一个上司分到一个任务就把任务给他的所有下属做,即他的下属的任务都是这个任务,查询一个人该时刻做的任务。

解题报告:

可以建模一下:是给定一棵树,然后一种操作是指定一个点,这个点及这个点的的子树被染色,另一种操作是指定一个点,问这个点的颜色。dfs序的实质就是:通过dfs树将这棵树放在线段上,然后用线段树优化求解。因为是单点查询所以不需要pushup,因为是区间更新并且需要叶子结点的值,所以需要pushdown。

AC代码:

#include<bits/stdc++.h>using namespace std;
const int MAX = 50000 + 5;
int n;
int cnt,id;
int head[MAX*2];
int q[MAX*4],in[MAX*4],out[MAX*4],rudu[MAX*4];
struct Edge {int to;int ne;
} e[MAX*4];//要乘2 吗? 
struct TREE {int l,r;int val;int laz;
} tree[MAX * 10];void dfs(int x,int root) {q[++id] = x;in[x] = id;for(int i = head[x]; i!=-1; i = e[i].ne) {dfs(e[i].to,x);}out[x] = id;
}
void add(int u,int v) {e[++cnt].to = v;e[cnt].ne = head[u];head[u] = cnt;
}
void build(int l,int r,int cur) {tree[cur].l = l;tree[cur].r = r;tree[cur].val = -1;tree[cur].laz = 0;//懒标记初始化成0 if(l == r) {return ;}int m = (l + r) /2;build(l,m,cur*2);build(m+1,r,cur*2+1);
}
void pushup(int cur) {if(tree[cur*2].val == tree[cur*2+1].val) tree[cur].val = tree[cur*2].val;else tree[cur].val = -2;//染色问题   通解?   但是此题是单点查询所以不需要pushup? 
} 
void pushdown(int cur) {if(tree[cur].laz == 0) return ;tree[cur*2].val =tree[cur*2].laz = tree[cur*2+1].laz = tree[cur*2+1].val = tree[cur].laz;tree[cur].laz = 0;
}
void update(int pl,int pr, int val,int cur) {if(pl <= tree[cur].l && pr >= tree[cur].r) {tree[cur].val = val;tree[cur].laz = val;return ;}pushdown(cur);if(pl <= tree[cur*2].r) update(pl,pr,val,2*cur);if(pr >= tree[cur*2+1].l) update(pl,pr,val,2*cur+1);//pushup(cur);
} 
int query(int tar,int cur) {if(tree[cur].l == tree[cur].r) {return tree[cur].val;}pushdown(cur);if(tar <= tree[cur*2].r) return query(tar,cur*2);else return query(tar,cur*2+1);//pushup(cur);
}
int main()
{
//	freopen("in.txt","r",stdin);int t;int m,iCase = 0;int u,v;char op[10];cin>>t;while(t--) {printf("Case #%d:\n",++iCase);cnt = id = 0;memset(head,-1,sizeof(head));memset(rudu,0,sizeof rudu);memset(e,0,sizeof(e));scanf("%d",&n);for(int i = 1; i<n; i++) {//读入n-1次 scanf("%d%d",&u,&v);add(v,u);rudu[u]++;}	int st =1;for(int i = 1; i<=n; i++) {if(rudu[i] == 0) {st = 	i;break;}}		dfs(st,0);build(1,n,1);scanf("%d",&m);while(m--) {scanf("%s",op);if(op[0] == 'C') {scanf("%d",&u);printf("%d\n",query(in[u],1));} else {scanf("%d%d",&u,&v);//把以u为根节点的子树更改为v update(in[u],out[u],v,1);}}}return 0 ;
}
//19:15 

总结:

   注意dfs序,必须从根节点开始,所以还需要找根节点!!!这里是用拓扑排序中入度的思想找的根节点。 

  【POJ - 3321】 Apple Tree这道题也是dfs序,但是没有找根节点,因为题目告诉你了1号节点是根节点.

本题的数组都是佛系开的,其实只是tree需要4倍,其他的都开MAX就够用。RE了一次是因为,发现dfs需要找根节点而不能直接用1号节点之后,加了rudu数组,但是rudu没有每次初始化,所以re了。。(话说不应该wa吗,搞不懂。。

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441909.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php 插件 代码架构,php反射机制详以及插件架构实例详解

1。用途&#xff1a;该扩展分析php程序&#xff0c;导出或提取出关于类、方法、属性、参数等的详细信息&#xff0c;包括注释。Reflection可以说是对php库函数&#xff1a;“Classes/Objects 类&#xff0f;对象函数”的一个扩展。主要用在通过程序检测现有php程序内部关于类、…

【HDU - 1540】 Tunnel Warfare (线段树进阶操作 区间合并+ 单点更新+ 最长覆盖区间查询 )

题干&#xff1a; During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was …

php扩展包安装了为啥没加载,已安装PHP扩展但未加载

我正在尝试安装php的ssh2扩展,并且有一点点困难.文件在那里,它只是没有加载到PHP.首先,我安装了ssh2&#xff1a;aptitude install libssh2-1-dev libssh2-php(对于它的价值,我在Nginx上运行Ubuntu 12.04.)我可以看到使用modules命令加载ssh2&#xff1a;php -m |grep ssh2ssh2…

【HDU - 1166】敌兵布阵 (线段树模板 单点更新+ 区间查询)

题干&#xff1a; C国的死对头A国这段时间正在进行军事演习&#xff0c;所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段&#xff0c;所以每个工兵营地…

php中获取本月第二天,php第二天

//heredoc方式,可以保存长文本 <<<$str <<长文本EOTS;header("Content-Type:text/html;charsetutf-8"); //防止乱码0 1 2 3 4 5 十进制0 1 10 11 100 101 二进制十进制专二进制除二取余二进制专十进制1011*2*20*2*1(1*2/2)5;120 0 1 0 16 3 211111$a…

【HDU - 1754】I Hate It (线段树模板 单点覆盖更新+区间最大值查询)

题干&#xff1a; 很多学校流行一种比较的习惯。老师们很喜欢询问&#xff0c;从某某到某某当中&#xff0c;分数最高的是多少。 这让很多学生很反感。 不管你喜不喜欢&#xff0c;现在需要你做的是&#xff0c;就是按照老师的要求&#xff0c;写一个程序&#xff0c;模拟老…

php微信上传头像,微信小程序怎么上传头像

这次给大家带来的是微信小程序 上传头像的实例详解&#xff0c;最近在做微信小程序上传头像和上传照片功能就随手写一下代码&#xff0c;这篇文章就给大家好好分析一下。上传头像html&#xff1a;头像JS代码// 切换头像changeAvatar: function () {var that this;// var child…

【HDU - 1698】 Just a Hook(线段树模板 区间覆盖更新(laz标记) + 区间和查询 )

题干&#xff1a; In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length. Now Pudge wants to do some operations on the hoo…

反序列化 php R类型,pikachu-PHP反序列化、XXE、SSFR

一、PHP反序列化1.1概述在理解这个漏洞前,你需要先搞清楚php中serialize()&#xff0c;unserialize()这两个函数。序列化serialize()序列化说通俗点就是把一个对象变成可以传输的字符串,比如下面是一个对象:class S{public $test"pikachu";}$snew S(); //创建一个对象…

【POJ - 3468 】 A Simple Problem with Integers (线段树模板 区间更新 + 区间和查询)(不能树状数组或差分数组)

题干&#xff1a; You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval. I…

php向下滑动,js如何判断鼠标滚轮是向下还是向上滚动

判断鼠标滚轮是向上或向下滚动&#xff0c;不同的浏览器的判别方式是不一样的&#xff0c;当前比较流行的浏览器有 IE&#xff0c;Opera&#xff0c;Safari&#xff0c;Firefox&#xff0c;Chrome&#xff0c;在这个问题上Firefox和其他浏览器的实现方式是不一样的。现在通过一…

学习php技巧,对初学者非常有用的PHP技巧

对初学者非常有用的PHP技巧131415161718function add_to_cart($item_id , $qty){if(!is_array($item_id)){$_SESSION[cart][$item_id] $qty;}else{foreach($item_id as $i_id > $qty){$_SESSION[cart][$i_id] $qty;}}}add_to_cart( IPHONE3 , 2 );add_to_cart( array(IPHO…

【51nod - 1065】 最小正子段和( 前缀和排序 )

题干&#xff1a; N个整数组成的序列a11,a22,a33,…,ann&#xff0c;从中选出一个子序列&#xff08;aii,ai1i1,…ajj&#xff09;&#xff0c;使这个子序列的和>0&#xff0c;并且这个和是所有和>0的子序列中最小的。 例如&#xff1a;4&#xff0c;-1&#xff0c;5&a…

oracle trace 文件名,限制oracle trace 文件大小

限制oracle trace 文件大小2007-10-10 17:34:55| 分类&#xff1a; oracle | 标签&#xff1a;|字号max_dump_file_size参数在oracle数据库中用来限制 oracle trace 文件大小max_dump_file_size 的单位是 操作系统块 默认值为 UNLIMITED在有些时候&#xff0c;oracle会产生…

【51nod - 前缀异或】 对前缀和的理解

题干&#xff1a; 前缀异或 基准时间限制&#xff1a;2 秒 空间限制&#xff1a;131072 KB 分值: 5 输入一个长度为n(1 < n < 100000)数组a[1], a[2], ..., a[n]。 输入一个询问数m(1 < m < 100000)和m组询问&#xff0c;每组询问形如(l, r) 对于每组询问(l, …

【C语言实现反转数组】(用栈实现)51nod - 训练营

题干&#xff1a; 输入一个长度为n(1 < n < 100000)数组&#xff0c;倒序输出他。 数组中的元素ai满足(1 < ai < 100000)。 Input 第一行一个整数n&#xff0c;表示数字长度 接下来n行&#xff0c;每行一个整数ai&#xff0c;表示数组的内容。 Output 输出第…

oracle 依赖包自动安装包,ORACLE 安装提示缺少依赖包

安装ORALCE 在做检验时提示缺少依赖包&#xff0c;其实是因为系统是64位系统而这些包都是32位的&#xff0c;This is a prerequisite condition to test whether the package "libaio-0.3.105" is available on the systemThis is a prerequisite condition to test…

oracle中创建实体,生成实体-SqlSugar 4.x-文档园

注意&#xff1a;使用DbFirst数据库账户要有系统表的权限,否则无法读取表的结构1.将库里面所有表都生成实体类文件db.DbFirst.CreateClassFile("c:\\Demo\\1",命名空间);2.指定名表生成 &#xff0c;可以传数组db.DbFirst.Where("Student").CreateClassFil…

永远年轻,永远热泪盈眶----致所有奋斗的ACMer

转载一个弱校ACMer的生涯回忆录&#xff1a;&#xff08;励志一下&#xff0c;顺便给自己一碗鸡汤~&#xff09; 有些事情&#xff0c;承诺了便要将它实现&#xff0c;就像我现在写下这些文字。或许之前我并不该定下“每个退役队员写一篇退役贴”这样一个规矩&#xff0c;这多少…

oracle pl/sql 面试,Oracle SQL 面试题(整理)

1、关于group by表内容&#xff1a;2005-05-09 胜2005-05-09 胜2005-05-09 负2005-05-09 负2005-05-10 胜2005-05-10 负2005-05-10 负如果要生成下列结果, 该如何写sql语句?胜 负2005-05-09 2 22005-05-10 1 2创建过程如下&#xff1a;create table tmp(rq varchar(10),shengf…