1 5 4 3 3 2 1 3 5 2 5 C 3 T 2 1 C 3 T 3 2 C 3
Case #1: -1 1 2
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int maxn=50000+100;
struct node{
int val;
int time;
}e[maxn];
int pre[maxn];
int t,n,m;
int main()
{
int x,y;
int cas=1;
char str[3];
scanf("%d",&t);
while(t--)
{
CLEAR(pre,-1);
scanf("%d",&n);
REPF(i,1,n)
{
e[i].val=-1;
e[i].time=-1;
}
REPF(i,1,n-1)
{
scanf("%d%d",&x,&y);
pre[x]=y;
}
scanf("%d",&m);
int T=0;
printf("Case #%d:\n",cas++);
while(m--)
{
scanf("%s",str);
if(str[0]=='T')
{
scanf("%d%d",&x,&y);
e[x].val=y;
e[x].time=++T;
}
else
{
scanf("%d",&x);
int val=e[x].val;
int time=e[x].time;
while(x!=-1)
{
if(e[x].time>time)
{
val=e[x].val;
time=e[x].time;
}
x=pre[x];
// cout<<"fuck "<<endl;
}
printf("%d\n",val);
}
}
}
return 0;
}
原文:http://blog.csdn.net/u013582254/article/details/42806475