HDU 3974 Assign the task(dfs建树+线段树)
题目大意:公司里有一些员工及对应的上级,给出一些员工的关系,分配给某员工任务后,其和其所有下属都会进行这项任务。输入T表示分配新的任务,
输入C表示查询某员工的任务。本题的难度在于建树,一开始百思不得其解,后来看了lx大大的博客后才明白,用递归建立了各个员工之间的关系,Start[x]
表示x员工为Boss的起点,End[x]表示x员工为Boss的终点。之后对这样的整体线段进行赋值即可。
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string.h>
using namespace std;
#define lson rt<<1
#define rson rt<<1|1
#define N 50005
struct tree
{
int l, r, task, iscover;
int mid()
{
return (l+r)/;
}
}a[N<<];
int vis[N], index, Start[N], End[N];
vector<int>G[N];
void dfs(int k)///模拟为boss树,记录各员工在树上的编号
{
Start[k] = ++index;
for(int i=, len=G[k].size(); i<len; i++)
dfs(G[k][i]); End[k] = index;
}
void build(int rt, int l, int r)
{
a[rt].l = l;
a[rt].r = r;
a[rt].task = -;
a[rt].iscover = ;
if(l==r)return ;
build(lson, l, a[rt].mid());
build(rson, a[rt].mid()+, r);
}
void Down(int rt)
{
if(a[rt].l!=a[rt].r && a[rt].iscover)
{
a[lson].iscover = a[rson].iscover = ;
a[rt].iscover = ;
a[lson].task = a[rson].task = a[rt].task;
}
}
void Update(int rt, int l, int r, int task)
{
if(a[rt].l==l && a[rt].r==r)
{
a[rt].task = task;
a[rt].iscover = ;
return ;
} Down(rt); if(a[rt].mid()>=r)Update(lson, l, r, task);
else if(a[rt].mid()<l)Update(rson, l, r, task);
else
{
Update(lson, l, a[rt].mid(), task);
Update(rson, a[rt].mid()+, r, task);
}
}
int Query(int rt, int k)
{
if(a[rt].l==a[rt].r)
{
return a[rt].task;
} Down(rt); if(a[rt].mid()>=k)return Query(lson, k);
else return Query(rson, k);
}
int main()
{
int T, icase = ;
scanf("%d", &T);
while(T--)
{
int n, b, c;
memset(vis, , sizeof(vis));
scanf("%d", &n);
for(int i=; i<=n; i++)
G[i].clear();
for(int i=; i<n; i++)
{
scanf("%d %d", &b, &c);
G[c].push_back(b);
vis[b] = ;
}
index = ;
for(int i=; i<=n; i++)
{
if(!vis[i])
{
dfs(i);///找到最高BOSS
break;
}
}
build(, , n);
int m, x, y;
scanf("%d", &m);
char order[];
printf("Case #%d:\n", icase++);
while(m--)
{
scanf("%s", order);
if(order[]=='T')
{
scanf("%d %d", &x, &y);
Update(, Start[x], End[x], y);
}
else
{
scanf("%d", &x);
printf("%d\n", Query(, Start[x]));
}
}
}
return ;
}
HDU 3974 Assign the task(dfs建树+线段树)的更多相关文章
- 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) ...
- [Assign the task][dfs序+线段树]
http://acm.hdu.edu.cn/showproblem.php?pid=3974 Assign the task Time Limit: 15000/5000 MS (Java/Other ...
- HDU 3974 Assign the task (DFS+线段树)
题意:给定一棵树的公司职员管理图,有两种操作, 第一种是 T x y,把 x 及员工都变成 y, 第二种是 C x 询问 x 当前的数. 析:先把该树用dfs遍历,形成一个序列,然后再用线段树进行维护 ...
- hdu3974 Assign the task dfs序+线段树
There is a company that has N employees(numbered from 1 to N),every employee in the company has a im ...
- HDU3974 Assign the task —— dfs时间戳 + 线段树
题目链接:https://vjudge.net/problem/HDU-3974 There is a company that has N employees(numbered from 1 to ...
- hdu 3974 Assign the task(dfs序上线段树)
Problem Description There is a company that has N employees(numbered from 1 to N),every employee in ...
- HDU - 3974 Assign the task (DFS建树+区间覆盖+单点查询)
题意:一共有n名员工, n-1条关系, 每次给一个人分配任务的时候,(如果他有)给他的所有下属也分配这个任务, 下属的下属也算自己的下属, 每次查询的时候都输出这个人最新的任务(如果他有), 没有就输 ...
随机推荐
- WebJars 进行 css js 资源文件管理
WebJars是将这些通用的Web前端资源打包成Java的Jar包,然后借助Maven工具对其管理,保证这些Web资源版本唯一性,升级也比较容易.关于webjars资源,有一个专门的网站http:// ...
- 【转】浏览器内核、渲染引擎、js引擎
[1]定义 浏览器内核分成两部分渲染引擎和js引擎,由于js引擎越来越独立,内核就倾向于只指渲染引擎 渲染引擎是一种对HTML文档进行解析并将其显示在页面上的工具[2]常见引擎 渲染引擎: firef ...
- Python error: ascii’/'utf-8′ codec can’t decode byte 0xb8 in position 50: ord
字符串使用了费ascii编码的字符,也就是它代表的16进制的编码超过127. 解决这个问题可以使用下面的方法解决,其实就是设置默认的编码.python 2.x的默认编码是ascii,如果改为utf-8 ...
- WeX5之xid相关API
WeX5针对xid提供了以下js api: 1.根据xid获取id:this.getIDByXID(xid): 2.根据xid获取HTML节点:this.getElementByXid(xid),此a ...
- 【freemaker】之文本,html文本,去除空格,字母大小写,循环数组,字符串截取,map取值,遍历map
测试代码 @Test public void test06(){ try { root.put("emp", "<span color='red'>你好张三& ...
- ADF_Data Binding系列2_使用URL Service Data Control
2015-02-16 Created By BaoXinjian
- codeforces 689 E. Mike and Geometry Problem 组合数学 优先队列
给定一个函数: f([l,r]) = r - l + 1; f(空集) = 0; 即f函数表示闭区间[l,r]的整点的个数 现在给出n个闭区间,和一个数k 从n个区间里面拿出k个区间,然后对这k个区间 ...
- UML精粹1 - 简介
Martin的主页 http://martinfowler.com/. Pavel Hruby开发的visio模板,可以用来画UML图: http://phruby.com 简介 统一建模语言UML是 ...
- Zero Requiem
“最后是在游行.暴君鲁路修高居王座,两侧列着所有反对者的代表:黑色骑士团.黎星刻.原圆桌骑士名列第三的吉诺,以及一身女囚装的娜娜丽,他们都即将被公开处死.尤菲米娅在第一次“特别行政区•日本”成立仪式上 ...
- [DFNews] GetData也出取证软件了
从事计算机取证的应该都听说过MIP(Mount Image Pro).VFC仿真和Recover My Files,上述三个应用比较广泛的软件都是GetData公司的产品.GetData现在也推出了自 ...