HDU-3974 Assign the task (DFS timestamp build multi-tree tree line)

http://acm.hdu.edu.cn/showproblem.php?pid=3974

 

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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

 

Meaning of the questions:

The company has N staff members, only one boss, one for each of the remaining staff supervisor, whichN staffs just make up a tree. Then there are M operation.

When a staff member to x assign tasks y (i.e.  T Y),x its subsidiaries and its affiliated subsidiaries will put down the work to complete the task at handy. For each inquiry (ie,x), the output of the program staff x what is going on missions.

Briefly: to give a multi-tree, all nodes initially to -1, there are two operations, one for the (T, x, y), x represents all the nodes and their subtree of nodes. becomes y, the other is (C, x), x is the evaluation node.

answer:

The difficulty of solving the problem is how to convert a correspondence relationship as a model segment in question of the tree, it is how to handle the mapping between the segment tree and tree lot.

The nature of a segment tree is the whole interval of continuous operation. So the key to solving the problem lies in the conversion operation will be clear how the multi-tree operated in pairs continuum of an entire paragraph.

Clearly the relationship between all of the company's employees can use more than one tree to represent. Then from the boss (that is, the degree of point zero), with dfs to the tree marked with a timestamp, to determine the scope of each point and recorded.

The id number assigned to the new node is mapped to the corresponding segment tree top. This is equivalent to assign the task to update a contiguous node, query task is equivalent to a single point of inquiry.

 

code show as below:

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <iostream>
  4 #include <string>
  5 #include <math.h>
  6 #include <algorithm>
  7 #include <vector>
  8 #include <queue>
  9 #include <set>
 10 #include <stack>
 11 #include <map>
 12 #include <math.h>
 13 const int INF=0x3f3f3f3f;
 14 typedef long long LL;
 15 const int mod=1e9+7;
 16 //const double PI=acos(-1);
 17 const int maxn=1e5+10;
 18 using namespace std;
 19 //ios::sync_with_stdio(false);
 20 //    cin.tie(NULL);
 21 
 22 struct Edge_node
 23 {
 24     int v;
 25     int next;
 26 }Edge[50005*2];
 27 
 28 structSegTree_node
 29  {
 30      int L;
 31 is      int R & lt;
 32      int NUM;
 33 is } SegTree [ 50005 << 2 ];
 34 is  int n-, m;
 35  int CNT; // edge of the counter, from 0 
36  int NUM; // tree line number counter, starting. 1 
37 [  int head [ 50005 ];
 38 is  int VIS [ 50005 ]; // is there a boss, i.e. the degree of 0 
39  int ST [ 50005 ]; //When the traversing number, while a segment tree number 
40  int ED [ 50005 ]; // traverse the number after the end, represented in the tree can affect the point of the line segment last sequence number (i.e., boss) 
41 is  
42 is  void the init () / / initialization 
43 is  {
 44 is      CNT = 0 ;
 45      NUM = 0 ; 
 46 is      Memset (head, - . 1 , the sizeof (head));
 47      Memset (ST, 0 , the sizeof (ST));
 48      Memset (ED, 0 , the sizeof ( ED));
 49      Memset (Edge, 0 ,sizeof(Edge));
 50     memset(vis,0,sizeof(vis));
 51 }
 52 
 53 void add_edge(int u,int v)
 54 {
 55     Edge[cnt].v=v;
 56     Edge[cnt].next=head[u];
 57     head[u]=cnt++; 
 58 }
 59 
 60 void DFS(int u)
 61 {
 62     num++;
 63     st[u]=num;
 64     for(int i=head[u];i!=-1;i=Edge[i].next)
 65     {
 66         DFS(Edge[i].v);
 67     }
 68     ed[u]=num;
 69 }
 70 
 71 void PushDown(int rt)
 72 {
 73     if(SegTree[rt].num!=-1)
 74     {
 75         SegTree[rt<<1].num=SegTree[rt<<1|1].num=SegTree[rt].num;
 76         SegTree[rt].num=-1;
 77     }
 78 }
 79 
 80 void Build(int l,int r,int rt)
 81 {
 82     SegTree[rt].l=l;
 83     SegTree[rt].r=r;
 84     SegTree[rt].num=-1;
 85     if(l==r)
 86         return ;
 87     int mid=(l+r)>>1;
 88     Build(l,mid,rt<<1);
 89     Build(mid+1,r,rt<<1|1);
 90 }
 91 
 92 void Update(int L,int R,int C,int rt)
 93 {
 94     int l=SegTree[rt].l;
 95     int r=SegTree[rt].r; 
 96     if(L<=l&&R>=r)
 97     {
 98         SegTree[rt].num=C;
 99         return ;
100     }
101     PushDown(rt);
102     int mid=(l+r)>>1;
103     if(L<=mid)
104         Update(L,R,C,rt<<1);
105     if(R>mid)
106         Update(L,R,C,rt<<1|1);
107 }
108 
109 int Query(int L,int rt)
110 {
111     int l=SegTree[rt].l;
112     int r=SegTree[rt].r;
113     if(l==r)
114         return SegTree[rt].num;
115     PushDown(rt);
116     int mid=(l+r)>>1;
117     if(L<=mid)
118         Query(L,rt<<1);
119     else if(L>mid)
120         Query(L,rt<<1|1);
121 }
122 
123 int main()
124 {
125     int T;
126     scanf("%d",&T);
127     for(int k=1;k<=T;k++)
128     {
129         printf("Case #%d:\n",k);
130         init();
131         scanf("%d",&n);
132         for(int i=1;i<n;i++)
133         {
134             int u,v;
135             scanf("%d %d",&u,&v);
136             vis[u]=1;
137             add_edge(v,u);
138         }
139         for(int i=1;i<=n;i++)
140         {
141             if(!vis[i])
142             {
143                 DFS(i);
144                 break;
145             }
146         }
147         Build(1,num,1);
148         scanf("%d",&m);
149         for(int i=1;i<=m;i++)
150         {
151             char c[5];
152             int x;
153             scanf("%s %d",c,&x);
154             if(c[0]=='C')
155             {
156                 printf("%d\n",Query(st[x],1));
157             } 
158             else if(c[0]=='T')
159             {
160                 int t;
161                  Scanf ( " % D " , & T);
 162                  // ST [x] -> interval range ED [x] x represents and x subordinate line segment tree 
163                  the Update (ST [x], ED [x], T , . 1 );
 164 is              }
 165          }
 166      }
 167 is      return  0 ;
 168 }

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/jiamian/p/11415980.html