(dfs)Company
链接:https://ac.nowcoder.com/acm/contest/322/C
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld
题目描述
在一个偏僻的大山里, 一共有n个村庄, 编号1~n,每个村庄都有一定数量的村民, 其中只有1号村庄有水井,为了方便村民们日常用水,村民们一共修建了n-1根水管, 保证每一村庄都能有水喝。因为水是从高流向低, 所以我们知道1号村庄海拔最高, 与1号村庄直接相连的村庄高度次之, 以此类推。
对于每一个村庄, 如果这个村庄的村民人数<=k, 我们称之为"劳动力不足的村庄"。
现在我们想知道, 如果水井不修在1号村庄, 而是修在任意一个村庄, 那么有水流过的村庄中有多少个村庄是"劳动力不足的村庄"(包括有水井的村庄本身)。
输入描述:
第一行输入两个数n(1 <= n <= 200000)村庄的数量, 以及劳动力的评估标准k(1 <= k <= 1e9) 第二行有n个数, 表示这n个村庄的村民数(1 <= a <= 1e9) 之后有n-1行, 每行两个数字u, v(1 <= u, v <= n) u, v两个村庄之间有一条水管。
输出描述:
输出n个数, 输出假设水井修在第i(1 <= i <= n)个村庄,那么有水流过的村庄中有多少个村庄是"劳动力不足的村庄"。 两个数之间用空格隔开
示例1
输入
8 10 12 15 11 4 5 19 14 20 3 7 1 5 2 1 6 5 2 3 6 4 8 3
输出
2 0 0 1 2 1 0 0
说明
蓝色数字表示村民数量, 红色数字为所求答案
示例2
输入
5 10 13 15 19 5 3 2 3 4 1 1 2 3 5
输出
2 1 1 1 1
说明
蓝色数字表示村民数量, 红色数字为所求答案
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<bits/stdc++.h>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=max(tree[rt<<1],tree[rt<<1|1])
#define nth(k,n) nth_element(a,a+k,a+n); // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
using namespace std;
inline int read(){
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
typedef long long ll;
const double pi = atan(1.)*4.;
const int M=1e9;
const int N=1e5+5;
int n,k;
vector<int>mapp[200005],ans[200005];
int a[200005],v[200005],cut=0;
void dfs(int x){ // 从双向图变成单向图
v[x]=1;
for(int i=0;i<mapp[x].size();i++){
if(v[mapp[x][i]]) continue;
ans[x].push_back(mapp[x][i]);
dfs(mapp[x][i]);
}
return ;
}
int dfs1(int x){ // 对 <=k 的情况进行累和
for(int i=0;i<ans[x].size();i++)
v[x]+=dfs1(ans[x][i]);
if(a[x]<=k)
v[x]++;
return v[x];
}
int main(){
scanf("%d %d",&n,&k);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int m=n-1;
while(m--){
int x,y;
scanf("%d %d",&x,&y);
mapp[x].push_back(y);
mapp[y].push_back(x);
}
dfs(1);
memset(v,0,sizeof(v));
dfs1(1);
for(int i=1;i<=n;i++){
printf("%d ",v[i]);
}
return 0;
}