連結:http://acm.hdu.edu.cn/showproblem.php?pid=3974
這道題目的意思是這樣的:
給你一個N,說明有N個人
然後接下來N行是這些人的關係
兩個數字表示後者是前者的boss
然後再給你一個M,說明下面有M種情形
遇到C,你就要輸出當前C後面跟著的那個號數的經理當前在做的任務。
遇到T,例如:T 2 1,你就要把任務1給2經理,而且,他的下屬都要馬上更換成這個任務。
所以出現C的時候,有可能問的很後面的經理當前的任務。
處理的方法是延後標記
記下最後一次改變任務的父親經理的任務
AC代碼如下(有參考大神的,忘記是哪位了,如果有看到神似的話,請不要噴,這裡道歉下):
#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;}