Problem-3974
http://acm.hdu.edu.cn/showproblem.php?pid=3974
Description:
There is a company, the have N employees (numbered from 1 to N), every employee in the company have a immediate boss (except F Or the leader of whole company). If you were the immediate boss of Someone,that person was your subordinate, and all he subordinates are your subordinates a s well. If you're nobody ' s boss, then you had no subordinates,the employee who had no immediate boss is the leader of whole comp Any. So it means the N employees form a tree.
The company usually assigns some tasks to some employees to finish. When a task was assigned to Someone,he/she would assigned it to all his/her subordinates. In other words,the person and any his/her subordinates received a task in the same time. Furthermore,whenever a employee received a task,he/she would stop the current task (if he/she have) and start the new one.
Write a program that would help in figuring out some employee's current task after the company assign some tasks to some EM Ployee.
Input:
The first line contains a positive integer t (t <=), indicates the number of test cases.
For each test case:
The first line contains a integer N (n≤50,000), which is the number of the employees.
The following N-1 lines each contain a 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 12
Test instructions: A company has n staff, they may have their own bosses and subordinates, if the company assigns a task to one of the staff, then this task will also be assigned to his subordinates, and each new task assignment, the staff current task can only be a new task, now to query some staff's current task is what, If the task has been unassigned, output-1. (
Although this topic is a line segment tree topic, but because of the problem with the line tree is more troublesome, and not easy to understand, this code for and check set, not a line tree, and then updated again)
#include <stdio.h>#defineN 50010intF[n], Vis[n], task[n], N;//The F array holds the parent node, which is the bossvoidInit () {inti; for(i =0; I <= N; i++) {F[i]=i; Vis[i]=0; Task[i]= -1; }}intFind (intx) { intR = x, V = vis[x], ans = task[x];//v is the task ordinal, ANS is the task while(F[r]! =r) {if(v < Vis[f[r]])//If you find a later ordinal, update the task, and its ordinal number{ans=Task[f[r]]; V=Vis[f[r]]; } R= F[r];//don't forget to always find its parent node, straight from its root node is itself } returnans;}intMain () {intT, M, I, A, b, k =0, Num;//Num records The ordinal of the assigned task, then the current task must be the last assigned Chars[Ten]; scanf ("%d", &T); while(t--) {k++; Num=1; scanf ("%d", &N); Init (); for(i =1; I < n; i++) {scanf ("%d%d", &a, &b); F[a]=b; } scanf ("%d", &m); printf ("Case #%d:\n", K); while(m--) {scanf ("%s", s); if(s[0] =='T') {scanf ("%d%d", &a, &b); Task[a]=b; Vis[a]= num++;//each time you assign a task, write down the serial number } Else if(s[0] =='C') {scanf ("%d", &a); printf ("%d\n", Find (a)); } } } return 0;}
HDU 3974 Assign the task