Hdustm4 ---- assign the task

Source: Internet
Author: User
Assign the task Time Limit: 15000/5000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 780 accepted submission (s): 392


Problem descriptionthere 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
 
Source2011 multi-university training contest 14-host by fzu
Recommend

First, change the DFS timestamp. Then, the subtree intervals are connected, and then the Interval Update spof query is performed.

/*************************************** * *********************************> File Name: hdu3974.cpp> author: Alex> mail: [email protected]> created time: ******************************** **************************************** /# include <map> # include <set> # include <queue> # include <stack> # include <vector> # include <cmath> # include <cstdio> # include <cstdlib> # include <cstring> # Include <iostream> # include <algorithm> using namespace STD; const int n = 50110; struct node {int L, R; int add; int job ;} tree [n <2]; vector <int> edge [N]; int s [N], E [N], CNT; int deg [N]; void DFS (int u) {int M = edge [u]. size (); s [u] = ++ CNT; For (INT I = 0; I <m; ++ I) {int v = edge [u] [I]; DFS (V);} e [u] = CNT;} void Pushdown (INT p) {If (tree [p]. add! =-1) {tree [P <1]. add = tree [P <1 | 1]. add = tree [p]. add; tree [P <1]. job = tree [P <1 | 1]. job = tree [p]. add; tree [p]. add =-1 ;}} void build (int p, int L, int R) {tree [p]. L = L; tree [p]. R = r; tree [p]. add =-1; tree [p]. job =-1; if (L = r) {return;} int mid = (L + r)> 1; build (P <1, L, mid ); build (P <1 | 1, Mid + 1, R);} void Update (int p, int L, int R, int Val) {If (L = tree [p]. L & R = tree [P]. r) {tree [p]. job = val; tree [p]. add = val; return;} Pushdown (p); int mid = (tree [p]. L + tree [p]. r)> 1; if (r <= mid) {Update (P <1, L, R, Val);} else if (L> mid) {Update (P <1 | 1, L, R, Val) ;}else {Update (P <1, L, mid, Val ); update (P <1 | 1, Mid + 1, R, Val) ;}} int query (int p, int POS) {If (tree [p]. L = tree [p]. r) {return tree [p]. job;} Pushdown (p); int mid = (tree [p]. L + tree [p]. r)> 1; if (PO S <= mid) {return query (P <1, POS) ;}else {return query (P <1 | 1, POS) ;}} int main () {int t, n, m; int icase = 1; char op [10]; int X, Y; scanf ("% d", & T); While (t --) {scanf ("% d", & N); memset (deg, 0, sizeof (DEG); For (INT I = 0; I <= N; ++ I) {edge [I]. clear () ;}int U, V; For (INT I = 1; I <= n-1; ++ I) {scanf ("% d ", & U, & V); edge [v]. push_back (U); deg [u] ++;} int root; For (INT I = 1; I <= N; ++ I) {If (! DEG [I]) {root = I; break;} CNT = 0; DFS (Root); Build (1, 1, CNT); scanf ("% d ", & M); printf ("case # % d: \ n", icase ++); While (M --) {scanf ("% s", OP ); if (OP [0] = 'C') {scanf ("% d", & X); printf ("% d \ n", query (1, s [x]);} else {scanf ("% d", & X, & Y); int L = s [x]; int r = E [X]; update (1, L, R, Y) ;}} return 0 ;}


Hdustm4 ---- assign the task

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.