当前位置:网站首页>2022牛客多校六 B-Eezie and Pie (dfs)
2022牛客多校六 B-Eezie and Pie (dfs)
2022-08-08 23:06:00 【AC__dream】
题目:
样例输入:
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
题意:给定一棵有根树,根为1,每个节点可以为它的 0 ~ 𝑑[𝑖] 级祖先贡献 1 的价值。求最终每个点的价值。
分析:对于每个子节点可以先找到他的第一个不能贡献的祖先节点v,并将loss[v]减1,那么在v之上的祖先节点也无法得到该节点的贡献,所以也就是说loss是可以上传的,那么loss[v]的含义就是说以v为根的子树中不能给v带来贡献的子节点数目。所以我们就可以得到,每个节点的最终价值就是以他为根的子树中的节点个数减去以他为根的子树中不能给他带来贡献的子节点数目,也就是cnt[]+loss[]
我们可以直接在dfs过程中求出每一个节点的若干级父亲节点,这样方便我们来查找d[i]+1级父亲,也就是该节点第一个不能贡献的祖先节点,查询的复杂度就是log级别,所以总的复杂度就是nlogn级别。
细节见代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
using namespace std;
const int N=5e6+10;
int h[N],e[N],idx,ne[N];
int fu[N][21],cnt[N],loss[N],w[N];
long long f[N];
void add(int x,int y)
{
e[idx]=y;
ne[idx]=h[x];
h[x]=idx++;
}
void dfs(int x,int fa)
{
fu[x][0]=fa;
cnt[x]=1;
for(int i=1;i<=20;i++)
fu[x][i]=fu[fu[x][i-1]][i-1];
for(int i=h[x];i!=-1;i=ne[i])
{
int j=e[i];
if(j==fa) continue;
dfs(j,x);
cnt[x]+=cnt[j];
loss[x]+=loss[j];
}
int ff=x;
for(int i=20;i>=0;i--)
if(w[x]>>i&1) ff=fu[ff][i];
loss[fu[ff][0]]--;
f[x]=cnt[x]+loss[x];
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++) h[i]=-1;
for(int i=1;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
for(int i=1;i<=n;i++)
scanf("%d",&w[i]);
dfs(1,-1);
for(int i=1;i<=n;i++)
printf("%d ",f[i]);
return 0;
}
边栏推荐
猜你喜欢
随机推荐
wps表格下拉选项怎么添加?wps表格下拉选项的添加方法
腾讯技术支持实习二面——腾讯爸爸的临幸就是这么突然(offer到手)
选择排序
Mysql数据库身份证统计sql数据库加密等操作
人人熟知的IPV6竟然还有这么多细节
flutter 基本类写法
微信小程序错误 undefined Expecting ‘STRING‘,‘NUMBER‘,‘NULL‘,‘TRUE‘,‘FALSE‘,‘{‘,‘[‘, got ]解决方案
sess.restore() 和 tf.import_meta_graph() 在使用时的一些关联
PHP7.2开发物流自动拣货机流程
从stm32移植ucos2的代码到GD32
【Pytorch】学习笔记(一)
Alipay To Financial Ranking Name Modification
Kubernetes 1.25 中的删除和主要变化
【Verilog基础】关于芯片中信号串扰的理解
The second side of Tencent technical support internship - Tencent's father's luck is so sudden (offer received)
同花顺的炒股软件买股票安全正规可信吗?
Python object-oriented
从洞察到决策,一文解读标签画像体系建设方法论丨DTVision分析洞察篇
Hi3516 使用 wifi模块
MPLS Virtual Private Network Everywhere in Life