开始被吓到了,以为要用什么动态树什么高深的东东。。。后来想了想跟线段树蛮像。。再后来想了想,并查集就可以完全搞定了。
因为这是一棵树哈,所以你在每个节点里记录任务以及这个任务的时间。查询的时候查当前节点所有的父节点(包括它本身),找最迟更新的(也就是离查询最近的)那个任务即可。
少写了句更新,下午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;
}