Assign the task dfs序+线段树

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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <cmath>
#include <vector>
using namespace std;
const int NUM=50005;
const int inf=0x3f3f3f3f;
struct point
{
    int l,r,w,f;
}tree[NUM*4];
int in[NUM],out[NUM];
vector<int> vec[NUM];
int time;
void dfs(int a) //dfs序
{
    in[a]=++time;   //在新序列中in[a]对应a点,区间【in[a],out[a]】对应以a为根的子树
    for(int i=0;i<vec[a].size();i++)  
        dfs(vec[a][i]);
    out[a]=time;
} //在dfs序完成后就可以不管之前的树了,用线段树对新序列进行维护就行了
int LL,RR,val;
void build(int k,int LL,int RR)
{
    tree[k].l=LL,tree[k].r=RR;
    tree[k].f=-1,tree[k].w=-1;
    if(tree[k].l==tree[k].r){
        return;
    }
    int bet=(tree[k].l+tree[k].r)/2;
    build(k*2,LL,bet);
    build(k*2+1,bet+1,RR);
}
void down(int k)
{
    tree[k*2].f=tree[k*2+1].f=tree[k].f;
    tree[k*2].w=tree[k*2].f;
    tree[k*2+1].w=tree[k*2+1].f;
    tree[k].f=-1;
}
void change(int k)
{
    if(LL<=tree[k].l&&RR>=tree[k].r){
        tree[k].w=val;
        tree[k].f=val;
        return;
    }
    if(tree[k].f!=-1) down(k);
    int bet=(tree[k].l+tree[k].r)/2;
    if(LL<=bet) change(k*2);
    if(RR>bet) change(k*2+1);
}
int ans;
void ask(int k,int point)
{
    if(tree[k].l==tree[k].r){
        ans=tree[k].w;
        return;
    }
    if(tree[k].f!=-1) down(k);
    int bet=(tree[k].l+tree[k].r)/2;
    if(point<=bet) ask(k*2,point);
    else ask(k*2+1,point);
}
int fp[NUM];
char ss[2];
int main()
{
    int t,cnt=1;
    scanf("%d",&t);
    while(t--){
        memset(fp,0,sizeof(fp));
        time=0;
        int n;
        scanf("%d",&n);
        build(1,1,n);
        for(int i=1;i<=n;i++)
            vec[i].clear();
        int a,b;
        for(int i=0;i<n-1;i++){
            scanf("%d %d",&a,&b);
            fp[a]++;
            vec[b].push_back(a);
        }
        int treepre;
        for(int i=1;i<=n;i++){ //查找树的根节点
            if(fp[i]==0){
                treepre=i;
                break;
            }
        }
        dfs(treepre);
        int m;
        scanf("%d",&m);
        printf("Case #%d:\n",cnt++);
        for(int i=0;i<m;i++){
            scanf("%s",ss);
            if(ss[0]=='C'){
                scanf("%d",&a);
                ask(1,in[a]);
                printf("%d\n",ans);
            }
            else{
                scanf("%d %d",&a,&b);
                LL=in[a],RR=out[a],val=b;
                change(1);
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Chter0/article/details/89351696