题目链接:

题目

Assign the task

Time Limit: 15000/5000 MS (Java/Others)

Memory Limit: 32768/32768 K (Java/Others)

问题描述

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.

输入

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)

输出

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.

样例

input

1

5

4 3

3 2

1 3

5 2

5

C 3

T 2 1

C 3

T 3 2

C 3

output

Case #1:

-1

1

2

题意

给你一颗树,执行两种操作:

  • 把以x为根的子树的任务全部赋值为y.

  • 查询x的当前任务。

题解

查了很多题解说是并查集,明明赤裸裸的暴力,看不出哪里跟并查集有关。。

暴力为什么能过orz,想都没敢想暴力。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; const int maxn = 5e4 + 10; int fa[maxn], tim[maxn], tas[maxn];
int n,tot=0; void init() {
tot = 0;
memset(tim, 0, sizeof(tim));
memset(fa, -1, sizeof(fa));
memset(tas, -1, sizeof(tas));
} int main() {
int tc, kase = 0;
scanf("%d", &tc);
while (tc--) {
scanf("%d", &n);
init();
for (int i = 0; i < n - 1;i++){
int u, v;
scanf("%d%d", &u, &v);
fa[u] = v;
}
int q;
scanf("%d", &q);
printf("Case #%d:\n", ++kase);
while (q--) {
char cmd[11]; int x, y;
scanf("%s", cmd);
if (cmd[0] == 'C') {
scanf("%d", &x);
int t = -1, v = -1;
while (x != -1) {
if (t < tim[x]) {
t = tim[x];
v = tas[x];
}
x = fa[x];
}
printf("%d\n", v);
}else{
scanf("%d%d", &x, &y);
tim[x] = ++tot;
tas[x] = y;
}
}
}
return 0;
}

还是感觉来一发线段树比较靠谱。

求dfs序,按照这个序来建线段树。

时间跑的竟然还不如暴力orz。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#define lson (o<<1)
#define rson ((o<<1)|1)
#define M (l+(r-l)/2)
using namespace std; const int maxn = 5e4 + 10; int n; int fa[maxn],pre[maxn], aft[maxn], dfs_clock;
vector<int> G[maxn];
void dfs(int u) {
pre[u] = ++dfs_clock;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (v == fa[u]) continue;
dfs(v);
}
aft[u] = ++dfs_clock;
} int setv[maxn << 3], timv[maxn << 3],tot; int _p, _ret,_tim;
void query(int o, int l, int r) {
if (l == r) {
if (_tim < timv[o]) {
_tim = timv[o], _ret = setv[o];
}
}
else {
if (_tim < timv[o]) {
_tim = timv[o], _ret = setv[o];
}
if (_p <= M) query(lson, l, M);
else query(rson, M + 1, r);
}
} int ul, ur, uv,ut;
void update(int o, int l, int r) {
if (ul <= l&&r <= ur) {
setv[o] = uv, timv[o] = ut;
}
else {
if(ul<=M) update(lson, l, M);
if(ur>M) update(rson, M + 1, r);
}
} void init() {
dfs_clock = tot = 0;
for (int i = 0; i <= n; i++) G[i].clear();
memset(setv, -1, sizeof(setv));
memset(timv, -1, sizeof(timv));
memset(fa, -1, sizeof(fa));
} int main() {
int tc, kase = 0;
scanf("%d", &tc);
while (tc--) {
scanf("%d", &n);
init();
for (int i = 0; i < n - 1; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[v].push_back(u);
fa[u] = v;
}
int rt = -1;
for (int i = 1; i <= n; i++) if (fa[i] == -1) {
rt = i; break;
}
dfs(rt);
int q;
scanf("%d", &q);
printf("Case #%d:\n", ++kase);
while(q--){
char cmd[22]; int x, y;
scanf("%s", cmd);
if (cmd[0] == 'C') {
scanf("%d", &x);
_p = pre[x], _ret = -1, _tim = -1;
query(1, 1, dfs_clock);
printf("%d\n", _ret);
}
else {
scanf("%d%d", &x, &y);
ul = pre[x], ur = aft[x],uv=y,ut=++tot;
update(1, 1, dfs_clock);
}
}
}
return 0;
}

HDU 3974 Assign the task 暴力/线段树的更多相关文章

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

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

  2. HDU - 3974 Assign the task (线段树区间修改+构建模型)

    https://cn.vjudge.net/problem/HDU-3974 题意 有一棵树,给一个结点分配任务时,其子树的所有结点都能接受到此任务.有两个操作,C x表示查询x结点此时任务编号,T ...

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

    题意:给定一棵树的公司职员管理图,有两种操作, 第一种是 T x y,把 x 及员工都变成 y, 第二种是 C x 询问 x 当前的数. 析:先把该树用dfs遍历,形成一个序列,然后再用线段树进行维护 ...

  4. hdu 3974 Assign the task (线段树+树的遍历)

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

  5. HDU 3974 Assign the task 并查集/图论/线段树

    Assign the task Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  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(dfs序上线段树)

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

随机推荐

  1. php 图片调整大小 封装类【转载】

    <?php class ImageResize { private $image; private $img_des; private $image_type; private $permiss ...

  2. Mvc项目右键没有控制器选项

    今天遇到了一个比较少见的问题,我用vs2010打开一个从Svn上拉下来的mvc2项目,在Controller文件夹上右键却发现没有新建控制器的选项,在View文件夹上右键也没有新建视图的选项. 我的第 ...

  3. C#中数组、ArrayList和List<T>三者的发展历程

    在C#中数组,ArrayList,List使我们用的最多的类型之一.他们共同的作用都是能够存储一组对象. 那么问题来了: (1)为什么要有三个一样作用的东西呢?他们都很完美吗? (2)谁先出生,又是因 ...

  4. UI1_ViewController视图切换及Appdelegate

    // // ThirdViewController.h // UI1_ViewController视图切换及Appdelegate // // Created by zhangxueming on 1 ...

  5. xmlspy注册后打开报错的解决办法

    XMLSpy 2011中文版破解补丁使用方法 1.如果你下载的版本是r2sp1的话(r2不用此步骤),先用补丁主程序(altova.xmlspy.v2011r2sp1b-patch.exe).2.XM ...

  6. Spark RDD整理

    参考资料: Spark和RDD模型研究:http://itindex.net/detail/51871-spark-rdd-模型 理解Spark的核心RDD:http://www.infoq.com/ ...

  7. MVC5_学习笔记_1_CodeFirst

    MVC5_EF6_1/* GitHub stylesheet for MarkdownPad (http://markdownpad.com) *//* Author: Nicolas Hery - ...

  8. NDK jni 加载静态库

    加载静态库到android,静态库的提供方式有2种, a. 通过源文件来编译静态库 b. 加载已经编译好的静态库 首先我们来看,通过源文件来编译静态库,工程目录如下 第一步:我们来看我们的jni目录, ...

  9. Google Ajax Library API使用方法(JQuery)

    Google Ajax Library API使用方法 1.传统方式: <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7. ...

  10. MVC 5.0(or5.0↓) Ajax.BeginForm 异步上传附件问题,答案是不能的!

    MVC 5.0(or5.0↓)  Ajax.BeginForm 异步上传附件问题,答案是不能的! (请注意我这里说的异步!) 来看一下下面这段一步提交file的代码 //前台 .cshtml 文件 & ...