HDU - 3974 Assign the task(DFS序+线段树维护)

题目链接

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

题意:

有1-N,N个人,雇员v是雇员u的直接老板,雇员u的下属也是雇员v的下属,现在公司对雇员x分配任务,每一个雇员都会放下手中的任务,并且会把任务分摊给下属。现在要求查询雇员x当前的工作。

思路:

从题意中可知,我们根据老板与下属之间的关系可以建立一颗树,要对老板及其下属分配任务,由于他们是一个无序的状态,所以很难通过线段树进行操作,那么我们是不是可以将无序的状态通过重新的标记变成有序的序列。

我们可以通过DFS,对每一个雇员进行一个有序标记。这样,对于每一个根节点,类似于二叉树的前序遍历,我们就会知道当前标记与它的下属数量,这样我们就得到一个老板与下属关系的区间[l,r],对这个区间染色即可。

为老板们献上代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
#define MAXN 50010
int vis[MAXN],tree[MAXN<<2];
int head,cnt;
vector<int>g[MAXN];
struct NODE
{
    int l,r;
}node[MAXN];
void init()
{
    for(int i=0;i<=MAXN;i++)    g[i].clear();
    memset(vis,0,sizeof(vis));
    memset(tree,-1,sizeof(tree));
    cnt=0;
}

void DFS(int x)     //对这棵树进行有序的标记。
{
    cnt++;
    node[x].l=cnt;
    for(int i=0;i<g[x].size();i++){
        DFS(g[x][i]);
    }
    node[x].r=cnt;
}

void pushdown(int rt)
{
    tree[rt<<1] = tree[rt<<1|1] = tree[rt];
    tree[rt]=-1;
    return ;
}

void build(int rt,int L,int R,int l,int r,int x)
{
    if(l<=L && R<=r){
        tree[rt]=x;
        return ;
    }
    if(tree[rt]!=-1)    pushdown(rt);
    int Mid = (L+R)>>1;
    if(l <= Mid)    build(rt<<1,L,Mid,l,r,x);
    if(Mid < r) build(rt<<1|1,Mid+1,R,l,r,x);
}

void query(int rt,int L,int R,int x)
{
    if(L == R){
        cout<<tree[rt]<<endl;
        return ;
    }
    if(tree[rt]!=-1)    pushdown(rt);
    int Mid = (L+R)>>1;
    if(x<=Mid)  query(rt<<1,L,Mid,x);
    else query(rt<<1|1,Mid+1,R,x);
}

int main()
{
    int T,cas=0;
    scanf("%d",&T);
    while(T--){
        cout<<"Case #"<<++cas<<":"<<endl;
        init();
        int n,m;
        scanf("%d",&n);
        for(int i=0;i<n-1;i++){
            int x,y;
            scanf("%d%d",&x,&y);
            g[y].push_back(x);
            vis[x]=1;
        }
        for(int i=1;i<=n;i++)   if(!vis[i]) head=i;	//找到这棵树的根节点
        DFS(head);      //对这颗树进行有序的标记
        scanf("%d",&m);
        while(m--){
            char ch;
            getchar();
            scanf("%c",&ch);
            if(ch == 'C'){
                int x;
                scanf("%d",&x);
                query(1,1,n,node[x].l);
            }
            if(ch == 'T'){
                int x,y;
                scanf("%d%d",&x,&y);
                build(1,1,n,node[x].l,node[x].r,y);
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值