题目链接:
题目
Assign the task
Time Limit: 15000/5000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
问题描述
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)
输出
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.
样例
input
1
5
4 3
3 2
1 3
5 2
5
C 3
T 2 1
C 3
T 3 2
C 3
output
Case #1:
-1
1
2
题意
给你一颗树,执行两种操作:
把以x为根的子树的任务全部赋值为y.
查询x的当前任务。
题解
查了很多题解说是并查集,明明赤裸裸的暴力,看不出哪里跟并查集有关。。
暴力为什么能过orz,想都没敢想暴力。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 5e4 + 10;
int fa[maxn], tim[maxn], tas[maxn];
int n,tot=0;
void init() {
tot = 0;
memset(tim, 0, sizeof(tim));
memset(fa, -1, sizeof(fa));
memset(tas, -1, sizeof(tas));
}
int main() {
int tc, kase = 0;
scanf("%d", &tc);
while (tc--) {
scanf("%d", &n);
init();
for (int i = 0; i < n - 1;i++){
int u, v;
scanf("%d%d", &u, &v);
fa[u] = v;
}
int q;
scanf("%d", &q);
printf("Case #%d:\n", ++kase);
while (q--) {
char cmd[11]; int x, y;
scanf("%s", cmd);
if (cmd[0] == 'C') {
scanf("%d", &x);
int t = -1, v = -1;
while (x != -1) {
if (t < tim[x]) {
t = tim[x];
v = tas[x];
}
x = fa[x];
}
printf("%d\n", v);
}else{
scanf("%d%d", &x, &y);
tim[x] = ++tot;
tas[x] = y;
}
}
}
return 0;
}
还是感觉来一发线段树比较靠谱。
求dfs序,按照这个序来建线段树。
时间跑的竟然还不如暴力orz。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#define lson (o<<1)
#define rson ((o<<1)|1)
#define M (l+(r-l)/2)
using namespace std;
const int maxn = 5e4 + 10;
int n;
int fa[maxn],pre[maxn], aft[maxn], dfs_clock;
vector<int> G[maxn];
void dfs(int u) {
pre[u] = ++dfs_clock;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (v == fa[u]) continue;
dfs(v);
}
aft[u] = ++dfs_clock;
}
int setv[maxn << 3], timv[maxn << 3],tot;
int _p, _ret,_tim;
void query(int o, int l, int r) {
if (l == r) {
if (_tim < timv[o]) {
_tim = timv[o], _ret = setv[o];
}
}
else {
if (_tim < timv[o]) {
_tim = timv[o], _ret = setv[o];
}
if (_p <= M) query(lson, l, M);
else query(rson, M + 1, r);
}
}
int ul, ur, uv,ut;
void update(int o, int l, int r) {
if (ul <= l&&r <= ur) {
setv[o] = uv, timv[o] = ut;
}
else {
if(ul<=M) update(lson, l, M);
if(ur>M) update(rson, M + 1, r);
}
}
void init() {
dfs_clock = tot = 0;
for (int i = 0; i <= n; i++) G[i].clear();
memset(setv, -1, sizeof(setv));
memset(timv, -1, sizeof(timv));
memset(fa, -1, sizeof(fa));
}
int main() {
int tc, kase = 0;
scanf("%d", &tc);
while (tc--) {
scanf("%d", &n);
init();
for (int i = 0; i < n - 1; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[v].push_back(u);
fa[u] = v;
}
int rt = -1;
for (int i = 1; i <= n; i++) if (fa[i] == -1) {
rt = i; break;
}
dfs(rt);
int q;
scanf("%d", &q);
printf("Case #%d:\n", ++kase);
while(q--){
char cmd[22]; int x, y;
scanf("%s", cmd);
if (cmd[0] == 'C') {
scanf("%d", &x);
_p = pre[x], _ret = -1, _tim = -1;
query(1, 1, dfs_clock);
printf("%d\n", _ret);
}
else {
scanf("%d%d", &x, &y);
ul = pre[x], ur = aft[x],uv=y,ut=++tot;
update(1, 1, dfs_clock);
}
}
}
return 0;
}