HDU3974 Assign the task(dfs建树+线段树)

Assign the task

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4603    Accepted Submission(s): 1849


Problem Description
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 1 C 3 T 3 2 C 3
 

Sample Output
 
  
Case #1: -1 1 2

题意:一个公司里面每个员工都有一个顶头上司,给某个员工分配任务后,这个员工以及该员工的所有下属都在做该任务。有m个操作,操作分别是分配给员工任务,查询该员工正在执行的任务。

看了题解再知道怎么做,仔细想了下,这本来是个多叉树,而询问的内容像一个线段树,想要用线段树来解决问题必须要考虑的怎么建树,那么dfs用来重新建图是个很好的选择,我们用dfs来把每个员工的子员工(子树)来算一下个数,看这个员工的管辖区域,并把这个员工打上属于他自己的编号。我们就用编号总数建图。

代码上有一些备注可以帮助理解

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
//#define ll long long
const int maxn=50005;
struct node
{
    int u,v,next;
} e[maxn];
int sum[maxn<<2],add[maxn<<2];
int ll[maxn],rr[maxn];
int first[maxn],fa[maxn];
int r,num,n;
void addd(int u,int v)
{
    e[r].v=u;
    e[r].next=first[v];
    first[v]=r++;
}
void init()
{
    r=0;
    num=0;
    memset(first,-1,sizeof(first));
    for(int i=0; i<=n; i++) fa[i]=i;
}
int roott(int x)//并查集里的找根
{
    if(fa[x]==x)
    return fa[x];
    return roott(fa[x]);
}
void dfs(int x)//用dfs来给员工打上时间戳,并把这个员工管辖的子员工统计下
{
    num++;//时间戳,其实就是编号
    ll[x]=num;//管辖子员工的左端点
    for(int i=first[x];i!=-1;i=e[i].next)
        dfs(e[i].v);
    rr[x]=num;//右端点
}
void pushdown(int rt)
{
    if(add[rt]!=-1)
    {
        sum[rt<<1]=add[rt];
        sum[rt<<1|1]=add[rt];
        add[rt<<1]=add[rt];
        add[rt<<1|1]=add[rt];
        add[rt]=-1;
    }
}
void build(int l,int r,int rt)
{
    sum[rt]=-1;
    add[rt]=-1;
    if(r==l)
        return ;
    int mid=(l+r)>>1;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
}
void update(int L,int R,int C,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        sum[rt]=C;
        add[rt]=C;
        return ;
    }
    pushdown(rt);
    int mid=(l+r)>>1;
    if(L<=mid) update(L,R,C,l,mid,rt<<1);
    if(R>mid) update(L,R,C,mid+1,r,rt<<1|1);
}
void query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        printf("%d\n",sum[rt]);
        return ;
    }
    pushdown(rt);
    int mid=(l+r)>>1;
    if(L<=mid) query(L,R,l,mid,rt<<1);
    if(R>mid) query(L,R,mid+1,r,rt<<1|1);
}
int main()
{
    int t,casee=0,u,v,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        init();
        for(int i=0; i<n-1; i++)
        {
            scanf("%d%d",&u,&v);
            fa[u]=v;
            addd(u,v);
        }
        int root=roott(1);
//        printf("%d\n",root);
        dfs(root);
        build(1,num,1);//算出了num就是时间戳的数,我们可以通过看某个员工的管辖区域来操作线段树
        scanf("%d",&m);
        char a[10];
        int x,y;
        printf("Case #%d:\n",++casee);
        while(m--)
        {
            scanf("%s%d",a,&x);
            if(a[0]=='C')
                query(ll[x],ll[x],1,num,1);
            else
            {
                scanf("%d",&y);
                update(ll[x],rr[x],y,1,num,1);
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/zezzezzez/article/details/80431560