HDU 3974 Assign the task(DFS建树+线段树)

HDU 3974 Assign the task

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


4 3 
3 2 
1 3 
5 2 

C 3 
T 2 1
 C 3 
T 3 2 
C 3
Sample Output
Case #1:
-1 

2
7
6

题意:一家公司有N个员工,除了最大的boss外,每一个员工都有一个immediate boss(实在不知道怎么翻译这个了),就是所有员工构成了一颗树,需要给员工分配任务,有两种操作,给其中一位员工分配某一项任务,需要注意的是这个员工和他的下属都必须停下当前的任务,来做这个刚被分配的任务,令一种操作时询问某一位员工当前的任务

线段树的区间更新和区间求和问题,这个题的难点在于如何把这棵员工的关系树转换为区间树,用了DFS,用了两个数组start,end,从最大的boss开始dfs,就拿大boss来说吧,对他进行深搜时先让他的start为1,然后对这颗树回溯一遍回到大boss,此时让他的end为n(已经遍历了一遍了),[1,n]就是大boss的员工对应的区间,对于其他节点也是如此,然后就是线段树的模板题了,使用了延迟标记

#include<stdio.h>
#include<string.h>
const int MAX=50100;
struct NODE{
	int l,r;
	int lazy;
	int val;
}segTree[MAX*4];
struct EDGE{
	int v;
	int next;
}e[MAX];
bool vis[MAX];
int head[MAX],start[MAX],end[MAX];
int cnt_1,cnt_2,change;
void add_edge(int u,int v)
{
	e[cnt_1].v=v;
	e[cnt_1].next=head[u];
	head[u]=cnt_1++;
}
void dfs(int num)
{
	start[num]=++cnt_2;
	for(int i=head[num];i!=-1;i=e[i].next)
	{
		dfs(e[i].v);
	}
	end[num]=cnt_2;
}
void build(int num,int l,int r)
{
	segTree[num].l=l;
	segTree[num].r=r;
	segTree[num].val=-1;
	segTree[num].lazy=0;
	if(l==r) return;
	int mid=(l+r)>>1;
	build(num<<1,l,mid);
	build(num<<1|1,mid+1,r);
}
void pushdown(int num)
{
	if(segTree[num].lazy)
	{
		segTree[num<<1].val=segTree[num].val;
		segTree[num<<1|1].val=segTree[num].val;
		segTree[num<<1].lazy=1;
		segTree[num<<1|1].lazy=1;
		segTree[num].lazy=0;
	}
}
void update(int num,int l,int r)
{
	if(segTree[num].l==l&&segTree[num].r==r)
	{
		segTree[num].val=change;
		segTree[num].lazy=1;
		return;
	}
	pushdown(num);
	int mid=(segTree[num].l+segTree[num].r)>>1;
	if(r<=mid) update(num<<1,l,r);
	else if(l>mid) update(num<<1|1,l,r);
	else{
		update(num<<1,l,mid);
		update(num<<1|1,mid+1,r);
	}
}
int query(int num,int t)
{
	if(segTree[num].l==t&&segTree[num].r==t)
	   return segTree[num].val;
	pushdown(num);  
	int mid=(segTree[num].l+segTree[num].r)>>1;
	if(t<=mid) return query(num<<1,t);
	else return query(num<<1|1,t);
}
int main(void)
{
	int T,N,M;
	scanf("%d",&T);
	int Case=0;
	while(T--)
	{
		printf("Case #%d:\n",++Case);
		cnt_1=0;
		int u,v;
		char op[2];
		memset(head,-1,sizeof(head));
		memset(vis,false,sizeof(vis));
		memset(start,0,sizeof(start));
		memset(end,0,sizeof(end));
		scanf("%d",&N);
		for(int i=1;i<N;i++)
		{
			scanf("%d%d",&u,&v);
			add_edge(v,u);
			vis[u]=true;
		}
		for(int i=1;i<=N;i++)
		{
			if(!vis[i])
			{
				cnt_2=0;
				dfs(i);
				break;
			}	
		}
		build(1,1,cnt_2);
		scanf("%d",&M);
		for(int i=1;i<=M;i++)
		{
			scanf("%s",op);
			if(op[0]=='T')
			{
				scanf("%d%d",&u,&change);
				update(1,start[u],end[u]);
			}
			else{
				scanf("%d",&u);
				printf("%d\n",query(1,start[u]));
			}
		}
	}
	return 0;
}
下面是用并查集做的方法
#include <stdio.h>
#include <algorithm>
using namespace std;
const int MAXN = 5e4 + 10;
int pre[MAXN],val[MAXN],time[MAXN];

int main(void)
{
    int T;
    scanf("%d",&T);
    int kase = 0;
    while(T--)
    {
        int n;
        scanf("%d",&n);
        for(int i = 1; i <= n; i++){
            pre[i] = i;
            val[i] = -1;
            time[i] = -1;
        }
        for(int i = 1; i < n; i++){
            int a,b;
            scanf("%d%d",&a,&b);
            pre[a] = b;
        }
        printf("Case #%d:\n",++kase);
        int q,cnt = 0;
        scanf("%d",&q);
        char op[2];
        while(q--){
            scanf("%s",op);
            //printf("%s--\n",op);
            if(op[0] == 'C'){
                int x;
                scanf("%d",&x);
                int cur = x,ans = val[cur],latt = -1;
                while(cur != pre[cur]){
                    if(time[cur] > latt){
                        latt = time[cur];
                        ans = val[cur];
                    }
                    cur = pre[cur];
                }
                if(time[cur] > latt){
                    latt = time[cur];
                    ans = val[cur];
                }
                printf("%d\n",ans);
            }
            else{
                int x,y;
                scanf("%d%d",&x,&y);
                val[x] = y;
                time[x] = cnt++;
            }
        }
        
    }
    return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值