Assign the task

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=3974

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

HINT

题意

给你一个森林,有两个操作

1.让某一个点,和他的儿子们全部染成X

2.查询某一个点是什么权值

题解:

当成单纯的图论跑一发就好,对于每一个查询直接跑到根位置就好,更新的时候,注意更新的时间问题

线段树也可以做,把一颗树的,全部放在一起,然后就区间更新就好,对于每一个点,记录他的位置,和他儿子最远能到哪儿。

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
/* inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
//**************************************************************************************
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
struct node
{
int x,y;
}a[maxn];
int pa[maxn];
int main()
{
char s[];
int b,c;
int t=read();
for(int cas=;cas<=t;cas++)
{
int n=read();
memset(a,,sizeof(a));
memset(pa,-,sizeof(pa));
for(int i=;i<n-;i++)
{
int u=read(),v=read();
pa[u]=v;
}
for(int i=;i<=n;i++)
{
a[i].x=-;
a[i].y=-;
}
int m=read();
printf("Case #%d:\n",cas);
int co=;
for(int i=;i<m;i++)
{
scanf("%s",s);
if(s[]=='C')
{
b=read();
int k=a[b].x;
int y=a[b].y;
while(b!=-)
{
if(a[b].y>y)
{
k=a[b].x;
y=a[b].y;
}
b=pa[b];
}
printf("%d\n",k);
}
else
{
b=read(),c=read();
a[b].x=c;
a[b].y=++co;
}
}
}
}

HDU 3974 Assign the task 并查集/图论/线段树的更多相关文章

  1. HDU 3974 Assign the task 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=3974 题目大意: 一个公司有N个员工,对于每个员工,如果他们有下属,那么他们下属的下属也是他的下属. 公司会给员 ...

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

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

  3. 【BZOJ2054】疯狂的馒头(并查集,线段树)

    [BZOJ2054]疯狂的馒头(并查集,线段树) 题面 BZOJ 然而权限题,随便找个离线题库看看题吧. 题解 线段树就是个暴力,如果数据可以构造就能卡掉,然而不能构造,要不然复杂度瓶颈成为了读入了. ...

  4. HDU 3974 Assign the task 暴力/线段树

    题目链接: 题目 Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...

  5. HDU 3974 Assign the task

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

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

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

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

  8. HDU 3974 Assign the task(简单线段树)

    Assign the task Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

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

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

随机推荐

  1. thinkphp中的验证器

  2. Python如何实现文本转语音

    准备 我测试使用的Python版本为2.7.10,如果你的版本是Python3.5的话,这里就不太适合了. 使用Speech API 原理 我们的想法是借助微软的语音接口,所以我们肯定是要进行调用 相 ...

  3. C/C++——C语言跳出多重循环方法

    c语言的break语句只能跳出离它最近的一层循环,但是我们有时候需要跳出多层循环,以下有几种跳出多重循环的方法: 1. 使用goto ; i < MAX1; i++) { ; j < MA ...

  4. asp基础

    0.1在浏览器中通过查看源代码的方式是无法看到 ASP 源代码的,你只能看到由 ASP 文件输出的结果,而那些只是纯粹的 HTML 而已.这是因为,在结果被送回浏览器前,脚本已经在服务器上执行了. 0 ...

  5. NTP详解-转

    网管实战:Linux时间服务器配置 [IT168 专稿]目前计算机网络中各主机和服务器等网络设备的时间基本处于无序的状态.随着计算机网络应用的不断涌现,计算机的时间同步问题成为愈来愈重要的事情.以Un ...

  6. reverse和reverse_copy函数的应用

    reverse函数的作用是:反转一个容器内元素的顺序.函数参数:reverse(first,last);//first为容器的首迭代器,last为容器的末迭代器.它没有任何返回值. 这个函数比较简单, ...

  7. Delphi 绘图对象

    来自:http://blog.csdn.net/lailai186/article/details/8755430 ========================================== ...

  8. Nginx 502错误:upstream sent too big header while reading response header from upstream

    原因: 在使用Shiro的rememberMe功能时,服务器返回response的header部分过大导致. 解决方法: https://stackoverflow.com/questions/238 ...

  9. JS模块化规范CMD之SeaJS

    1. 在接触规范之前,我们用模块化来封装代码大多为如下: ;(function (形参模块名, 依赖项, 依赖项) { // 通过 形参模块名 修改模块 window.模块名 = 形参模块名 })(w ...

  10. 【DEV C++】 Error: ld returned 1 exit status

    一般出现“ld returned 1 exit status”错误都是由于函数名称拼写错误造成的,或者在一个工程中不同的函数使用了同一个函数名,暂时还未遇到其他情况.