1. 程式人生 > >J - Assign the task HDU - 3974(線段樹 + dfs序)

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;
}