码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • [树形dp]Maex 2022杭电多校第6场 1006


    Problem Description

    You are given a rooted tree consisting of nn vertices numbered from 11 to nn, and the root is vertex 11.

    Vertex ii has a natural number weight a_iai​, and \textbf{no two different vertexes have the same weight}no two different vertexes have the same weight.

    Define b_u = MEXbu​=MEX { x \space | \space \exists v \in subtree\left( u \right), x = a_vx ∣ ∃v∈subtree(u),x=av​}.

    Unfortunately, a_iai​ are not given. Please find out the maximum possible \sum_{i=1}^{n}b_i∑i=1n​bi​.

    The \textbf{MEX}MEX of a set is the minimum non-negative integer that doesn't belong to the set.

    Input

    The first line contains one integer T \left( 1 \leq T \leq 10 \right)T(1≤T≤10), indicating the number of test cases.

    For each test case:

    The first line contains one integer n \left( 1 \le n \le 5 \cdot 10^5 \right)n(1≤n≤5⋅105), indicating the number of nodes.

    In the following n-1n−1 lines, each line contains two interger u, v \left(1 \le u, v \le n \right)u,v(1≤u,v≤n), indicating an edge \left( u, v \right)(u,v) of the tree.

    A guarantee is that forming trees.

    Output

    For each test case: One line with an integer, indicating the maximum possible \sum_{i=1}^{n}b_i∑i=1n​bi​.

    Sample Input

    3

    5

    1 2

    3 2

    1 5

    4 1

    3

    1 2

    2 3

    1

    Sample Output

    8

    6

    1

    题意: 给出一棵树,树上各点都有一个点权ai,还各有一个价值bi,bi的值是点i子树中a值集合的MEX,ai的值可以为任意自然数,并且各ai值不同,求bi加和的最大值。

    分析: 根据样例模拟一下可知,只有从根节点到某个叶子结点路径上的点才会对答案有贡献,而且这个贡献值一定是该点子树中点的个数,可以设状态dp[i]表示在以i为根的子树中得到的b加和最大值,显然初始状态也就是叶子结点的dp值为1,之后当前结点now的dp值可以由子结点dp值加上子树中点个数得到,显然我们应该选取dp值最大的那个子结点,最后答案就是dp[1]。

    具体代码如下:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #define int long long
    9. using namespace std;
    10. vector<int> tr[500005];
    11. int num[500005], dp[500005];
    12. void dfs(int now, int fa){
    13. int mx = 0;
    14. for(int i = 0; i < tr[now].size(); i++){
    15. int to = tr[now][i];
    16. if(to == fa) continue;
    17. dfs(to, now);
    18. num[now] += num[to]+1;
    19. mx = max(mx, dp[to]);
    20. }
    21. dp[now] = mx+num[now]+1;
    22. }
    23. signed main()
    24. {
    25. int T;
    26. cin >> T;
    27. while(T--){
    28. int n;
    29. scanf("%lld", &n);
    30. for(int i = 1; i <= n; i++){
    31. dp[i] = 0;
    32. num[i] = 0;
    33. tr[i].clear();
    34. }
    35. for(int i = 1; i < n; i++){
    36. int u, v;
    37. scanf("%lld%lld", &u, &v);
    38. tr[u].push_back(v);
    39. tr[v].push_back(u);
    40. }
    41. dfs(1, 0);
    42. printf("%lld\n", dp[1]);
    43. }
    44. return 0;
    45. }

     

  • 相关阅读:
    Centos磁盘爆满_openEuler系统磁盘爆满清理方法---Linux工作笔记060
    node 第十二天 npm补充 详解package-lock.json在团队协作中的作用
    spring与spring boot升级官方指导文档
    金仓数据库 KingbaseES V8 GIS数据迁移方案(4. 基于SuperMap平台的数据迁移到KES)
    【C++杂货铺】再谈哈希算法:位图 | 布隆过滤器 | 哈希切分
    R语言ggplot2可视化:使用ggplot2可视化散点图、使用labs参数自定义Y轴的轴标签文本(customize Y axis labels)
    Win10怎么设置不进入屏保也不关闭显示器
    斗地主老是输?一起用Python做个AI出牌器!
    [jetson]jetson更新系统时候提示nvidia-l4t-bootloader的错误
    Jvm.分析工具(jconsole,jvisualvm,arthas,jprofiler,mat)
  • 原文地址:https://blog.csdn.net/m0_55982600/article/details/126166327
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号