并查集--Assign the task

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

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

题意:
一个公司有n个职员,他们可能有自己的上司和下属,公司如果给其中一个职员分配任务,那么这个任务也会同时分配给他的下属,而且每个新任务分配时,此职员当前任务只能是新任务,现在要查询一些职员的当前任务是什么,若一直未分配任务则输出-1。

测试样例分析:

#include<stdio.h>
#define N 50010

int fa[N], vis[N], task[N]; 
//fa数组保存父节点,即上司
//task[i] 代表分配给i的任务为task[i] 
//vis数组用于记录任务分配的优先级 
int find(int x){
    int r = x, v = vis[x], Working_task = task[x]; 
	//v是任务序号,ans是任务
    while(fa[r] != r){
        if(v<vis[fa[r]]){ //如果找到更靠后的序号,那就更新任务,及其序号
            Working_task = task[fa[r]];
            v = vis[fa[r]];
        }
        r = fa[r]; //一直查找其父节点,直自其根节点是本身
    }
    return Working_task;
}

int main(){
    int T, num; //num记录的是分配任务的序号,那么当前任务一定是最后分配的
    
	int num_Test = 0,num_Message;
	
    scanf("%d",&T);//T组测试样例 
    while(T--){
        num_Test++;
        num = 1;
        int n;
        scanf("%d",&n);//n个人 
        //初始化 
    	for(int i = 0;i<=n;i++){
        	fa[i] = i;//每个点都是独立的 
        	vis[i] = 0;//记录访问i的次数 
        	task[i] = -1; //记录i当前任务 
    	}
        //输入 
        for(int i = 1;i<n;i++){//输入关系 
        	int a,b;
            scanf("%d%d",&a,&b);//b是a的上司 
            fa[a] = b;
        }
        scanf("%d",&num_Message);//输入信息数量 
        
		printf("Case #%d:\n", num_Test);
        while(num_Message--){
		 	char operate[2];//操作符:C/T 
		 	
            scanf("%s", operate);
            if(operate[0] == 'T'){ //T为输入数据 
            	int a,b;
                scanf("%d%d",&a,&b); 
                task[a] = b;
                vis[a] = num++;//每次分配任务,要记下序号,序号越往后,代表越新的
				 
            }
            else if(operate[0] == 'C'){
            	int a;
                scanf("%d",&a);
                printf("%d\n", find(a));
            }
        }
    }
    return 0;
}

网上好多用的是dfs+线段树,奈何我看了一上午,还是没看懂(T^T),待我学了线段树,再回来看看吧

#include<stdio.h>
#include<string.h>
#include<queue>
#define inf 0x3f3f3f3f
#define MAXN 50005
#define ll long long
 
using namespace std;

int n,m;
int ord[MAXN*4],root,tot,tmp[MAXN*4],ed[MAXN*4];
int laz[MAXN*4];
bool hasIn[MAXN];
vector<int> vec[MAXN];

/*
//假设root --> a --> b 
	       +->c
	dfs(root){
		tmp[root] = 1; ord[1] = root;
		for(int i = 0;i<2;i++){  //vec[root].size() = 2
		//第一轮 
			int v = a;	//int v = vec[root][0];
			
			dfs(a){ //dfs(a);
				tmp[a] = 2; ord[2] = a; 
				for(int i = 0;i<1;i++){//vec[a].size() = 1 
					int v = c;//int v=vec[a][0];
					
					dfs(c){
						tmp[c] = 3; ord[3] = c;
						for{...} //不符合直接跳过
						ed[c] = 3;//tot = 3 
					}
				
				}
			}
		//第二轮 
			int v = b; //int v = vec[root][1];
			
			dfs(b){
				tmp[b] = 4; ord[4] = b;
				for{...} //不符合直接跳过
				ed[b] = 4; 
			}
		}
		ed[root] = 4
	}
*/
void dfs(int u){//开始传进根节点 

    tmp[u]=++tot;
    ord[tot]=u;
    for(int i=0;i<vec[u].size();i++){
        int v=vec[u][i];
        dfs(v);
    }
    ed[u]=tot;
}


void pushdown(int v)
{
    laz[v*2]=laz[v];
    laz[v*2+1]=laz[v];
    laz[v]=-1;
}
void updata(int v,int L,int R,int ql,int qr,int d)
{
    if(ql<=L && R<=qr)
    {
        laz[v]=d;
        return;
    }
    if(laz[v]!=-1) pushdown(v);
    int mid=(L+R)/2;
    if(ql<=mid) updata(v*2,L,mid,ql,qr,d);
    if(qr>mid) updata(v*2+1,mid+1,R,ql,qr,d);
}

/*
//还是假设 root --> a --> b ,假设查询 b节点 
		   +-> c
	int query(1,1,4,4){	//v = 1;L = 1;R = 4;q = tmp[b]=4;			
		if(1==4){...} //不满足 
		if(laz[1]!=-1){...} //laz[1] = -1;不满足 
		int mid = 1; //(1+1)/2
		if(4<=1){...} //不满足
		else
			return query(3,2,4,4){
		
		
		}
	}			
*/
int query(int v,int L,int R,int q)
{
    if(L==R) 
		return laz[v];
    if(laz[v]!=-1) 
		pushdown(v);
    int mid=(L+R)/2;
    if(q<=mid) 
		return query(v*2,L,mid,q);
    else 
		return query(v*2+1,mid+1,R,q);
}


int main(){
    int t; //t组测试样例 
    scanf("%d",&t);
    for(int num_t=1;num_t<=t;num_t++){
        printf("Case #%d:\n",num_t);
        
        scanf("%d",&n);//输入总节点数 
        //初始化 
        for(int i = 1;i<=n;i++)
            vec[i].clear();
        memset(hasIn,false,sizeof(hasIn));
        memset(tmp,0,sizeof tmp);
        memset(laz,-1,sizeof laz);
        tot=0;
        
		for(int i = 1;i<n;i++){
            int u,v;//用于表示输入的两个数 
            scanf("%d%d",&u,&v);// v --> u
            vec[v].push_back(u);//vec[v]存放所有v所指向的节点 
            hasIn[u]=true;
        }
        for(int i = 1;i<=n;i++)//扫描所有节点,找出没有入度的节点 
            if(!hasIn[i]){//没有入度,代表是根节点 
            	root=i;
            	break;
        	}

        dfs(root);//深搜根节点 
        char operate[2];//操作符号:C/T 
        
        scanf("%d",&m);//m个信息 
        while(m--){
            scanf("%s",operate);
            if(operate[0]=='C'){
            //C为查询,则输入查询节点u
				int u; 
                scanf("%d",&u);
                printf("%d\n",query(1,1,tot,tmp[u]));
            }
            else if(operate[0]=='T'){
                int u,d;
                scanf("%d%d",&u,&d);
                updata(1,1,tot,tmp[u],ed[u],d);
            }
        }
    }
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值