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. PHP中的 get_magic_quotes_runtime

    get_magic_quotes_runtime() 获得外部文件及数据库资料时是否进行转义 set_magic_quotes_runtime(1); 临时设置获得外部文件及数据库资料时是否进行转义 ...

  2. 24 - 面向对象基础-多继承-super-mro-Mixin

    目录 1 类的继承 2 不同版本的类 3 基本概念 4 特殊属性和方法 5 继承中的访问控制 6 方法的重写(override) 6.1 super 6.2 继承中的初始化 7 多继承 7.1 多继承 ...

  3. LCD驱动分析【转】

    转自:http://blog.csdn.net/hanmengaidudu/article/details/21559153 1.S3C2440上LCD驱动 (FrameBuffer)实例开发讲解 其 ...

  4. openjudge-NOI 2.6-2718 移动路线

    题目链接:http://noi.openjudge.cn/ch0206/2718/ 题解: 递推,某一个点只能从其左边或者下边走过来 f[i][j]存储(i,j)这个点上的结果,即f[i][j]=f[ ...

  5. lumen 框架的特殊使用

    1. 配置代码格式管理工具: composer require squizlabs/php_codesniffer, 使用 php artisan cs 检查代码格式: 2.单元测试用例编写 1./d ...

  6. TCP三次链接和四次断开

    经典的三次握手示意图:(#add,“握手”即图中左边到右边的连线)    经典的四次握手关闭图:    TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: 位码即t ...

  7. 快速地从Redhat系转Ubuntu系

    ubuntu官网的,https://help.ubuntu.com/community/SwitchingToUbuntu/FromLinux/RedHatEnterpriseLinuxAndFedo ...

  8. CSU 1351 Tree Counting

    原题链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1351 DP题,毫无疑问.由于动态规划题目做得少.不熟悉,刚开始自己用f[i]表示用 i ...

  9. GUC-1 volatile

    /* * 一.volatile 关键字:当多个线程进行操作共享数据时,可以保证内存中的数据可见. * 相较于 synchronized 是一种较为轻量级的同步策略. * * 注意: * 1. vola ...

  10. Java学习(final、static关键词)

    final关键词 概念:final的意思为最终,不可变.final是个修饰符,它可以用来修饰类,类的成员,以及局部变量.不能修饰构造方法. 特点: 1.final修饰的类不可以被继承,但可以继承别的类 ...