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.
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)
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
代码如下:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 using namespace std; 9 const int MAXN = 50500; 10 int n,Q,t,topboss,cnt; 11 int head[MAXN],tot; 12 int start[MAXN],endd[MAXN]; 13 bool used[MAXN]; 14 struct Edge 15 { 16 int to,next; 17 }edge[MAXN]; 18 void init() 19 { 20 cnt=0; 21 tot=0; 22 memset(head,-1,sizeof head); 23 memset(used,false,sizeof used); 24 } 25 void addedge (int u,int v) 26 { 27 edge[tot].to=v; 28 edge[tot].next=head[u]; 29 head[u]=tot++; 30 } 31 void dfs(int u) 32 { 33 ++cnt; 34 start[u]=cnt; 35 for (int i=head[u];i!=-1;i=edge[i].next) 36 { 37 dfs(edge[i].to); 38 } 39 endd[u]=cnt; 40 } 41 struct Node 42 { 43 int l,r,val,lazy; 44 }segTree[MAXN<<2]; 45 void upDate_Same (int r,int v) 46 { 47 if (r) 48 { 49 segTree[r].val=v; 50 segTree[r].lazy=1; 51 } 52 } 53 void push_down (int r) 54 { 55 if (segTree[r].lazy) 56 { 57 upDate_Same(r<<1,segTree[r].val); 58 upDate_Same(r<<1|1,segTree[r].val); 59 segTree[r].lazy=0; 60 } 61 } 62 void buildTree (int i,int l,int r) 63 { 64 segTree[i].l=l; 65 segTree[i].r=r; 66 segTree[i].val=-1; 67 segTree[i].lazy=0; 68 if (l==r) 69 return ; 70 int mid =(l+r)>>1; 71 buildTree(i<<1,l,mid); 72 buildTree(i<<1|1,mid+1,r); 73 } 74 void update (int i,int l,int r,int v) 75 { 76 if (segTree[i].l==l&&segTree[i].r==r) 77 { 78 upDate_Same(i,v); 79 return ; 80 } 81 push_down(i); 82 int mid =(segTree[i].l+segTree[i].r)/2; 83 if (r<=mid) update(i<<1,l,r,v); 84 else if (l>mid) update(i<<1|1,l,r,v); 85 else 86 { 87 update(i<<1,l,mid,v); 88 update(i<<1|1,mid+1,r,v); 89 } 90 } 91 int query (int i,int u) 92 { 93 if (segTree[i].l==u&&segTree[i].r==u) 94 return segTree[i].val; 95 push_down(i); 96 int mid =(segTree[i].l+segTree[i].r)/2; 97 if (u<=mid) 98 return query(i<<1,u); 99 else 100 return query(i<<1|1,u); 101 } 102 int main() 103 { 104 //freopen("de.txt","r",stdin); 105 cin>>t; 106 int casee=0; 107 while (t--){ 108 printf("Case #%d: ",++casee); 109 int u,v; 110 init(); 111 scanf("%d",&n); 112 for (int i=0;i<n-1;++i){ 113 cin>>u>>v; 114 used[u]=true; 115 addedge(v,u); 116 } 117 for (int i=1;i<=n;++i){ 118 if (!used[i]){ 119 dfs(i); 120 break; 121 } 122 } 123 cin>>Q; 124 buildTree(1,1,cnt); 125 char op[10]; 126 while (Q--){ 127 scanf("%s",op); 128 if (op[0]=='C'){ 129 scanf("%d",&u); 130 printf("%d ",query(1,start[u])); 131 } 132 else{ 133 scanf("%d%d",&u,&v); 134 update(1,start[u],endd[u],v); 135 } 136 } 137 } 138 return 0; 139 }
700ms,有人200ms过了,正在研究。http://vjudge.net/contest/source/7108995 http://vjudge.net/contest/source/6308790