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 题意 : 每个老板有很多员工 , 当老板有任务时 , 他的所有员工都会执行此任务 , 并且弃掉他以前的任务。 思路 :
  这题是在线段树专题里的 , 然后我自己构造了一个树 , 用 vector 存的点 , 但是超内存了 , 我也不知道什么鬼 , 搜网上的也有用 vector 写的 , 但他们没事 , 搞不懂 。 还有一种解法 , 是用并查集解的 , 他与普通的区别在于 他对每个节点在多加一个参数 ,表示当前变量出现的时间截点 。 代码示例 :
  
/*
* Author: ry
* Created Time: 2017/10/13 20:59:29
* File Name: 1.cpp
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <time.h>
using namespace std;
const int eps = 5e4+5;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define ll long long struct s
{
int task;
int time;
}po[eps]; int pre[eps]; int main() {
int t, n, m;
int a, b;
char ch[5];
int k = 1; cin >>t;
while (t--){
cin >>n;
for(int i = 1; i <= n; i++){
po[i].task = -1;
po[i].time = 0;
}
for(int i = 1; i <= n; i++) pre[i] = i;
for(int i = 1; i < n; i++){
scanf("%d%d", &a, &b);
pre[a] = b;
}
printf("Case #%d:\n", k++);
cin >>m;
int t = 0;
while (m--){
scanf("%s", ch);
if (ch[0] == 'C'){
scanf("%d", &a);
int ans = -1, time = 0;
int i = a;
while (pre[i] != i){
if (po[i].time > time) {
ans = po[i].task;
time = po[i].time;
}
i = pre[i];
}
if (po[i].time > time) ans = po[i].task;
printf("%d\n", ans);
}
else {
scanf("%d%d", &a, &b);
t++;
po[a].task = b;
po[a].time = t;
}
}
} return 0;
}

线段树 or 并查集 (多一个时间截点)的更多相关文章

  1. Educational Codeforces Round 51 (Rated for Div. 2) G. Distinctification(线段树合并 + 并查集)

    题意 给出一个长度为 \(n\) 序列 , 每个位置有 \(a_i , b_i\) 两个参数 , \(b_i\) 互不相同 ,你可以进行任意次如下的两种操作 : 若存在 \(j \not = i\) ...

  2. BZOJ4399魔法少女LJJ——线段树合并+并查集

    题目描述 在森林中见过会动的树,在沙漠中见过会动的仙人掌过后,魔法少女LJJ已经觉得自己见过世界上的所有稀奇古怪的事情了LJJ感叹道“这里真是个迷人的绿色世界,空气清新.淡雅,到处散发着醉人的奶浆味: ...

  3. 【BZOJ2733】永无乡(线段树,并查集)

    [BZOJ2733]永无乡(线段树,并查集) 题面 BZOJ 题解 线段树合并 线段树合并是一个很有趣的姿势 前置技能:动态开点线段树 具体实现:每次合并两棵线段树的时候,假设叫做\(t1,t2\), ...

  4. 2019牛客暑期多校训练营(第八场)E:Explorer(LCT裸题 也可用线段树模拟并查集维护连通性)

    题意:给定N,M,然后给出M组信息(u,v,l,r),表示u到v有[l,r]范围的通行证有效.问有多少种通行证可以使得1和N连通. 思路:和bzoj魔法森林有点像,LCT维护最小生成树.  开始和队友 ...

  5. 2018.09.30 bzoj4025: 二分图(线段树分治+并查集)

    传送门 线段树分治好题. 这道题实际上有很多不同的做法: cdq分治. lct. - 而我学习了dzyo的线段树分治+并查集写法. 所谓线段树分治就是先把操作分成lognlognlogn个连续不相交的 ...

  6. 洛谷P4121 [WC2005]双面棋盘(线段树套并查集)

    传送门 先膜一下大佬->这里 据说这题正解是LCT,然而感觉还是线段树套并查集的更容易理解 我们对于行与行之间用线段树维护,每一行内用并查集暴力枚举 每一行内用并查集暴力枚举连通块这个应该容易理 ...

  7. 线段树 或者 并查集 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C

    http://codeforces.com/contest/722/problem/C 题目大意:给你一个串,每次删除串中的一个pos,问剩下的串中,连续的最大和是多少. 思路一:正方向考虑问题,那么 ...

  8. Codeforces.1051G.Distinctification(线段树合并 并查集)

    题目链接 \(Description\) 给定\(n\)个数对\(A_i,B_i\).你可以进行任意次以下两种操作: 选择一个位置\(i\),令\(A_i=A_i+1\),花费\(B_i\).必须存在 ...

  9. BZOJ2733[HNOI2012]永无乡——线段树合并+并查集+启发式合并

    题目描述 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达 ...

随机推荐

  1. Springboot-webscoket with sockjs

    新建springboot maven工程,引入以下包 <dependency> <groupId>org.springframework.boot</groupId> ...

  2. 关于scipy包的安装

    先安装好scikit-learn和numpy,从网上下载scipy的whl文件,通过pip install+安装包地址的方法安装

  3. Roslyn 如何使用 MSBuild Copy 复制文件

    本文告诉大家如何在 MSBuild 里使用 Copy 复制文件 需要知道 Rosyln 是 MSBuild 的 dotnet core 版本. 在 MSBuild 里可以使用很多命令,本文告诉大家如何 ...

  4. 【9103】求n的累加和

    Time Limit: 10 second Memory Limit: 2 MB 问题描述 用高精度方法,求s=1+2+3+...+n的精确值(n以一般整数输入) Input 文件输入仅一行,输入n ...

  5. H3C DHCP服务器可选配置

  6. dotnet core 输出调试信息到 DebugView 软件

    本文告诉大家如何在 dotnet core 输出调试信息到 DebugView 软件 在之前告诉小伙伴,如何在 WPF 输出调试信息到 DebugView 软件,请看文章 WPF 调试 获得追踪输出 ...

  7. LuoguP3521 [POI2011]ROT-Tree Rotations

    P3521 [POI2011]ROT-Tree Rotations 题目大意: 给一棵\((1≤n≤200000)\)个叶子的二叉树,可以交换每个点的左右子树,要求前序遍历叶子的逆序对最少. 我们发现 ...

  8. Checkpoint 防火墙修改网卡buffer

    近期有一台CP5100频繁丢包,修改网卡buffer步骤如下,默认的网卡buffer为256,最大可以修改为4096,其步骤如下: step1:ssh登录checkpoint 防火墙控制台 step2 ...

  9. Wannafly挑战赛25 因子 [数论]

    一.题意 令 X = n!, 给定一大于1的正整数p 求一个k使得 p ^k | X 并且 p ^(k + 1) 不是X的因子 输入为两个数n, p (1e18>= n>= 10000 & ...

  10. while Ture怎么退出全部

    while True: #这是一个死循环 如果想要quit出去只能一层一层的退出 比如如果在第三层输入quit会回到第二层 那么有什么方法能够在第三层就退出全部? print("level1 ...