HDU 3974 Assign the task (並查集改編)

來源:互聯網
上載者:User

連結: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;}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.