Assign the task 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

题解:想到了DFS序,这题就好做了。记录一下每个人下面总共有多少个下属,加上自己。剩下的用线段树维护标记就行了。
  1 #pragma warning(disable:4996)
  2 #include<cstdio>
  3 #include<stack>
  4 #include<cmath>
  5 #include<string>
  6 #include<cstring>
  7 #include<iostream>
  8 #include<algorithm>
  9 using namespace std;
 10 
 11 #define lson root<<1
 12 #define rson root<<1|1
 13 #define ll long long 
 14 
 15 const int maxn = 200005;
 16 const int maxm = 50005;
 17 
 18 int T, n, q, tot, cnt;
 19 int head[maxn], color[maxn], use[maxm], End[maxm], dp[maxm];
 20 
 21 struct node { int to, next; } e[maxm];
 22 
 23 void Inite() {
 24     tot = 0;
 25     cnt = 0;
 26     memset(use, 0, sizeof(use));
 27     memset(head, -1, sizeof(head));
 28 }
 29 
 30 void addedge(int u, int v) {
 31     e[tot].to = v;
 32     e[tot].next = head[u];
 33     head[u] = tot++;
 34 }
 35 
 36 void DFS(int u) {
 37     dp[u] = 1;
 38     End[u] = ++cnt;
 39     for (int i = head[u]; i != -1; i = e[i].next) {
 40         DFS(e[i].to);
 41         dp[u] += dp[e[i].to];
 42     }
 43 }
 44 
 45 void Build(int l, int r, int root) {
 46     color[root] = -1;
 47     if (l == r) return;
 48     int mid = (l + r) >> 1;
 49     Build(l, mid, lson);
 50     Build(mid + 1, r, rson);
 51 }
 52 
 53 void Pushdown(int root) {
 54     color[lson] = color[root];
 55     color[rson] = color[root];
 56     color[root] = -1;
 57 }
 58 
 59 void Update(int L, int R, int l, int r, int root,int x) {
 60     if (l > R || r < L) return;
 61     if (L <= l && r <= R) {
 62         color[root] = x;
 63         return;
 64     }
 65     if (color[root] >= 0) Pushdown(root);
 66     int mid = (l + r) >> 1;
 67     Update(L, R, l, mid, lson, x);
 68     Update(L, R, mid + 1, r, rson, x);
 69 }
 70 
 71 int Query(int k, int l, int r, int root) {
 72     if (l == r) return color[root];
 73     int mid = (l + r) >> 1;
 74     if (color[root] > 0) Pushdown(root);
 75     if (k <= mid) return Query(k, l, mid, lson);
 76     else return Query(k, mid + 1, r, rson);
 77 }
 78 
 79 int main()
 80 {
 81     scanf("%d", &T);
 82     for (int t = 1; t <= T; t++) {
 83         scanf("%d", &n);
 84         Inite();
 85         for (int i = 2; i <= n; i++) {
 86             int u, v;
 87             scanf("%d%d", &u, &v);
 88             addedge(v, u);
 89             use[u] = 1;
 90         }
 91         for (int i = 1; i <= n; i++) if (!use[i]) DFS(i);
 92 
 93         Build(1, n, 1);
 94 
 95         printf("Case #%d:\n", t);
 96         scanf("%d", &q);
 97 
 98         char op[2];
 99         for (int i = 1; i <= q; i++) {
100             scanf("%s", op);
101             if (op[0] == 'C') {
102                 int x;
103                 scanf("%d", &x);
104                 printf("%d\n", Query(End[x], 1, n, 1));
105             }
106             else {
107                 int x, y;
108                 scanf("%d%d", &x, &y);
109                 Update(End[x], End[x] + dp[x] - 1, 1, n, 1, y);
110             }
111         }
112     }
113     return 0;
114 }

 

posted @ 2018-04-16 23:58  天之道,利而不害  阅读(192)  评论(0编辑  收藏  举报