• bzoj3631 [JLOI2014]松鼠的新家


    [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<bits/stdc++.h>
    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<int> 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
    ", ans[i]);
    }
    
    int main()
    {
    	putit();
    	workk();
    	print();
    	return 0;
    }
    
    
    
    心如花木,向阳而生。
  • 相关阅读:
    JavaScript相关,this 的指向
    酷炫的渲染,CSS3动画!
    正襟危坐,聊一聊Web语义化
    本地存储的技术,localStorage、sessionStorage、cookie、session
    Express 系列(终章):MongoDB使数据持久化
    股票投资相关,成交量怎么看
    【日本語新聞選読】第5回:3月31日
    【1901日語听解2】第5回:3月30日
    【日語視聴説2】第5回:3月30日
    【日本語新聞編集】第4回:3月27日
  • 原文地址:https://www.cnblogs.com/LLppdd/p/9026952.html
Copyright © 2020-2023  润新知