一个简单的dp ,统计每一个结点包括自身和所有下属结点的个数sum,并统计每一个结点感染后可以保存最多多少个结点ss表示,如果他的分支为0就是0为1就是他的儿子结点的sum-1,如果为2则是max(a[pace[0]].sum - 1 + a[pace[1]].ss, a[pace[1]].sum - 1 + a[pace[0]].ss);然后就是一些小细节的实现了

代码:

#include<bits/stdc++.h>
#define AC return 0;
#define int long long
using namespace std;
typedef pair<int, int> pii;
const int maxx = 300005;
const int mod = 1e9 + 7;
int n, m, t;
struct node {
    int sum;
    vector<int>next;
    bool vis;
    int deep;
    int ss;
    node() {
        deep = 0;
        vis = 0;
        sum = 0;
        ss = 0;
        next.clear();
    }
} a[maxx];
int dfs(int k) {

a[k].vis = 1;
    vector<int>pace;
    int len = a[k].next.size();
    for (int i = 0; i < len; i++)if(!a[a[k].next[i]].vis)pace.push_back(a[k].next[i]);
    if (a[k].next.size() == 1 && k != 1) {
        a[k].sum = 1;
        a[k].deep = 1;
        return a[k].sum;
    } else {
        a[k].sum = 1;
        int p = a[k].next.size();
        for (int i = 0; i < p ; i++) {
            if (!a[a[k].next[i]].vis)a[k].sum += dfs(a[k].next[i]);
        }
    }

if (pace.size() == 0) {
        a[k].ss = 0;
    } else if (pace.size() == 1) {
        a[k].ss = a[pace[0]].sum - 1;
    } else {
        a[k].ss = max(a[pace[0]].sum - 1 + a[pace[1]].ss, a[pace[1]].sum - 1 + a[pace[0]].ss);
    }
    return a[k].sum;
}
int ans = 0;
signed  main() {
    cin >> t;
    while (t--) {
        cin >> n;
        ans = 0;
        for (int i = 0; i <= n; i++) {
            a[i].next.clear();
            a[i].sum = 0;
            a[i].vis = 0;
            a[i].deep = 0;
            a[i].ss = 0;
        }
        for (int i = 1; i < n; i++) {
            int x, y;
            cin >> x >> y;
            a[x].next.push_back(y);
            a[y].next.push_back(x);
        }
        dfs(1);
        cout << a[1].ss << '\n';
    }
    AC
}

C - Infected Tree相关推荐

  1. Infected Tree(递归/树形)

    Infected Tree 传送门 Problem - C - Codeforces 思路 一开始第一眼以为是树形dp,就先把每个节点下面的节点个数(不包括自己)全求出来了,然后后面发现没用- 然后又 ...

  2. Codeforces Round #798 C. Infected Tree

    Byteland is a beautiful land known because of its beautiful trees. Misha has found a binary tree wit ...

  3. [dfs][思维]Infected Tree CF1689C

    Byteland is a beautiful land known because of its beautiful trees. Misha has found a binary tree wit ...

  4. C. Infected Tree(思维/dfs/树形dp)

    题目 题意: 给定一颗树 ,根节点为2,每个结点的子节点数不超过2(也就是说这是一个二叉树).现在根节点被感染了,我们需要拯救这棵树. 有n次过程,每次过程如下: 我们可以选择砍掉一个非感染点,使得该 ...

  5. (Codeforces798Div2)C. Infected Tree(思维)

    题目链接:Problem - C - Codeforces 样例输入: 4 2 1 2 4 1 2 2 3 2 4 7 1 2 1 5 2 3 2 4 5 6 5 7 15 1 2 2 3 3 4 4 ...

  6. A Time-Dependent SIR Model for COVID-19 With Undetectable Infected Persons

    A Time-Dependent SIR Model for COVID-19 With Undetectable Infected Persons 带有无法检测到的感染者的COVID-19的时变SI ...

  7. R语言SIR模型(Susceptible Infected Recovered Model)代码sir模型实例

    最近我们被客户要求撰写关于SIR的研究报告,包括一些图形和统计输出. SIR模型定义 SIR模型是一种传播模型,是信息传播过程的抽象描述. SIR模型是传染病模型中最经典的模型,其中S表示易感者,I表 ...

  8. 107. Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  9. 102. Binary Tree Level Order Traversal

    题目 Binary Tree Level Order Traversal 层次遍历二叉树 链接 Given a binary tree, return the level order traversa ...

最新文章

  1. Java复习2-对象与类
  2. linux 路由跟踪表满错误 nf_conntrack: table full, dropping packet 原理解决方法
  3. SAP WebIDE 里开发 SAP UI5 应用时,使用 Ctrl + Space 实现代码自动完成功能
  4. 装饰者模式如何拯救了我的一天
  5. 灰度MANA信托增持729.04万MANA,FIL持仓增长185%
  6. 数组题 倒置 加到字符串
  7. java hibernate注解_Hibernate注解方法使用总结
  8. 2020 快手 被吊打面经
  9. 惠普局域网共享打印机设置_网络共享惠普打印机的操作方法
  10. 图片放大镜 jQuery插件
  11. ROS2.9.27架设网吧软路由实战篇之端口映射与回流
  12. Java最准确的获取当前一周开始时间和结束时间
  13. 荐书:「时差党」——出国留学不是一件容易的事
  14. 我再copy回来。中海真是有心人。只是,你们在哪里?
  15. Data Migration 架构
  16. 2021-01-19第二次Sping Boot学习汇报--利用用Spring Initializr构建项目
  17. Python实现《人民的名义》关系可视化
  18. Angular读取文件内容并显示
  19. 【Python+OpenCV+sklearn+easygui】人脸(口罩)识别+口罩下人脸补全的系统设计
  20. 爆火的ChatGPT到底能做什么?5分钟带你进入AI的大门

热门文章

  1. python拆分excel并命名_python 拆分excel 随机分组
  2. 使用 TensorFlow 构建机器学习项目:6~10
  3. 软件工程师如何转型人工智能?
  4. adb调试夜神安卓模拟器和网易模拟器
  5. Android 利用 AsyncTask 异步读取网络图片
  6. Note 7爆炸原因是激进设计
  7. 转载python中append函数添加多个
  8. 智慧公路解决方案-最新全套文件
  9. 网络工程属计算机专业吗,网络工程专业属于什么门类
  10. CCF认证考试201809-2买菜-Python实现