1. 程式人生 > >HDU3974 Assign the task —— dfs時間戳 + 線段樹

HDU3974 Assign the task —— dfs時間戳 + 線段樹

media close you void iat gns can for each ack

題目鏈接:https://vjudge.net/problem/HDU-3974

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.

InputThe 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)OutputFor 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

代碼如下:

技術分享
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <vector>
  7 #include <queue>
  8
#include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 const double EPS = 1e-8; 15 const int INF = 2e9; 16 const LL LNF = 2e18; 17 const int MAXN = 5e4+10; 18 19 vector<int>g[MAXN]; 20 int
have_fa[MAXN]; 21 int index, id[MAXN], le[MAXN], ri[MAXN]; 22 int task[MAXN<<2]; 23 24 void dfs(int u) 25 { 26 id[u] = le[u] = ++index; 27 for(int i = 0; i<g[u].size(); i++) 28 dfs(g[u][i]); 29 ri[u] = index; 30 } 31 32 void push_down(int u) 33 { 34 if(task[u]!=-1) 35 { 36 task[u*2] = task[u*2+1] = task[u]; 37 task[u] = -1; 38 } 39 } 40 41 void set_val(int u, int l, int r, int x, int y, int val) 42 { 43 if(x<=l && r<=y) 44 { 45 task[u] = val; 46 return; 47 } 48 49 push_down(u); 50 int mid = (l+r)>>1; 51 if(x<=mid) set_val(u*2, l, mid, x, y, val); 52 if(y>=mid+1) set_val(u*2+1, mid+1, r, x, y, val); 53 } 54 55 int query(int u, int l, int r, int x) 56 { 57 if(l==r) 58 return task[u]; 59 60 push_down(u); 61 int mid = (l+r)>>1; 62 if(x<=mid) return query(u*2, l, mid, x); 63 else return query(u*2+1, mid+1, r, x); 64 } 65 66 int main() 67 { 68 int n, m, T; 69 scanf("%d", &T); 70 for(int kase = 1; kase<=T; kase++) 71 { 72 scanf("%d", &n); 73 for(int i = 1; i<=n; i++) 74 g[i].clear(), have_fa[i] = 0; 75 for(int i = 1; i<n; i++) 76 { 77 int u, v; 78 scanf("%d%d", &u, &v); 79 g[v].push_back(u); 80 have_fa[u] = 1; 81 } 82 83 index = 0; 84 for(int i = 1; i<=n; i++) 85 if(!have_fa[i]) 86 dfs(i); 87 88 scanf("%d", &m); 89 memset(task, -1, sizeof(task)); 90 printf("Case #%d:\n", kase); 91 for(int i = 1; i<=m; i++) 92 { 93 char op[2]; int x, y; 94 scanf("%s", op); 95 if(op[0]==T) 96 { 97 scanf("%d%d", &x, &y); 98 set_val(1, 1, n, le[x], ri[x], y); 99 } 100 else 101 { 102 scanf("%d", &x); 103 printf("%d\n", query(1, 1, n, le[x])); 104 } 105 } 106 } 107 }
View Code

HDU3974 Assign the task —— dfs時間戳 + 線段樹