kuangbin专题七 HDU3974 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.

InputThe 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)OutputFor 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建树。有一个结论,如果v是u的祖先,那么dfs序st[v]<st[u]&&ed[v]>ed[u]

于是就可以建树了。然后就是打标记查标记



  1 #include <iostream>
  2 #include <stdio.h>
  3 #include <math.h>
  4 #include <string.h>
  5 #include <stdlib.h>
  6 #include <string>
  7 #include <vector>
  8 #include <set>
  9 #include <map>
 10 #include <queue>
 11 #include <algorithm>
 12 #include <sstream>
 13 #include <stack>
 14 using namespace std;
 15 #define FO freopen("in.txt","r",stdin);
 16 #define rep(i,a,n) for (int i=a;i<n;i++)
 17 #define per(i,a,n) for (int i=n-1;i>=a;i--)
 18 #define pb push_back
 19 #define mp make_pair
 20 #define all(x) (x).begin(),(x).end()
 21 #define fi first
 22 #define se second
 23 #define SZ(x) ((int)(x).size())
 24 #define debug(x) cout << "&&" << x << "&&" << endl;
 25 #define lowbit(x) (x&-x)
 26 #define mem(a,b) memset(a, b, sizeof(a));
 27 typedef vector<int> VI;
 28 typedef long long ll;
 29 typedef pair<int,int> PII;
 30 const ll mod=1000000007;
 31 const int inf = 0x3f3f3f3f;
 32 ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
 33 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
 34 //head
 35 
 36 const int maxn=50010;
 37 int _,lazy[maxn<<3],st[maxn],ed[maxn],cur,m,vis[maxn],n;
 38 vector<int> boss[maxn];
 39 
 40 void dfs(int rt) {//建树
 41     st[rt]=++cur;
 42     for(int i=0;i<boss[rt].size();i++) {
 43         dfs(boss[rt][i]);
 44     }
 45     ed[rt]=cur;
 46 }
 47 
 48 void pushdown(int rt) {
 49     if(lazy[rt]!=-1) {
 50         lazy[rt<<1]=lazy[rt];
 51         lazy[rt<<1|1]=lazy[rt];
 52         lazy[rt]=-1;
 53     }
 54 }
 55 
 56 void build(int rt,int L,int R) {
 57     lazy[rt]=-1;
 58     if(L==R) return;
 59     int mid=(L+R)>>1;
 60     build(rt<<1,L,mid);
 61     build(rt<<1|1,mid+1,R);
 62 }
 63 
 64 void updata(int rt,int L,int R,int l,int r,int zhi) {
 65     if(L>=l&&R<=r) {
 66         lazy[rt]=zhi;
 67         return;
 68     }
 69     pushdown(rt);
 70     int mid=(L+R)>>1;
 71     if(l<=mid) updata(rt<<1,L,mid,l,r,zhi);
 72     if(r>mid) updata(rt<<1|1,mid+1,R,l,r,zhi);
 73 }
 74 
 75 int query(int rt,int L,int R,int pos) {
 76     if(L==R) return lazy[rt];//单点查
 77     pushdown(rt);
 78     int mid=(L+R)>>1;
 79     if(pos<=mid) query(rt<<1,L,mid,pos);
 80     else query(rt<<1|1,mid+1,R,pos);
 81 }
 82 
 83 int curr=1;
 84 int main() {
 85     for(scanf("%d",&_);_;_--) {
 86         printf("Case #%d:
",curr++);
 87         cur=0;
 88         mem(boss,0);
 89         mem(vis,0);
 90         scanf("%d",&n);
 91         int u,v;
 92         rep(i,1,n) {//存关系
 93             scanf("%d%d",&u,&v);
 94             boss[v].push_back(u);
 95             vis[u]=1;
 96         }
 97         rep(i,1,n+1) {//找到根
 98             if(!vis[i]) {
 99                 dfs(i);
100                 break;
101             }
102         }
103         build(1,1,cur);//建树
104         scanf("%d",&m);
105         char s[2];
106         int pos,zhi;
107         while(m--) {
108             scanf("%s",s);
109             if(s[0]=='T') {
110                 scanf("%d%d",&pos,&zhi);
111                 updata(1,1,cur,st[pos],ed[pos],zhi);//区间st[pos]-ed[pos]是pos的员工
112             } else {
113                 scanf("%d",&pos);
114                 printf("%d
",query(1,1,cur,st[pos]));//查pos的任务(ed[pos]就不是了)
115             }
116         }
117     }
118 }
原文地址:https://www.cnblogs.com/ACMerszl/p/9895421.html