[CSAcademy]Virus on a Tree

题目大意:

给你一棵\(n(n\le10^5)\)个点的树,一开始点\(1\)有病毒,可以沿着边扩散。你可以事先切掉若干条边,使得病毒扩散不超过\(k\)个结点。告诉你哪些边可以切,问最少需要切掉多少条边。

思路:

从\(1\)开始DFS,碰到一条可以切的边就返回。将得到的边按照子树大小排序,把能加上的边都加回去。

源代码:

#include<queue>
#include<cstdio>
#include<cctype>
#include<vector>
inline int getint() {register char ch;while(!isdigit(ch=getchar()));register int x=ch^'0';while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');return x;
}
const int N=1e5+1;
struct Edge {int to;bool w;
};
std::vector<Edge> e[N];
inline void add_edge(const int &u,const int &v,const bool &w) {e[u].push_back((Edge){v,w});e[v].push_back((Edge){u,w});
}
int size[N],tot;
std::priority_queue<int,std::vector<int>,std::greater<int> > q;
void dfs(const int &x,const int &par,const bool &flag) {size[x]=1;for(unsigned i=0;i<e[x].size();i++) {const int &y=e[x][i].to;if(y==par) continue;if(flag||!e[x][i].w) {dfs(y,x,flag);size[x]+=size[y];} else {dfs(y,x,true);q.push(size[y]);}}
}
int main() {const int n=getint(),m=getint();for(register int i=1;i<n;i++) {const int u=getint(),v=getint();add_edge(u,v,getint());}dfs(1,0,0);int tot=size[1];while(!q.empty()) {if(tot+q.top()>m) break;tot+=q.top();q.pop();}const int ans=q.size();printf("%d\n",tot<=m?ans:-1);return 0;
}

转载于:https://www.cnblogs.com/skylee03/p/10141114.html

[CSAcademy]Virus on a Tree相关推荐

  1. 数论三之排列组合Ⅱ——Virus Tree 2,RGB Coloring,123 Triangle,排列计数,排队,卡农

    丝且人一口 Virus Tree 2 description solution code RGB Coloring description solution code 123 Triangle des ...

  2. [CSAcademy]Cycle Tree

    [CSAcademy]Cycle Tree 题目大意: 定义环树是一张无向连通的简单图,它的生成方式如下: \(2\)个点\(1\)条边的图是环树: 对任意一个环树,加入\(k\)个点\(a_{1\s ...

  3. 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 ...

  4. 102. Binary Tree Level Order Traversal

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

  5. Python---哈夫曼树---Huffman Tree

    今天要讲的是天才哈夫曼的哈夫曼编码,这是树形数据结构的一个典型应用. !!!敲黑板!!!哈夫曼树的构建以及编码方式将是我们的学习重点. 老方式,代码+解释,手把手教你Python完成哈夫曼编码的全过程 ...

  6. [Java]LeetCode297. 二叉树的序列化与反序列化 | Serialize and Deserialize Binary Tree

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  7. Code Forces Bear and Forgotten Tree 3 639B

    B. Bear and Forgotten Tree 3 time limit per test2 seconds memory limit per test256 megabytes inputst ...

  8. Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈)

    Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈) 标签: codeforces 2017-06-02 11:41 29人阅读 ...

  9. datagrid底部显示水平滚动_DevExpress WPF v19.1:Data Grid/Tree List等控件功能增强

    行业领先的.NET界面控件DevExpress 日前正式发布v19.1版本,本站将以连载的形式介绍各版本新增内容.在本系列文章中将为大家介绍DevExpress WPF v19.1中新增的一些控件及部 ...

最新文章

  1. JVM最多支持多少个线程?
  2. 艾伟_转载:C#语言基础常见问题汇总
  3. 享有的意思是_“fuck you money”不是“x你钱”,真实意思却是这个
  4. Twitter Heron 实时流处理系统简介
  5. Java多线程系列--“JUC原子类”01之 框架
  6. 原生html小游戏,原生JS实现别踩白块小游戏(一)
  7. 修改mysql表的itemId字段为自增长SQL语句
  8. Mysql查询高速缓存区
  9. Tricks(三十三)—— 自定义 zipWithIndex
  10. opennms安装心得
  11. eclipse中编译java_eclipse编译java文件
  12. 基于RV1126平台imx291分析 --- rkcif_mipi注册
  13. 个人网页制作 大学生个人网页设计 个人网站模板 简单静态HTML个人网页作品
  14. 程序设计语言的基本概念
  15. 淘宝APP用户行为数据分析
  16. Python+FFmpeg提取哔哩哔哩安卓缓存
  17. 理论力学知识要点(五)
  18. [C++]cpp小笔记3 --- C++ String and char
  19. SAP能为纺织印染行业带来什么?
  20. excel基础-note-4.25

热门文章

  1. php常用案例,PHP常用数组处理函数总结,附带运行案例
  2. 单片机怎么跳出循环_自学单片机第二十七篇:矩阵按键的硬件测试
  3. python怎么输出字符串_python出输出字符串方式:
  4. php7做了哪些优化,PHP语言学习之PHP7做了哪些优化
  5. chrome开发者工具功能拾遗:Sources面板篇
  6. mysql 重要监控参数_mysql 的重要参数,监控需要
  7. bootstrap菜单展开收起_菜单展开及收缩效果 bootstrap+jquery
  8. 怎么用python打印照片_[宜配屋]听图阁
  9. python资本市场财务数据分析_Python对股票财务数据进行可视化分析
  10. 第十五届全国大学生智能车全国总决赛获奖信息-华北赛区