2015
04-14

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)

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)

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
1
2

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

ps：此题的数据太弱了实际上乱暴力都可以过。。。.。。。。

#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <iostream>
using namespace std;

const int N=100010;
struct node
{
int l, r, mid;
int value, lazy, parent;
} tr[N*4];
int n, m, up, l[N], r[N], first[N], edge[N], next[N], f[N];
int value;

{
f[x] = y;
edge[up] = x;
next[up] = first[y];
first[y] = up++;
}
int findset(int i)
{
while(f[i]!=i)
i = f[i];
return i;
}
void dfs(int p)
{
int i;
i = first[p];
l[p] = up;
while(i!=-1)
{
dfs(edge[i]);
i = next[i];
}
r[p] = up++;
}
void build(int l, int r, int p)
{
tr[p].l = l;
tr[p].r = r;
tr[p].value = -1;
tr[p].lazy = -1;
tr[p].parent = -1;
tr[p].mid = (l+r)>>1;
if(l==r)
return;
build(l, tr[p].mid, p*2);
build(tr[p].mid+1, r, p*2+1);
}
void down(int p)
{
if(tr[p].lazy!=-1)
{
tr[p*2].value = tr[p].lazy;
tr[p*2].lazy = tr[p].lazy;
tr[p*2].parent = tr[p].lazy;
tr[p*2+1].value = tr[p].lazy;
tr[p*2+1].lazy = tr[p].lazy;
tr[p*2+1].parent = tr[p].lazy;
tr[p].lazy = -1;
}
}
void update(int l, int r, int p)
{
if(l==tr[p].l && r==tr[p].r)
{
tr[p].value = value;
tr[p].lazy = value;
return ;
}
down(p);
if(r<=tr[p].mid)
update(l, r, p*2);
else if(l>tr[p].mid)
update(l, r, p*2+1);
else
{
update(l, tr[p].mid, p*2);
update(tr[p].mid+1, r, p*2+1);
}
}
int query(int l, int r, int p)
{
if(l==tr[p].l && r==tr[p].r)
return tr[p].value;
down(p);
if(r<=tr[p].mid)
return query(l, r, p*2);
else if(l>tr[p].mid)
return query(l, r, p*2+1);
else
{
return tr[p*2].parent;
}
}
int main()
{
char s[20];
int i, j, x, y, cas, cas1, root;

scanf("%d", &cas);
cas1 = 1;
while(cas--)
{
scanf("%d", &n);
for(i=1; i<=n; i++)
{
first[i] = -1;
f[i] = i;
}

up = 1;
for(i=1; i<n; i++)
{
scanf("%d %d", &x, &y);
}

root = findset(1);
up = 1;
dfs(root);
up--;
//for(i=1; i<=5; i++)
//	printf("i=%d...%d %d...\n", i, l[i],  r[i]);

build(1, up, 1);
printf("Case #%d:\n", cas1++);
scanf("%d", &m);
while(m--)
{
scanf("%s", s);
if(s[0]=='C')
{
scanf("%d", &x);
printf("%d\n", query(r[x], r[x], 1));
}
else
{
scanf("%d %d", &x, &value);
update(l[x], r[x], 1);
}
}
}
return 0;
}

1. 自由本来就是个自我感觉嘛，自己觉得开心就好。。。就像50年代乡村音乐流行，叛逆的追求“自由”的青年们纷纷去追求摇滚乐。结果到了这些年，原本非主流文化的摇滚乐成为了主流文化，叛逆的追求“自由”的青年们纷纷去追求民谣、乡村音乐了。。。

2. Thanks for using the time to examine this, I truly feel strongly about it and enjoy finding out far more on this subject matter. If achievable, as you achieve knowledge

3. 第2题，TCP不支持多播，多播和广播仅应用于UDP。所以B选项是不对的。第2题，TCP不支持多播，多播和广播仅应用于UDP。所以B选项是不对的。

4. 可以根据二叉排序树的定义进行严格的排序树创建和后序遍历操作。如果形成的排序树相同，其树的前、中、后序遍历是相同的，但在此处不能使用中序遍历，因为，中序遍历的结果就是排序的结果。经在九度测试，运行时间90ms，比楼主的要快。