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
题解:用入度为0的点当作根节点DFS建立线段树。
vector版本:
#include<iostream>
#include<vector>
#include<cstring>
#include<stdio.h>
using namespace std;
const int N=50050;
int start[N],end_[N];
int sum[N<<2],lazy[N<<2];
int vis[N];
int t,n,m,x,y,tot;
vector<int>G[N];
void DFS(int rt) //DFS建树,
{
start[rt]=++tot; //为1
for(int i=0;i<G[rt].size();i++)
DFS(G[rt][i]);
end_[rt]=tot; //为n
}
void build(int l,int r,int rt)
{
sum[rt]=-1;
lazy[rt]=0;
if(l==r)
return ;
int mid=(l+r)>>1;
build(l,mid,rt*2);
build(mid+1,r,rt*2+1);
}
void pushdown(int rt)
{
if(lazy[rt])
{
lazy[rt*2]=lazy[rt*2+1]=lazy[rt];
sum[rt*2]=sum[rt*2+1]=lazy[rt];
lazy[rt]=0;
}
}
void update(int L,int R,int w,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
sum[rt]=w;
lazy[rt]=w;
return ;
}
pushdown(rt);
int mid=(l+r)/2;
if(L<=mid)
update(L,R,w,l,mid,rt*2);
if(R>=mid+1)
update(L,R,w,mid+1,r,rt*2+1);
}
int query(int p,int l,int r,int rt)
{
if(l==r)
return sum[rt];
pushdown(rt);
int mid=(l+r)/2;
if(p<=mid)
return query(p,l,mid,rt*2);
else
return query(p,mid+1,r,rt*2+1);
}
int main()
{
int k=1;
scanf("%d",&t);
while(t--)
{
memset(vis,0,sizeof(vis));
scanf("%d",&n);
for(int i=1;i<=n;i++)
G[i].clear();
for(int i=1;i<=n-1;i++)
{
int u,v;
scanf("%d%d",&u,&v);
G[v].push_back(u); //这是v是u的老板,则从v到u有一条有向边
vis[u]=1; //标记有入度的点
}
tot=0;
for(int i=1;i<=n;i++)
{
if(vis[i]==0) //无入度的点为树根
{
DFS(i);
break;
}
}
build(1,n,1);
scanf("%d",&m);
cout<<"Case #"<<k++<<":"<<endl;
for(int i=1;i<=m;i++)
{
char s[10];
cin>>s;
if(s[0]=='C')
{
scanf("%d",&x);
cout<<query(start[x],1,n,1)<<endl;
}
else if(s[0]=='T')
{
scanf("%d%d",&x,&y);
update(start[x],end_[x],y,1,n,1);
}
}
}
return 0;
}
前向星版本:
#include<iostream>
#include<vector>
#include<cstring>
#include<stdio.h>
using namespace std;
const int N=50050;
int start[N],end_[N];
int sum[N<<2],lazy[N<<2];
int vis[N],head[N];
int t,n,m,x,y,tot;
struct Edge
{
int v,next;
}e[N];
void add(int u,int v)
{
e[tot].v=u;
e[tot].next=head[v];
head[v]=tot++;
}
void DFS(int rt)
{
start[rt]=++tot;
for(int i=head[rt];i!=-1;i=e[i].next)
DFS(e[i].v);
end_[rt]=tot;
}
void build(int l,int r,int rt)
{
sum[rt]=-1;
lazy[rt]=0;
if(l==r)
return ;
int mid=(l+r)/2;
build(l,mid,rt*2);
build(mid+1,r,rt*2+1);
}
void pushdown(int rt)
{
if(lazy[rt])
{
lazy[rt*2]=lazy[rt*2+1]=lazy[rt];
sum[rt*2]=sum[rt*2+1]=lazy[rt];
lazy[rt]=0;
}
}
void update(int L,int R,int w,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
sum[rt]=w;
lazy[rt]=w;
return ;
}
pushdown(rt);
int mid=(l+r)/2;
if(L<=mid)
update(L,R,w,l,mid,rt*2);
if(R>=mid+1)
update(L,R,w,mid+1,r,rt*2+1);
}
int query(int p,int l,int r,int rt)
{
if(l==r)
return sum[rt];
pushdown(rt);
int mid=(l+r)/2;
if(p<=mid)
return query(p,l,mid,rt*2);
else
return query(p,mid+1,r,rt*2+1);
}
int main()
{
int k=1;
scanf("%d",&t);
while(t--)
{
memset(start,0,sizeof(start));
memset(end_,0,sizeof(end_));
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
scanf("%d",&n);
tot=0;
for(int i=1;i<=n-1;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
vis[u]=1;
}
tot=0;
for(int i=1;i<=n;i++)
{
if(vis[i]==0)
{
DFS(i);
break;
}
}
build(1,n,1);
scanf("%d",&m);
cout<<"Case #"<<k++<<":"<<endl;
for(int i=1;i<=m;i++)
{
char s[10];
cin>>s;
if(s[0]=='C')
{
scanf("%d",&x);
cout<<query(start[x],1,n,1)<<endl;
}
else if(s[0]=='T')
{
scanf("%d%d",&x,&y);
update(start[x],end_[x],y,1,n,1);
}
}
}
return 0;
}