Kuangbin Topic 7 hdu1_4 assign the task (DFS timestamp creation)

Source: Internet
Author: User
There is a company that has n employees (numbered from 1 to n), every employee in the company has a immediate boss (employee t 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 assigned ed a task in the same time. furthermore, whenever a employee ed 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


After reading the questions, I found that the achievements were a problem, And then I learned the achievements of DFS. There is a conclusion that if V is the ancestor of U, then the DFS sequence st [v] <st [u] & ED [v]> ed [u]

So we can make achievements. Then Mark and check



1 # include <iostream> 2 # include <stdio. h> 3 # include <math. h> 4 # include <string. h> 5 # include <stdlib. h> 6 # include <string> 7 # include <vector> 8 # include <set> 9 # include <map> 10 # include <queue> 11 # include <algorithm> 12 # include <sstream> 13 # include <stack> 14 using namespace STD; 15 # define fo freopen ("in.txt", "r", stdin); 16 # define rep (I, A, n) for (INT I = A; I <N; I ++) 17 # define per (I, A, n) f Or (INT I = n-1; I> = A; I --) 18 # define Pb push_back 19 # define MP make_pair 20 # define all (x ). begin (), (x ). end () 21 # define Fi first 22 # define se second 23 # define SZ (x) (INT) (x ). size () 24 # define debug (x) cout <"&" <x <"&" <Endl; 25 # define lowbit (X) (X &-x) 26 # define MEM (a, B) memset (a, B, sizeof (a); 27 typedef vector <int> VI; 28 typedef long ll; 29 typedef pair <int, int> PII; 30 Const ll mod = 1000000007; 31 const int INF = 0x3f3f3f3f; 32 ll powmod (ll a, LL B) {ll res = 1; A % = MOD; For (; B; B >>= 1) {If (B & 1) RES = res * A % MOD; A = A * A % MOD;} return res ;} 33 ll gcd (ll a, LL B) {return B? Gcd (B, A % B): A;} 34 // head 35 36 const int maxn = 50010; 37 int _, lazy [maxn <3], st [maxn], Ed [maxn], cur, M, vis [maxn], n; 38 vector <int> boss [maxn]; 39 40 void DFS (int rt) {// 41 st [RT] = ++ cur; 42 for (INT I = 0; I <boss [RT]. size (); I ++) {43 DFS (Boss [RT] [I]); 44} 45 ed [RT] = cur; 46} 47 48 void Pushdown (int rt) {49 If (lazy [RT]! =-1) {50 lazy [RT <1] = lazy [RT]; 51 lazy [RT <1 | 1] = lazy [RT]; 52 lazy [RT] =-1; 53} 54} 55 56 void build (int rt, int L, int R) {57 lazy [RT] =-1; 58 If (L = r) return; 59 int mid = (L + r)> 1; 60 build (RT <1, L, mid ); 61 build (RT <1 | 1, Mid + 1, R); 62} 63 64 void updata (int rt, int L, int R, int L, int R, int zhi) {65 if (L> = L & R <= r) {66 lazy [RT] = Zhi; 67 return; 68} 69 Pushdown (RT ); 70 int mid = (L + r)> 1; 71 If (L <= mid) Updata (RT <1, L, mid, L, R, zhi); 72 If (r> mid) updata (RT <1 | 1, Mid + 1, R, l, R, zhi); 73} 74 75 int query (int rt, int L, int R, int POS) {76 if (L = r) return lazy [RT]; // Single Point query 77 Pushdown (RT); 78 int mid = (L + r)> 1; 79 if (Pos <= mid) query (RT <1, L, mid, POS); 80 else query (RT <1 | 1, Mid + 1, R, POS ); 81} 82 83 int curr = 1; 84 int main () {85 for (scanf ("% d ",&_);_;_--) {86 printf ("case # % d: \ n", curr ++); 87 cur = 0; 88 MEM (Boss, 0 ); 89 MEM (VIS, 0); 90 scanf ("% d", & N); 91 int U, V; 92 rep (I, 1, n) {// deposit relation 93 scanf ("% d", & U, & V); 94 boss [v]. push_back (U); 95 vis [u] = 1; 96} 97 rep (I, 1, n + 1) {// find the root 98 If (! Vis [I]) {99 DFS (I); 100 break; 101} 102} 103 build (104, cur); // build scanf ("% d ", & M); 105 char s [2]; 106 int POs, Zhi; 107 while (M --) {108 scanf ("% s", S ); 109 If (s [0] = 'T') {110 scanf ("% d", & Pos, & zhi); 111 updata (, cur, st [POS], Ed [POS], zhi ); // range st [POS]-ed [POS] Is POS employee 112} else {113 scanf ("% d", & Pos ); 114 printf ("% d \ n", query (, cur, St [POS]); // query the POs task (Ed [POS] is not) 115} 116} 117} 118}

 

Kuangbin Topic 7 hdu1_4 assign the task (DFS timestamp creation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.