Link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3974
This question is like this:
Give you n, meaning there are n people
Then the next n rows are the relationships between these people.
Two digits indicate that the latter is the boss of the former.
Then I will give you another M, which means there are m cases below
When you encounter C, you need to output the task that the manager of the number following current C is currently doing.
If you encounter T, for example, T 2 1, you need to assign Task 1 to the Manager 2. In addition, all his subordinates need to change to this task immediately.
Therefore, when C occurs, it is possible to ask the manager about the current task.
The processing method is to delay the mark.
Write down the task of the last parent manager who changed the task
The AC code is as follows (for more information, please forgive me. If you see something similar, please do not spray it. I apologize here ):
#include <stdio.h>#include <iostream>using namespace std;const int MAX = 50010;struct NODE{int task,count;};NODE p[MAX];int parent[MAX];void init(){memset(parent,-1,sizeof(parent));}int main(){int ncases,n,u,v,m,x,task;char s[5];scanf("%d",&ncases);int ind=1;while(ncases--){scanf("%d",&n);init();int i;for(i=0;i<n-1;i++){scanf("%d%d",&u,&v);parent[u]=v;}for(i=1;i<=n;i++){p[i].task=-1;p[i].count=-1;}scanf("%d",&m);printf("Case #%d:\n",ind++);int count=0;while(m--){scanf("%s",s);if(s[0]=='T'){scanf("%d%d",&x,&task);p[x].task=task;p[x].count=++count;}else{scanf("%d",&x);task=p[x].task;int count=p[x].count;while(x!=-1){if(p[x].count>count){task=p[x].task;count=p[x].count;}x=parent[x];}printf("%d\n",task);}}}return 0;}