# HDU - 3974 DFS建树（DFS序）&线段树

DFS序：由于题目给定的节点之间的关系可得一个树以及树的根节点，由根节点遍历完整颗树得到的序列就是dfs序。

#include<iostream>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
struct node{
int l,r;
int work,lazy;
}tree[4*50005];
vector<int>map[50005];
int len,vis[50005],ll[50005],rr[50005];
void dfs(int x){
++len;
vis[x]=1;
ll[x]=len;
for(int i=0;i<map[x].size();i++){
dfs(map[x][i]);
}
rr[x]=len;
}
void build(int i,int l,int r){
tree[i].l=l;tree[i].r=r;
tree[i].work=-1;tree[i].lazy=0;
if(l==r)
return;
build(i<<1,l,(int)floor((r+l)/2.0));
build((i<<1)+1,(int)floor((r+l)/2.0)+1,r);
}
void pushdown(int i){
tree[i<<1].lazy=tree[(i<<1)+1].lazy=1;
tree[i<<1].work=tree[(i<<1)+1].work=tree[i].work;
tree[i].lazy=0;
}
int ans=0;
void que(int i,int id){
if(tree[i].l==tree[i].r&&tree[i].l==id){
ans=tree[i].work;
return;
}
if(tree[i].lazy){
pushdown(i);
}
int mid=(tree[i].l+tree[i].r)>>1;
if(id<=mid){
que(i<<1,id);
}
else{
que((i<<1)+1,id);
}
}
void update(int i,int l,int r,int w){
if(tree[i].l>=l&&tree[i].r<=r){
tree[i].work=w;
tree[i].lazy=1;
return;
}
if(tree[i].r<l||tree[i].l>r){
return;
}
if(tree[i].lazy){
pushdown(i)	;
}
update(i<<1,l,r,w);
update((i<<1)+1,l,r,w);
}
int main(){
int t,flag=1;
cin>>t;
while(t--){
int n,m;
cin>>n;
len=0;
memset(vis,0,sizeof(vis));
//        memset(ll,0,sizeof(ll));
//        memset(rr,0,sizeof(rr));
for(int i=1;i<=n;i++)
map[i].clear();
for(int i=1;i<n;i++){
int u,v;
cin>>v>>u;
map[u].push_back(v);
vis[v]=1;
//			cout<<"asdf"<<endl;
}
for(int i=1;i<=n;i++){
if(!vis[i]){
dfs(i);
break;
}
}
//		cout<<len<<endl;
build(1,1,len);
char s[5];
cout<<"Case #"<<flag++<<":"<<endl;
cin>>m;
while(m--){
cin>>s;
if(s[0]=='C'){
int id;
cin>>id;
ans=-1;
que(1,ll[id]);
cout<<ans<<endl;
}
else{
int id,wo;
cin>>id>>wo;
update(1,ll[id],rr[id],wo);
}
}
}
return 0;
}