zoukankan      html  css  js  c++  java
  • hdu3974 Assign the task dfs序+线段树

    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.、

    题意:

    某公司员工上下级关系呈现树形结构,现在有任务需要分配,某个员工获得任务后,他的所有下属都会转为做该任务。现在需要分配任务以及查询某个员工正在做的任务。

    将树形结构用dfs序的方法转变为线性结构,因此他的所有子树内部节点的编号均会在他的进入以及离开编号之间。这样就可以进行线段树区间修改和单点查询了。

      1 #include<stdio.h>
      2 #include<string.h>
      3 const int maxm=5e4+5;
      4 
      5 int head[maxm],nxt[maxm],point[maxm],size;
      6 bool f[maxm];
      7 int t,stx[maxm],edx[maxm];
      8 int st[maxm<<2],ch[maxm<<2];
      9 
     10 void add(int a,int b){
     11     point[size]=a;
     12     nxt[size]=head[b];
     13     head[b]=size++;
     14 }
     15 
     16 void dfs(int s){
     17     stx[s]=++t;
     18     for(int i=head[s];~i;i=nxt[i]){
     19         int j=point[i];
     20         dfs(j);
     21     }
     22     edx[s]=t;
     23 }
     24 
     25 void pushdown(int o){
     26     if(ch[o]!=-1){
     27         ch[o<<1]=ch[o];
     28         ch[o<<1|1]=ch[o];
     29         st[o<<1]=ch[o];
     30         st[o<<1|1]=ch[o];
     31         ch[o]=-1;
     32     }
     33 }
     34 
     35 void pushup(int o){
     36     if(st[o<<1]==st[o<<1|1])st[o]=st[o<<1];
     37     else st[o]=-2;
     38 }
     39 
     40 void update(int o,int l,int r,int ql,int qr,int c){
     41     if(ql<=l&&qr>=r){
     42         ch[o]=c;
     43         st[o]=c;
     44         return;
     45     }
     46     pushdown(o);
     47     int m=l+((r-l)>>1);
     48     if(ql<=m)update(o<<1,l,m,ql,qr,c);
     49     if(qr>=m+1)update(o<<1|1,m+1,r,ql,qr,c);
     50     pushup(o);
     51 }
     52 
     53 int query(int o,int l,int r,int ind){
     54     if(st[o]!=-2)return st[o];
     55     if(l==r)return st[o];
     56     pushdown(o);
     57     int m=l+((r-l)>>1);
     58     if(ind<=m)return query(o<<1,l,m,ind);
     59     return query(o<<1|1,m+1,r,ind);
     60 }
     61 
     62 char s[10];
     63 
     64 int main(){
     65     int T,cnt=0;
     66     scanf("%d",&T);
     67     while(T--){
     68         memset(head,-1,sizeof(head));
     69         size=0;
     70         memset(f,0,sizeof(f));
     71         t=0;
     72         int n;
     73         scanf("%d",&n);
     74         for(int i=1;i<n;++i){
     75             int a,b;
     76             scanf("%d%d",&a,&b);
     77             f[a]=1;
     78             add(a,b);
     79         }
     80         for(int i=1;i<=n;++i){
     81             if(!f[i]){
     82                 dfs(i);
     83                 break;
     84             }
     85         }
     86         memset(st,-1,sizeof(st));
     87         memset(ch,-1,sizeof(ch));
     88         printf("Case #%d:
    ",++cnt);
     89         int m;
     90         scanf("%d",&m);
     91         for(int i=1;i<=m;++i){
     92             scanf("%s",s);
     93             if(s[0]=='C'){
     94                 int a;
     95                 scanf("%d",&a);
     96                 printf("%d
    ",query(1,1,t,stx[a]));
     97             }
     98             else if(s[0]=='T'){
     99                 int a,b;
    100                 scanf("%d%d",&a,&b);
    101                 update(1,1,t,stx[a],edx[a],b);
    102             }
    103         }
    104     }
    105     return 0;
    106 }
    View Code
  • 相关阅读:
    luogu P1486 [NOI2004]郁闷的出纳员
    Luogu P1894 [USACO4.2]The Perfect Stall
    关于中间6个月停更通知
    Luogu P1381油滴扩展
    没有上司的舞会(题解)
    幂的模运算(题解)
    闭合区域面积统计(题解)
    字符序列(题解)
    最大连续和(题解)
    排列问题
  • 原文地址:https://www.cnblogs.com/cenariusxz/p/6592568.html
Copyright © 2011-2022 走看看