HDUOJ 3974 Assign the task

HDUOJ 3974 Assign the task

题目链接

Problem Description

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<=1e9)

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

很明显的 DFS序+线段树(区间修改+单点查询)~
我们先对每个点 i i i 求出 DFS 的时间戳 i n [ i ] in[i] in[i] o u t [ i ] out[i] out[i],也即查询的区间 [ i n [ i ] , o u t [ i ] ] [in[i],out[i]] [in[i],out[i]],线段树的部分套板子即可,AC代码如下:

#include
using namespace std;
typedef long long ll;
const int N=5e4+5;
char c;
int n,m,t,u,v,cnt,tree[N<<2],add[N<<2],vis[N],in[N],out[N];
vector<int>g[N];

void init(){
    cnt=0;
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
    memset(vis,0,sizeof(vis));
    memset(tree,0,sizeof(tree));
    for(int i=0;i<=n;i++) g[i].clear();
}

void dfs(int u){
    vis[u]=1;
    in[u]=++cnt;
    for(auto v:g[u]) dfs(v);
    out[u]=cnt;
}

void build(int i,int l,int r)
{
     add[i]=0;
     if(l==r)
     {
         return ;
     }
     int mid=l+(r-l)/2;
     build(i<<1,l,mid);
     build(i<<1|1,mid+1,r);
}

void pushdown(int i)
{
     if(add[i])
     {
         add[i<<1]=add[i];
         add[i<<1|1]=add[i];
         tree[i<<1]=add[i];
         tree[i<<1|1]=add[i];
         add[i]=0;
     }
}

void update(int i,int l,int r,int m,int n,int k)
{
    if(m<=l && r<=n)
    {
        add[i]=k;
        tree[i]=k;
        return ;
    }
    pushdown(i);
    int mid=l+(r-l)/2;
    if(m<=mid) update(i<<1,l,mid,m,n,k);
    if(n>mid) update(i<<1|1,mid+1,r,m,n,k);
}

int query(int i,int l,int r,int m,int n)
{
    if(m<=l && r<=n) return tree[i];
    pushdown(i);
    int mid=l+(r-l)/2,ans=0;
    if(m<=mid) ans+=query(i<<1,l,mid,m,n);
    if(n>mid) ans+=query(i<<1|1,mid+1,r,m,n);
    return ans;
}

int main(){
    scanf("%d",&t);
    for(int i=1;i<=t;i++){
        scanf("%d",&n);
        printf("Case #%d:\n",i);
        init();
        build(1,1,n);
        for(int j=1;j<n;j++){
            scanf("%d%d",&u,&v);
            g[v].push_back(u);
            vis[u]=1;
        }
        for(int j=1;j<=n;j++) if(!vis[j]) {dfs(j);break;}
        scanf("%d",&m);
        while(m--){
           getchar();
            scanf("%c",&c);
            if(c=='C'){
                scanf("%d",&u);
                int ans=query(1,1,n,in[u],in[u]);
                if(ans==0) printf("-1\n");
                else printf("%d\n",ans);
            }else{
                scanf("%d%d",&u,&v);
                update(1,1,n,in[u],out[u],v);
            }
        }
    }
	return 0;
}

你可能感兴趣的:(线段树,DFS,HDUOJ)