题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974
Assign the task
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8661 Accepted Submission(s): 3187
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<=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
题意:公司里有一堆人,然后对于每个人(除了公司老板)每个人都有上属和下属,他们恰好构成一棵树,每当把任务分给某个人时,若他有下属,则他的所有子树全部做这个任务,对于每次查询输出某个节点正在做什么任务。
题解:DFS序+线段树维护
好像跑一遍DFS然后直接当成线段树写就好了??
#include<bits/stdc++.h>
using namespace std;
const int maxn=5e4+6;
vector<int>ar[maxn];
bool vis[maxn];
int in[maxn],out[maxn];//进入某个点的时间和出去某个点的时间
int n,m,cnt;
struct node{
int l,r,sum;
int lazy;
}sz[maxn<<2];
inline void init()//初始化
{
cnt=0;
memset(in,0,sizeof in);
memset(out,0,sizeof out);
memset(sz,0,sizeof sz);
memset(vis,0,sizeof vis);
for(int i=0;i<=n;i++)
ar[i].clear();
}
void dfs(int u)//便利每个点,得到时间戳
{
vis[u]=1;
in[u]=++cnt;
for(int i=0;i<ar[u].size();i++)
{
dfs(ar[u][i]);
}
out[u]=cnt;
}
void build(int l,int r,int n)//普通建树
{
sz[n].l=l;
sz[n].r=r;
sz[n].lazy=0;
if(l==r)
{
return ;
}
int mid=l+r>>1;
build(l,mid,n<<1);
build(mid+1,r,n<<1|1);
}
void push_down(int n)//普通线段树lazy标记
{
if(sz[n].lazy!=0)
{
sz[n<<1].lazy=sz[n].lazy;
sz[n<<1|1].lazy=sz[n].lazy;
sz[n<<1].sum=sz[n].lazy;
sz[n<<1|1].sum=sz[n].lazy;
sz[n].lazy=0;
}
}
void updata(int l,int r,int n,int ll,int rr,int k)//普通更新
{
if(l>=ll&&r<=rr)
{
sz[n].sum=k;
sz[n].lazy=k;
return ;
}
push_down(n);
int mid=l+r>>1;
if(mid>=ll) updata(l,mid,n<<1,ll,rr,k);
if(mid<rr) updata(mid+1,r,n<<1|1,ll,rr,k);
}
int getSum(int l,int r,int n,int ll,int rr)//普通查询
{
if(l==r)
{
return sz[n].sum;
}
push_down(n);
int mid=l+r>>1;
if(mid>=ll) return getSum(l,mid,n<<1,ll,rr);
else return getSum(mid+1,r,n<<1|1,ll,rr);
}
int main()
{
int t,q=1;
scanf("%d",&t);
while(t--)
{
printf("Case #%d:\n",q++);
scanf("%d",&n);
init();
build(1,n,1);
for(int i=1;i<n;i++)
{
int x,y;
scanf("%d %d",&x,&y);
ar[y].push_back(x);//可抽象为y-x有一条路
vis[x]=1;
}
for(int i=1;i<=n;i++)
{
if(vis[i]==0)
{
dfs(i);
break;
}
}
scanf("%d",&m);
while(m--)
{
char s[2];
scanf("%s",s);
if(s[0]=='T')
{
int x,y;
scanf("%d %d",&x,&y);
updata(1,n,1,in[x],out[x],y);
}
else
{
int x;
scanf("%d",&x);
int ans=getSum(1,n,1,in[x],in[x]);
if(ans==0) puts("-1");
else printf("%d\n",ans);
}
}
}
}