J - Assign the task HDU - 3974(线段树 + dfs序)

本文介绍了一种解决公司员工任务分配与实时查询问题的算法。通过构建员工间的上下级关系树,并使用线段树数据结构,实现了高效的任务分配及员工当前任务的快速查询。文章详细阐述了算法实现步骤,包括图的构建、深度优先搜索确定影响范围、线段树的更新和查询操作。

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.

Input

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)

Output

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.

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

解题思路:

这道题的难点就在于如何将题中的对应关系转化为线段树的模型。我们先将其给出的点的关系建立一个图(也可以称之为树,不过不是二叉树)然后从公司老板(也就是入度为0的点)出发,用dfs来确定每个点的影响范围并记录下来,在更新某个点的时候转化过来就好了,至于查询就是简单的单点查询了。

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn = 50010;
int data[maxn], ss[maxn], tt[maxn], head[maxn], Tree[maxn * 4], lazy[maxn * 4];
int t, n, m, top, len;
bool vis[maxn];

 ///接下来结构体和add函数是用数组模拟邻接表,也可以换用其他的方式来存储图的信息只要能dfs遍历就行
struct Edge                          
{
	int v;
	int next;
}edge[maxn * 2];

void add(int u, int v)
{
	edge[top].v = v;
	edge[top].next = head[u];
	head[u] = top++;
}

void dfs(int x)
{
	for(int i = head[x]; i != -1; i = edge[i].next)
	{
		if(!vis[edge[i].v])
		{
			vis[edge[i].v] = true;
			ss[edge[i].v] = len + 1;///至于为什么加1,以及下边为什么++len,把样例拿出来自己试试就明白了
			dfs(edge[i].v);
			tt[edge[i].v] = ++len;
		}
	}
}

void update(int L, int R, int data, int l, int r, int rt)
{
	if(l >= L && r <= R)
	{
		Tree[rt] = data;
		lazy[rt] = data;
		return ;
	}
	if(lazy[rt] != -1)
	{
		Tree[rt << 1] = lazy[rt];
		lazy[rt << 1] = lazy[rt];
		Tree[rt << 1 | 1] = lazy[rt];
		lazy[rt << 1 | 1] = lazy[rt];
		Tree[rt] = -1;
		lazy[rt] = -1;
	}
	int m = (l + r) / 2;
	if(m >= L)
		update(L, R, data, l, m, rt << 1);
	if(m < R)
		update(L, R, data, m + 1, r, rt << 1 | 1);
}

int query(int pos, int l, int r, int rt)
{
	if(l == r && l == pos)
	{
		return Tree[rt];
	}
	if(lazy[rt] != -1)
	{
		Tree[rt << 1] = lazy[rt];
		lazy[rt << 1] = lazy[rt];
		Tree[rt << 1 | 1] = lazy[rt];
		lazy[rt << 1 | 1] = lazy[rt];
		Tree[rt] = -1;
		lazy[rt] = -1;
	}
	int m = (l + r) / 2;
	int ans = 0;
	if(m >= pos)
		ans += query(pos, l, m, rt << 1);
	else
		ans += query(pos, m + 1, r, rt << 1 | 1);
	return ans;
}

int main()
{
   // freopen("in.txt", "r", stdin);
	scanf("%d", &t);
	int cnt = 1;
	while(t--)
	{
		scanf("%d", &n);
		memset(Tree, -1, sizeof(Tree));
		memset(lazy, -1, sizeof(lazy));
		memset(vis, false, sizeof(vis));
		memset(head, -1, sizeof(head));
		top = 0;
		len = 0;
		int a, b;
		for(int i = 1; i <= n - 1; ++ i)
		{
			scanf("%d%d", &a, &b);
			add(b, a);
			vis[a] = true;
		}
		int k;
		for(int i = 1; i <= n; ++ i) ///找到最终boss
		{
			if(!vis[i])
			{
				k = i;
				break;
			}
		}
		memset(vis, false, sizeof(vis)); ///重新初始化用来标记是否走过
		vis[k] = true;
		ss[k] = 1;
		dfs(k);
		tt[k] = ++len;
		scanf("%d", &m);
		char st[4];
		int x, y;
		printf("Case #%d:\n", cnt++);
		for(int i = 1; i <= m; ++ i)
		{
			scanf("%s", st);
			if(st[0] == 'C')
			{
				scanf("%d", &x);
				//cout << "query " << tt[x] << endl;
				printf("%d\n", query(tt[x], 1, n, 1));
			}
			else
			{
				scanf("%d%d",  &x, &y);
				//cout << "update " << ss[x] << ' ' <<tt[x] << endl;
				update(ss[x], tt[x], y, 1, n, 1);
			}
		}
	}
	return 0;
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值