Assign the Task
Time Limit: 5000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64u
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 1 2line tree? I didn't see it. According to my solution, it is still the graph theory.
#include"Cstdio"#include"Vector"#include"CString"using namespacestd;Const intmaxn=50005;intTask[maxn];vector<int>BOSS[MAXN];voidAssignintEmintTas) {Task[em]=Tas; for(intI=0; I<boss[em].size (); i++) { intsub=Boss[em][i]; Task[sub]=Tas; Assign (Sub,tas); }}intMain () {intT; scanf ("%d",&T); for(intcas=1; cas<=t;cas++) {memset (task,-1,sizeof(Task)); intN; scanf ("%d",&N); for(intI=0; i<=n;i++) {boss[i].clear (); } for(intI=1; i<=n-1; i++) { intu,v; scanf ("%d%d",&u,&v); Boss[v].push_back (U); } printf ("Case #%d:\n", CAs); intm; scanf ("%d",&m); while(m--) {scanf ("%*c"); Charop; scanf ("%c",&op); if(op=='C') { intem; scanf ("%d",&em); printf ("%d\n", Task[em]); } Else { intx, y; scanf ("%d%d",&x,&y); Assign (x, y); } } }}
HDU3974 (Traversal of the tree)