博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
bzoj3631 [JLOI2014]松鼠的新家
阅读量:4334 次
发布时间:2019-06-07

本文共 2656 字,大约阅读时间需要 8 分钟。

[JLOI2014]松鼠的新家

Time Limit: 10 Sec Memory Limit: 128 MB

Description

松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的。天哪,他居然真的住在“树”上。松鼠想邀请小熊维尼前来参观,并且还指定一份参观指南,他希望维尼能够按照他的指南顺序,先去a1,再去a2,……,最后到an,去参观新家。

可是这样会导致维尼重复走很多房间,懒惰的维尼不听地推辞。可是松鼠告诉他,每走到一个房间,他就可以从房间拿一块糖果吃。维尼是个馋家伙,立马就答应了。
现在松鼠希望知道为了保证维尼有糖果吃,他需要在每一个房间各放至少多少个糖果。因为松鼠参观指南上的最后一个房间an是餐厅,餐厅里他准备了丰盛的大餐,所以当维尼在参观的最后到达餐厅时就不需要再拿糖果吃了。

Input

第一行一个整数n,表示房间个数

第二行n个整数,依次描述a1-an
接下来n-1行,每行两个整数x,y,表示标号x和y的两个房间之间有树枝相连。

Output

一共n行,第i行输出标号为i的房间至少需要放多少个糖果,才能让维尼有糖果吃。

Sample Input

5

1 4 5 3 2
1 2
2 4
2 3
4 5

Sample Output

1

2
1
2
1

HINT

2<= n <=300000

拒绝数据结构。。。。。树上差分。。。

ans是子树的和。。。
每次操作(s,t)
lca--,lca的爸爸--;
s++,t++;

#include
using namespace std;const int maxn = 3e5 + 5;struct lpl{ int top, deep, fa, size, son, tag;}node[maxn];int n, root, ini[maxn], ans[maxn];vector
point[maxn];inline void putit(){ int x, y; scanf("%d", &n); for(int i = 1; i <= n; ++i) scanf("%d", &ini[i]); for(int i = 1; i < n; ++i){ scanf("%d%d", &x, &y); point[x].push_back(y); point[y].push_back(x); }}void dfs_1(int t){ node[t].size = 1; int lin = 0; for(int i = point[t].size() - 1; i >= 0; --i){ if(point[t][i] == node[t].fa) continue; node[point[t][i]].fa = t; node[point[t][i]].deep = node[t].deep + 1; dfs_1(point[t][i]); node[t].size += node[point[t][i]].size; if(lin < node[point[t][i]].size){ lin = node[point[t][i]].size; node[t].son = point[t][i]; } }}inline int LCA(int a, int b){ while(node[a].top != node[b].top){ if(node[node[a].top].deep < node[node[b].top].deep) swap(a, b); a = node[a].top; a = node[a].fa; } return (node[a].deep < node[b].deep) ? a : b;}inline void dfs_2(int t){ if(node[t].top == 0) node[t].top = t; node[node[t].son].top = node[t].top; for(int i = point[t].size() - 1; i >= 0; --i){ if(point[t][i] != node[t].fa) dfs_2(point[t][i]); }}inline void workk(){ root = 1; dfs_1(root); dfs_2(root); for(int i = 2; i <= n; ++i){ int lca = LCA(ini[i], ini[i - 1]); node[lca].tag--; node[node[lca].fa].tag--; node[ini[i]].tag++; node[ini[i - 1]].tag++; }}void dfs_3(int t){ ans[t] += node[t].tag; for(int i = point[t].size() - 1; i >= 0; --i){ if(point[t][i] == node[t].fa) continue; dfs_3(point[t][i]); ans[t] += ans[point[t][i]]; }}inline void print(){ dfs_3(root); for(int i = 2; i <= n; ++i) ans[ini[i]]--; for(int i = 1; i <= n; ++i) printf("%d\n", ans[i]);}int main(){ putit(); workk(); print(); return 0;}

转载于:https://www.cnblogs.com/LLppdd/p/9026952.html

你可能感兴趣的文章
个人工作总结05(第二阶段)
查看>>
Java clone() 浅拷贝 深拷贝
查看>>
深入理解Java虚拟机&运行时数据区
查看>>
02-环境搭建
查看>>
spring第二冲刺阶段第七天
查看>>
搜索框键盘抬起事件2
查看>>
阿里百川SDK初始化失败 错误码是203
查看>>
透析Java本质-谁创建了对象,this是什么
查看>>
BFS和DFS的java实现
查看>>
关于jquery中prev()和next()的用法
查看>>
一、 kettle开发、上线常见问题以及防错规范步骤
查看>>
eclipse没有server选项
查看>>
CRC码计算及校验原理的最通俗诠释
查看>>
QTcpSocket的连续发送数据和连续接收数据
查看>>
使用Gitbook来编写你的Api文档
查看>>
jquery扩展 $.fn
查看>>
Markdown指南
查看>>
influxDB的安装和简单使用
查看>>
JPA框架学习
查看>>
JPA、JTA、XA相关索引
查看>>