hdu 3974 Assign the task

开始被吓到了,以为要用什么动态树什么高深的东东。。。后来想了想跟线段树蛮像。。再后来想了想,并查集就可以完全搞定了。

因为这是一棵树哈,所以你在每个节点里记录任务以及这个任务的时间。查询的时候查当前节点所有的父节点(包括它本身),找最迟更新的(也就是离查询最近的)那个任务即可。

少写了句更新,下午WAshi了 = =。。。

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define BUG puts("here!!!")
#define STOP system("pause")

using namespace std;

const int MAX = 50010;
struct NODE{
	int col, t;
};
NODE p[MAX];
int pre[MAX];

void init()
{
	memset(pre, -1, sizeof(pre));
}
int main()
{
	int ncases, n, u, v, m, x, col;
	char s[5];
	
	scanf("%d", &ncases);
	
	int ind = 1;
	while( ncases-- )
	{
		scanf("%d", &n);
		init();
		for(int i=0; i<n-1; i++)
		{
			scanf("%d%d", &u, &v);
			pre[u] = v;
		}
		
		for(int i=1; i<=n; i++)
		{
			p[i].col = -1;
			p[i].t = -1;
		}
		
		scanf("%d", &m);
		
		printf("Case #%d:\n",ind++);
		int T = 0;
		while( m-- )
		{
			scanf("%s", s);
			if( s[0] == 'T' )
			{
				scanf("%d%d", &x, &col);
				p[x].col = col;
				p[x].t = ++T;
			}
			else
			{
				scanf("%d", &x);
				col = p[x].col;
				int t = p[x].t;
				while( x != -1 )
				{
					if( p[x].t > t )
					{
						col = p[x].col;
						t = p[x].t;
					}
					x = pre[x];
				}
				printf("%d\n", col);
			}
		}
	}

return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值