ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

"蔚来杯"2022牛客暑期多校训练营6 B题 Eezie and Pie 树上差分 链序

2022-08-06 19:22:02  阅读:143  来源: 互联网

标签:node 10 le int 蔚来 pie 多校 Eezie iii


 链接:https://ac.nowcoder.com/acm/contest/33191/B
来源:牛客网

题目描述

Eezie, a pie maniac, would like to have some pies with her friends on a hot summer day. However, the weather is so hot that she can't go outdoors and has to call for the delivery service.

The city Eezie lives in can be represented by NNN nodes connected by N−1N - 1N−1 edges, and the city center is node 111. In other words, the city is a rooted tree, root of which is node 111. There are NNN pie houses in the city, the iii-th on node iii. For some reason, a pie house on node iii can only deliver its pie to nodes on the simple path from node iii to node 111.

Eezie is a bit worried that a pie might lose its flavor during the deliver. After some careful calculation, she decided that a pie from the iii-th pie house can maintain its flavor if the distance it is delivered does not exceed its flavor-loss-distance did_idi​. The distance between two nodes on the tree is the number of edges on the simple path between them.

Now, Eezie wants to order some pies for all her friends who live on different nodes of the tree. Therefore, she wants you to calculate for each node how many pie houses can deliver their pie to the node without flavor loss.

输入描述:

The first line contains an integer N(1≤N≤2×106)N(1\le N \le 2\times 10^6)N(1≤N≤2×106), representing the number of nodes of the city Eezie lives in.

Each of the next N−1N - 1N−1 lines contains two integers u,v(1≤u,v≤N)u, v(1\le u,v \le N)u,v(1≤u,v≤N), representing an edge. It is guaranteed that the edges form a tree.
The last line contains NNN integers d1,d2,⋯ ,dN(0≤di≤N)d_1, d_2, \cdots, d_N(0\le d_i \le N)d1​,d2​,⋯,dN​(0≤di​≤N), representing the maximum travel distances for pies from pie houses.

输出描述:

Output NNN integers in a line, the iii-th integer representing the answer for node iii.
示例1

输入

复制
10
1 2
2 3
2 4
3 5
4 6
4 7
1 8
8 9
8 10
0 0 1 2 2 5 3 1 0 2

输出

复制
6 6 2 3 1 1 1 2 1 1

分析

用栈存dfs序,记录入栈,出栈的时候删除,保证遍历到当前,栈中存的是该节点的所有祖先。链序

树上差分,遍历到当前节点,可以配送到当前节点 以及 当前节点链上范围d[x] 内的可以配送得到的糕点数增加

最后将求和。太妙了。

//-------------------------代码----------------------------

//#define int ll
const int N = 2e6+10,M = N * 2;
int n,m,d[N],st[N],top,c[N];
V<int> ve[N];

void dfs(int x,int fa) {
    st[++top] = x;
    c[x] ++ ,c[st[top - min(top,d[x] + 1)]]--;
    for(int v:ve[x]) if(v != fa) dfs(v,x);
    top -- ;//
}

void dfss(int x,int fa) {
    for(int v:ve[x]) if(v != fa) {
        dfss(v,x);
        c[x] += c[v];
    }
}


void solve()
{
    cin>>n;assert(1<=n && n <= 2000000);
    fo(i,1,n-1) {
        int x,y;cin>>x>>y;assert(1<=x && x <= n);assert(1<=y && y<= n);
        ve[x].pb(y),ve[y].pb(x);
    }
    fo(i,1,n) {cin>>d[i];assert(0<=d[i] && d[i]<= n);}
    dfs(1,0);
    dfss(1,0);
    fo(i,1,n) cout<<c[i]<<' ';
}

signed main(){
    AC();
    clapping();TLE;
//  while(cin>>n,n)
//  while(cin>>n>>m,n,m)
//    int t;cin>>t;while(t -- )
    solve();
//    {solve(); }
    return 0;
}

/*样例区


*/

//------------------------------------------------------------

 

标签:node,10,le,int,蔚来,pie,多校,Eezie,iii
来源: https://www.cnblogs.com/er007/p/16557710.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有