zoukankan      html  css  js  c++  java
  • HDU 3974 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.

    InputThe 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)OutputFor 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 1
     C 3 
    T 3 2 
    C 3
    Sample Output
    Case #1:
    -1 
    1 
    2

    分析:将人物的从属关系,映射到线段树的区间上。
    先找到入度为0的点,代表公司的最大BOSS,然后从中进行DFS序操作,这样使具有从属关系的在同一个区间里,l[u]到r[u]代表它和它所有下属的这段区间,l[u]代表它对应的位置。这样就可以通过线段树的区间修改和单点查询解决问题

    代码如下:

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <vector>
    using namespace std;
    const int MAXN=1e5+100;
    typedef long long ll;
    vector<int>V[MAXN];
    int id[MAXN<<2];
    int add[MAXN<<2];
    int res;
    int t,n,m,x,y;
    int ru[MAXN];
    struct node
    {
        int l;
        int r;
    }tree[MAXN<<2];
    void PushDown(int rt,int m)
    {
        if(add[rt])
        {
          add[rt<<1]=add[rt];
          add[rt<<1|1]=add[rt];
          id[rt<<1]=add[rt];
          id[rt<<1|1]=add[rt];
          add[rt]=0;
        }
    }
    
    void BuildTree(int l,int r,int rt)
    {
        tree[rt].l=l;
        tree[rt].r=r;
        add[rt]=0;
        if(l==r)
        {
          id[rt]=-1;
          return;
        }
        int mid=(tree[rt].l+tree[rt].r)/2;
        BuildTree(l,mid,rt<<1);
        BuildTree(mid+1,r,rt<<1|1);
    }
    
    void Update(int c,int l,int r,int rt)
    {
        if(tree[rt].l==l&&tree[rt].r==r)
        {
            id[rt]=c;
            add[rt]=c;
            return;
        }
        int mid=(tree[rt].l+tree[rt].r)/2;
        PushDown(rt,tree[rt].r-tree[rt].l+1);
        if(r<=mid) Update(c,l,r,rt<<1);
        else if(l>mid)Update(c,l,r,rt<<1|1);
        else
        {
            Update(c,l,mid,rt<<1);
            Update(c,mid+1,r,rt<<1|1);
        }
    }
    
    void Query(int l,int r,int rt)
    {
        if(tree[rt].l==l&&tree[rt].r==r)
        {
          res=id[rt];
          return;
        }
        int mid=(tree[rt].l+tree[rt].r)/2;
        PushDown(rt,tree[rt].r-tree[rt].l+1);
        if(r<=mid)Query(l,r,rt<<1);
        else if(l>mid)Query(l,r,rt<<1|1);
        else
        {
            Query(l,mid,rt<<1);
            Query(mid+1,r,rt<<1|1);
        }
    }
    int u,v,cnt;
    char ch;
    int l[MAXN];
    int r[MAXN];
    void dfs(int u)
    {
      l[u]=++cnt;
      for(int i=0;i<V[u].size();i++)
      {
           dfs(V[u][i]);
      }
      r[u]=cnt;
    }
    int root,Case=0;
    int main()
    {
        scanf("%d",&t);
        while(t--)
        {
           Case++;
           printf("Case #%d:
    ",Case);
           memset(ru,0,sizeof(ru));
           cnt=0;
           scanf("%d",&n);
           for(int i=1;i<=n;i++)
           V[i].clear();
           for(int i=1;i<=n-1;i++)
           {
               scanf("%d%d",&u,&v);
               V[v].push_back(u);
               ru[u]++;
           }
           for(int i=1;i<=n;i++)
           {
               if(ru[i]==0)
               {
                root=i;
                break;
               }
           }
           dfs(root);
           BuildTree(1,cnt,1);
           scanf("%d",&m);
           while(m--)
           {
            scanf(" %c",&ch);
            if(ch=='C')
            {
              scanf("%d",&x);
              Query(l[x],l[x],1);
              printf("%d
    ",res);
            }
            else
            {
              scanf("%d%d",&x,&y);
              Update(y,l[x],r[x],1);
            }
           }
        }
        return 0;
    }




  • 相关阅读:
    C#遍历DataSet中数据的几种方法总结
    angularjs作用域
    SQL 语句日期用法及函数
    Sublime Text3 快捷键汇总
    AngularJS 最常用的功能
    C#中如何排除/过滤/清空/删除掉字符串数组中的空字符串
    AngularJS的指令用法
    [WCF REST] Web消息主体风格(Message Body Style)
    泛型
    语法补充
  • 原文地址:https://www.cnblogs.com/a249189046/p/9658477.html
Copyright © 2011-2022 走看看