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

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

作者:互联网

 链接: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