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条关系, 每次给一个人分配任务的时候,(如果他有)给他的所有下属也分配这个任务, 下属的下属也算自己的下属, 每次查询的时候都输出这个人最新的任务(如果他有), 没有就输 ...
随机推荐
- 【转载】解决方案:Resharper对系统关键字提示‘can not resolve symbol XXX’,并且显示红色,但是编译没有问题
环境:Visual studio 2013 community Update 4 + Resharper 8.2 + Windows 7 现象: 我的C#工程编译没有问题, 但是在代码编辑器中系统关键 ...
- LNMP编译安装教程
LNMP编译安装教程 此次安装在Centos上,我采用的CentOS的版本是:CentOS release 6.5 (Final) 可以通过以下命令查看:lsb_release -a 一.准备工作: ...
- MSSQL 和 REDIS的数据类型对应关系
when user_type_id in (34) then 'BLOB' --image when user_type_id in (35) then 'CLOB' --tex ...
- oracle返回多个参数
CREATE OR REPLACE PACKAGE BODY get_form_no_pub IS /*================================================ ...
- LuManager 2.0.97 设置shopex 手机版waptouch,绑定二级目录
1.步骤,先要把主站的 限制目录访问那个勾选,去掉 2.创建新网站,不需要ftp,直接设置网站目录“/home/主站目录/waptouch” 3.保存
- 观察者模式及Java实现例子
http://www.cnblogs.com/mengdd/archive/2013/02/07/2908929.html 观察者模式 观察者模式 Observer 观察者模式定义了一种一对多的依赖关 ...
- Qt 动画框架
最近一个项目中的需要做动画效果,很自然就想起来用qt animation framework .这个框架真的很强大.支持多个动画组合,线性动画,并行动画等.在此总结一下使用该框架一些需要注意的地方: ...
- sql server 相似度对比
转自:http://www.dotblogs.com.tw/rachen/archive/2008/10/07/5611.aspx 函數一.產生 Like 比對用字串 ) ) ) as begin / ...
- python(27)requests 爬取网页乱码,解决方法
最近遇到爬取网页乱码的情况,找了好久找到了种解决的办法: html = requests.get(url,headers = head) html.apparent_encoding html.enc ...
- 加快phpstorm、rubymine、pycharm系列IDE运行速度的方法
对jetbrains公司出品的IDE均有效,以rubymine为例: 打开C:\Program Files\JetBrains\RubyMine 6.3.3\bin,打开 rubymine.exe.v ...