日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【HDU - 3974】 Assign the task (dfs序 + 线段树维护 区间更新+ 单点查询)

發(fā)布時(shí)間:2023/12/10 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【HDU - 3974】 Assign the task (dfs序 + 线段树维护 区间更新+ 单点查询) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題干:

There is a company that has N employees(numbered from 1 to N),every employee in the company has a immediate boss (except for the leader of whole company).If you are the immediate boss of someone,that person is your subordinate, and all his subordinates are your subordinates as well. If you are nobody's boss, then you have no subordinates,the employee who has no immediate boss is the leader of whole company.So it means the N employees form a tree.?

The company usually assigns some tasks to some employees to finish.When a task is assigned to someone,He/She will assigned it to all his/her subordinates.In other words,the person and all his/her subordinates received a task in the same time. Furthermore,whenever a employee received a task,he/she will stop the current task(if he/she has) and start the new one.?

Write a program that will help in figuring out some employee’s current task after the company assign some tasks to some employee.

Input

The first line contains a single positive integer T( T <= 10 ), indicates the number of test cases.?

For each test case:?

The first line contains an integer N (N ≤ 50,000) , which is the number of the employees.?

The following N - 1 lines each contain two 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 1C 3 T 3 2 C 3

Sample Output

Case #1: -1 1 2

題目大意:

先輸入n個(gè)人,然后是n個(gè)人的關(guān)系(下屬 上司),然后分配任務(wù),每一個(gè)上司分到一個(gè)任務(wù)就把任務(wù)給他的所有下屬做,即他的下屬的任務(wù)都是這個(gè)任務(wù),查詢一個(gè)人該時(shí)刻做的任務(wù)。

解題報(bào)告:

可以建模一下:是給定一棵樹,然后一種操作是指定一個(gè)點(diǎn),這個(gè)點(diǎn)及這個(gè)點(diǎn)的的子樹被染色,另一種操作是指定一個(gè)點(diǎn),問這個(gè)點(diǎn)的顏色。dfs序的實(shí)質(zhì)就是:通過dfs樹將這棵樹放在線段上,然后用線段樹優(yōu)化求解。因?yàn)槭菃吸c(diǎn)查詢所以不需要pushup,因?yàn)槭菂^(qū)間更新并且需要葉子結(jié)點(diǎn)的值,所以需要pushdown。

AC代碼:

#include<bits/stdc++.h>using namespace std; const int MAX = 50000 + 5; int n; int cnt,id; int head[MAX*2]; int q[MAX*4],in[MAX*4],out[MAX*4],rudu[MAX*4]; struct Edge {int to;int ne; } e[MAX*4];//要乘2 嗎? struct TREE {int l,r;int val;int laz; } tree[MAX * 10];void dfs(int x,int root) {q[++id] = x;in[x] = id;for(int i = head[x]; i!=-1; i = e[i].ne) {dfs(e[i].to,x);}out[x] = id; } void add(int u,int v) {e[++cnt].to = v;e[cnt].ne = head[u];head[u] = cnt; } void build(int l,int r,int cur) {tree[cur].l = l;tree[cur].r = r;tree[cur].val = -1;tree[cur].laz = 0;//懶標(biāo)記初始化成0 if(l == r) {return ;}int m = (l + r) /2;build(l,m,cur*2);build(m+1,r,cur*2+1); } void pushup(int cur) {if(tree[cur*2].val == tree[cur*2+1].val) tree[cur].val = tree[cur*2].val;else tree[cur].val = -2;//染色問題 通解? 但是此題是單點(diǎn)查詢所以不需要pushup? } void pushdown(int cur) {if(tree[cur].laz == 0) return ;tree[cur*2].val =tree[cur*2].laz = tree[cur*2+1].laz = tree[cur*2+1].val = tree[cur].laz;tree[cur].laz = 0; } void update(int pl,int pr, int val,int cur) {if(pl <= tree[cur].l && pr >= tree[cur].r) {tree[cur].val = val;tree[cur].laz = val;return ;}pushdown(cur);if(pl <= tree[cur*2].r) update(pl,pr,val,2*cur);if(pr >= tree[cur*2+1].l) update(pl,pr,val,2*cur+1);//pushup(cur); } int query(int tar,int cur) {if(tree[cur].l == tree[cur].r) {return tree[cur].val;}pushdown(cur);if(tar <= tree[cur*2].r) return query(tar,cur*2);else return query(tar,cur*2+1);//pushup(cur); } int main() { // freopen("in.txt","r",stdin);int t;int m,iCase = 0;int u,v;char op[10];cin>>t;while(t--) {printf("Case #%d:\n",++iCase);cnt = id = 0;memset(head,-1,sizeof(head));memset(rudu,0,sizeof rudu);memset(e,0,sizeof(e));scanf("%d",&n);for(int i = 1; i<n; i++) {//讀入n-1次 scanf("%d%d",&u,&v);add(v,u);rudu[u]++;} int st =1;for(int i = 1; i<=n; i++) {if(rudu[i] == 0) {st = i;break;}} dfs(st,0);build(1,n,1);scanf("%d",&m);while(m--) {scanf("%s",op);if(op[0] == 'C') {scanf("%d",&u);printf("%d\n",query(in[u],1));} else {scanf("%d%d",&u,&v);//把以u為根節(jié)點(diǎn)的子樹更改為v update(in[u],out[u],v,1);}}}return 0 ; } //19:15

總結(jié):

? ?注意dfs序,必須從根節(jié)點(diǎn)開始,所以還需要找根節(jié)點(diǎn)!!!這里是用拓?fù)渑判蛑腥攵鹊乃枷胝业母?jié)點(diǎn)。?

??【POJ - 3321】 Apple Tree這道題也是dfs序,但是沒有找根節(jié)點(diǎn),因?yàn)轭}目告訴你了1號節(jié)點(diǎn)是根節(jié)點(diǎn).

本題的數(shù)組都是佛系開的,其實(shí)只是tree需要4倍,其他的都開MAX就夠用。RE了一次是因?yàn)?#xff0c;發(fā)現(xiàn)dfs需要找根節(jié)點(diǎn)而不能直接用1號節(jié)點(diǎn)之后,加了rudu數(shù)組,但是rudu沒有每次初始化,所以re了。。(話說不應(yīng)該wa嗎,搞不懂。。

?

總結(jié)

以上是生活随笔為你收集整理的【HDU - 3974】 Assign the task (dfs序 + 线段树维护 区间更新+ 单点查询)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。