HDU3974 Assign the task(DFS序+线段树)

本文介绍了一种基于多叉树结构的任务分配算法,通过DFS序实现子树更新与查询,解决了公司员工间任务传递的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目大意:给出一种的人员之间上下级的关系,其实就是一棵多叉树,当boss获得一个任务时,boss的员工们也会立即获得这个任务,不论员工原来拥有怎样的任务,查询对于某一时刻某一个员工对怎样的任务。

像是线段树,但是多叉和查询怎样解决,对于这样的子树问题,一下的更新某个根的子树,可以用DFS序,将这棵树重新编号,用入时间戳和出时间戳再将其投映到线段树内,每次更改时将入和出时间戳之间都更新,查询时由于DFS序是先序遍历,所以查询时直接使用入时间戳查询就行。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
const int N=50005;
vector<int>vec[N];
map<int,int >mp;
int tot1;
int in[N],out[N];
struct node
{
    int val;
    int mark;
}tree[N*4];
void pushdown(int i)
{
    if(tree[i].mark!=0)
    {
        tree[i*2].mark=tree[i*2+1].mark=1;
        tree[i*2].val=tree[i*2+1].val=tree[i].val;
        tree[i].mark=0;
    }
}
void build(int l,int r,int i)
{
    tree[i].mark=tree[i].val=0;
    if(l==r)
        return ;
    int mid=(l+r)/2;
    build(l,mid,i*2);
    build(mid+1,r,i*2+1);
}
void updata(int l,int r,int L,int R,int i,int add)
{
    //printf("%d\n",i);
    if(l>=L&&r<=R)
    {
        tree[i].val=add;
        tree[i].mark=1;
        return ;
    }
    pushdown(i);
    int mid=(l+r)/2;
    if(L<=mid)
        updata(l,mid,L,R,i*2,add);
    if(R>mid)
        updata(mid+1,r,L,R,i*2+1,add);
}
int query(int l,int r,int pos,int i)
{
    //printf("%d\n",i);
    if(l==r&&l==pos)
    {
        return tree[i].val;
    }
    pushdown(i);
    int mid=(l+r)/2;
    if(pos<=mid)
        query(l,mid,pos,i*2);
    else
        query(mid+1,r,pos,i*2+1);
    //return 0;
}
void dfs(int root)
{
    in[root]=++tot1;
    for(int i=0;i<vec[root].size();i++)
        dfs(vec[root][i]);
    out[root]=tot1;
}
int main()
{
    int t;
    int ca=1;
    scanf("%d",&t);
    while(t--)
    {
        tot1=0;
        int n;
        scanf("%d",&n);
        mp.clear();
        for(int i=1;i<=n;i++)
            vec[i].clear();
        for(int i=1;i<n;i++)
        {
            int u,v;
            scanf("%d %d",&u,&v);
            mp[u]=1;
            vec[v].push_back(u);
        }
        for(int i=1;i<=n;i++)
        {
            if(!mp[i])
                dfs(i);
        }
        build(1,n,1);
        printf("Case #%d:\n",ca++);
        int q,x,y;
        char str;
        scanf("%d",&q);
        while(q--)
        {
            scanf(" %c ",&str);
            if(str=='C')
            {
                scanf("%d",&x);
                int ans=query(1,n,in[x],1);
                if(ans==0)
                    printf("%d\n",-1);
                else
                    printf("%d\n",ans);
            }
            if(str=='T')
            {
                scanf("%d%d",&x,&y);
                updata(1,n,in[x],out[x],1,y);
            }
/*
            for(int i=1;i<=10;i++)
                printf("*%d\n",tree[i].val);
*/
        }

    }
    return 0;
}

Assign the task

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


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
 

Source


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值