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.
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)
1 5 4 3 3 2 1 3 5 2 5 C 3 T 2 1 C 3 T 3 2 C 3
Case #1: -1 12
思路1: 由于人员关系本身就像一颗并查集类似的树,因此可以给任务分配时间,对于每次查询只要从查询点往上找找到所有父亲里面最新的任务就行了。
#include <cstdio> #include <queue> #include <map> #include <cmath> #include <string> #include <cstring> #include <algorithm> using namespace std; int k; int pre[50004]; int job[50004]; int tme[50004]; int Find(int x) { int cur = tme[x]; int ans = job[x]; while(pre[x]!= x) { if(cur == k) return ans; x = pre[x]; if(tme[x] > cur) { cur = tme[x]; ans = job[x]; } } if(tme[x] > cur) { cur = tme[x]; ans = job[x]; } return ans; } int main() { int t,n,m; int index = 1; scanf("%d",&t); while(t--) { scanf("%d",&n); k = 0; for(int i = 1; i <= n; i++) { pre[i] = i; job[i] = -1; tme[i] = -1; } for(int i = 1; i < n; i++) { int x,y; scanf("%d%d",&x,&y); pre[x] = y; } scanf("%d",&m); printf("Case #%d:\n",index++); for(int i = 0; i < m; i++) { char s[10]; int a,b; scanf("%s",s); if(s[0] == 'C') { scanf("%d",&a); printf("%d\n",Find(a)); } else { scanf("%d%d",&a,&b); job[a] = b; tme[a] = ++k; } } } return 0; }
思路 2 : 因为上级包含下级,所以更新a的工作相当于更新a所有下属的工作,相当于区间更新点查询,就想到线段树了,难点在与如何重新编号,这里采用一遍深搜编号,同时记录每个点子节点个数。
#include <cstdio> #include <queue> #include <map> #include <vector> #include <cmath> #include <climits> #include <string> #include <cstring> #include <algorithm> using namespace std; int pre[50004]; int num[50004]; map <int,int> mp; vector <int> vc[50004]; int k; void setNum(int cur,int last) { for(int j = 0; j < vc[cur].size(); j++) { int tmp = vc[cur][j]; mp[tmp] = k++; setNum(tmp,cur); } if(cur!=last) num[last] += num[cur] + 1; } struct node{ int l,r,task,lazy; }; node tree[200005]; void build(int i,int l,int r) { tree[i].l = l; tree[i].r = r; tree[i].task = -1; tree[i].lazy = 0; if(l == r) return; int mid = (l+r) / 2; build(i<<1,l,mid); build(i<<1|1,mid+1,r); } void update(int i,int l,int r,int task) { if(tree[i].l == l && tree[i].r == r) { tree[i].task = task; tree[i].lazy = 1; return; } if(task == tree[i].task) return; if(tree[i].lazy) { tree[i].lazy = 0; tree[i<<1].lazy = 1; tree[i<<1].task = tree[i].task; tree[i<<1|1].lazy = 1; tree[i<<1|1].task = tree[i].task; } tree[i].task = -2; // 表示区间内人任务不同 int mid = (tree[i].l + tree[i].r) / 2; if(r <= mid) update(i<<1,l,r,task); else if(l >mid) update(i<<1|1,l,r,task); else { update(i<<1,l,mid,task); update(i<<1|1,mid+1,r,task); } if(tree[i<<1].task == tree[i<<1|1].task && tree[i<<1].task!= -2) tree[i].task = tree[i<<1].task; } int query(int i,int t) { if(tree[i].task != -2) return tree[i].task; if(tree[i].lazy) { tree[i].lazy = 0; tree[i<<1].lazy = 1; tree[i<<1].task = tree[i].task; tree[i<<1|1].lazy = 1; tree[i<<1|1].task = tree[i].task; } int mid = (tree[i].l+tree[i].r) / 2; if(t <= mid) query(i<<1,t); else query(i<<1|1,t); } int main() { int t,n,m; int index = 1; scanf("%d",&t); while(t--) { scanf("%d",&n); memset(pre,-1,sizeof(pre)); memset(num,0,sizeof(num)); mp.clear(); k = 1; for(int i = 1; i <= n; i++) vc[i].clear(); for(int i = 1; i < n; i++) { int x,y; scanf("%d%d",&x,&y); pre[x] = y; vc[y].push_back(x); } int root = 0; for(int i = 1; i <= n; i++) { if(pre[i] == -1) root = i; } mp[root] = k++; build(1,1,n); setNum(root,root); scanf("%d",&m); printf("Case #%d:\n",index++); for(int i = 0; i < m; i++) { char s[10]; int a,b; scanf("%s",s); if(s[0] == 'C') { scanf("%d",&a); printf("%d\n",query(1,mp[a])); } else { scanf("%d%d",&a,&b); update(1,mp[a],mp[a]+num[a],b); } } } return 0; }