普通的研究生。

[题目|线段树]kuangbin-7-J (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. 

输入格式

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)

输出格式

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.

样例输入#1

1
5
4 3
3 2
1 3
5 2
5
C 3
T 2 1
C 3
T 3 2
C 3

样例输出#1

Case #1:
-1
1
2

思想

1.依靠本题学习了树形dfs序的做法,建边直接使用vector进行即可(也可用链式前向星),重点是找树根。我们知道,只有根节点无入度,则可利用此性质设置vis数组,每次加边时将末节点的vis值置为true,最后遍历vis寻找false的点即为根节点。

2.本题也学习了如何建立一层的起始点至最终点相邻的线段树,即维护两个数组begin和end(小心保留字),然后按照所示代码中建立即可。

代码

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<iostream>
#include<map>
#define M 50005
using namespace std;
const int inf=0x7f7f7f7f;
vector<int>rel[M];
bool vis[M];
int beg[M],ed[M];
int st[M<<2];
int lazy[M<<2];
int cnt;
void build(int o,int l,int r)
{
lazy[o]=-1;
if(l==r) return ;
int m=(l+r)>>1;
build(o<<1,l,m);
build((o<<1)|1,m+1,r);
}
void pushdown(int o,int l,int r)
{
if(lazy[o]!=-1)
 {
  int m=(l+r)>>1;
  lazy[o<<1]=lazy[o];
  lazy[(o<<1)|1]=lazy[o];
  lazy[o]=-1;
 }
}
void update(int o,int l,int r,int ql,int qr,int c)
{
if(ql<=l&&qr>=r)
 {
  lazy[o]=c;
  return ;
 }
int m=(l+r)>>1;
pushdown(o,l,r);
if(ql<=m) update(o<<1,l,m,ql,qr,c);
if(qr>=m+1) update((o<<1)|1,m+1,r,ql,qr,c); 
}
int query(int o,int l,int r,int ind)
{
if(l==r)
 {
  return lazy[o]; 
 }
int m=(l+r)>>1;
int ans;
pushdown(o,l,r);
if(ind<=m) ans=query(o<<1,l,m,ind);
else  ans=query((o<<1)|1,m+1,r,ind);
return ans;

void dfs(int pt)
{
beg[pt]=++cnt;
for(int i=0;i<rel[pt].size();i++)
 {
  dfs(rel[pt][i]);
 }
ed[pt]=cnt;
}
int main() //本题线段树里的初始值没用 
{
ios::sync_with_stdio(false);
char oper[2];
int x,y;
int num=1;
int t,n,m;
int u,v;
cin>>t;
while(t--)
 {
  cin>>n;
  cnt=0;
  memset(vis,false,sizeof(vis));
  memset(lazy,-1,sizeof(lazy));
  for(int i=0;i<M;i++)
  {
   rel[i].clear();
  }
  //1.找树根     2.dfs序遍历产生每层的子节点线段树范围 
  for(int i=0;i<n-1;i++)
  {
   cin>>u>>v; //v -boss-> u
   rel[v].push_back(u);
   vis[u]=true;
  }
  for(int i=1;i<=n;i++)
  {
   if(!vis[i])
   {
    dfs(i);
   }
  }
  build(1,1,n);
  cin>>m;
  cout<<"Case #"<<num++<<":"<<endl;
  for(int i=0;i<m;i++)
  {
   cin>>oper;
   if(oper[0]=='C')
   {
    cin>>x;
    cout<<query(1,1,n,beg[x])<<endl;
   }
   else if(oper[0]=='T')
   {
    cin>>x>>y;
    update(1,1,n,beg[x],ed[x],y);
   }
  }
 }
return 0;
}

评论

© 方鸢子 | Powered by LOFTER