HDU 3974 Assign the task 暴力/线段树
题目链接:
题目
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 暴力/线段树的更多相关文章
- hdu 3974 Assign the task(线段树)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3974 题意:给定一棵树,50000个节点,50000个操作,C x表示查询x节点的值,T x y表示更 ...
- HDU - 3974 Assign the task (线段树区间修改+构建模型)
https://cn.vjudge.net/problem/HDU-3974 题意 有一棵树,给一个结点分配任务时,其子树的所有结点都能接受到此任务.有两个操作,C x表示查询x结点此时任务编号,T ...
- HDU 3974 Assign the task (DFS+线段树)
题意:给定一棵树的公司职员管理图,有两种操作, 第一种是 T x y,把 x 及员工都变成 y, 第二种是 C x 询问 x 当前的数. 析:先把该树用dfs遍历,形成一个序列,然后再用线段树进行维护 ...
- hdu 3974 Assign the task (线段树+树的遍历)
Description There is a company that has N employees(numbered from 1 to N),every employee in the comp ...
- HDU 3974 Assign the task 并查集/图论/线段树
Assign the task Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- HDU 3974 Assign the task (DFS序 + 线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 给你T组数据,n个节点,n-1对关系,右边的是左边的父节点,所有的值初始化为-1,然后给你q个操 ...
- 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 ...
- HDU 3974 Assign the task(简单线段树)
Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3974 Assign the task(dfs序上线段树)
Problem Description There is a company that has N employees(numbered from 1 to N),every employee in ...
随机推荐
- MYSQL多实例配置与使用教程
原文http://www.111cn.net/database/mysql/58651.htm 在实际的开发过程中,可能会需要在一台服务器上部署多个MYSQL实例,那建议使用MYSQL官方的解决方案 ...
- iOS 最新版 CocoaPods 的安装使用
当在开发iOS应用时,会经常使用到很多的第三方开源类库,一般的方法是直接从GitHub下载,然后拖到项目中使用,如果该开源类库不依赖其他的类库,就可以直接使用:如果该开源类库还依赖一些其他的类库,则需 ...
- C语言知识总结(4)
变量的作用域 C语言根据变量作用域的不同,将变量分为局部变量和全局变量 1.局部变量 1> 定义:在函数内部定义的变量,称为局部变量.形式参数也属于局部变量. 2> 作用域:局部变量只在定 ...
- 内核 current宏解析
Technorati 标签: current thread_info 在内核中,可以通过current宏来获得当前执行进程的task_struct指针.现在来简要分析以下: 最原始 ...
- curl raise 信号出core
在使用c++多线程使用libcurl抓取网页时,遇到程序随机core掉的情况,gdb 一下出错信息有这么一条:longjmp causes uninitialized stack frame. 在网上 ...
- vector C++ 详细用法
vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说,vec ...
- 模板:abs用法
c语言书本上说,数学函数除了求整数的绝对值函数abs()之外<abs() 定义在stdlib.h中>,其余的函数都在头文件 math.h 中定义,包括对浮点数求绝对值的函数fabs().c ...
- iOS Foundation框架 -2.常用集合类简单总结
Foundation框架中常用的类有:NSString.NSArray.NSSet.NSDictionary 以及它们对应的子类 NSMutableString.NSMutableArray.NSMu ...
- Windows Phone 8.1开发:如何从ListView中,获取ScrollViewer对象
在使用ListView作为信心呈现载体开发应用程序时,我们经常需要通过监视滚动条(ScrollViewer)的位置状态来完成一些交互逻辑.最直接的体现就是 延时加载,(上滑加载更多,下拉获取更新数据) ...
- 火狐谷歌浏览器Json查看插件
1.搜: Firefox的JSON插件 参考: Chrome/FireFox浏览器下处理JSON的插件_Bruce_新浪博客 JSONView :: Firefox 附加组件 但是后来去发现没用: 打 ...