Gardener and Tree 图论,树,队列
题意 :
- 给一无根树,每次操作将所有叶子结点去除,问k次操作后还剩多少结点
思路 :
- 输入时存每个点的入度和出度,然后把度 小于等于 1的放入队列并记录为第一层,然后从队头开始弹出元素,枚举当前点的邻边,假设枚举到的邻边的点的度-1后等于1,那么说明它是下一层的点,更新层数并加入队列,最后枚举每个点的层数是否大于k,大于k则是被剩下的点
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#define debug(a) cout << #a << " = " << a << endl;
#define x first
#define y second
using namespace std;
typedef long long ll;const int N = 4e5 + 10;int n, m;
int d[N], vis[N];
int h[N], ne[N * 2], e[N * 2], idx; // 无向图void init()
{idx = 0;for (int i = 1; i <= n; i ++ )h[i] = -1, d[i] = 0, vis[i] = 0;
}void add(int a, int b)
{e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}int main()
{ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);int _;cin >> _;while (_ -- ){cin >> n >> m;init(); // 初始化图for (int i = 1; i <= n - 1; i ++ ){int a, b;cin >> a >> b;add(a, b), add(b, a);d[a] ++ , d[b] ++ ;}queue<int> q;for (int i = 1; i <= n; i ++ )if (d[i] <= 1){vis[i] = 1;q.push(i);}while (q.size()){int t = q.front(); q.pop();for (int i = h[t]; ~i; i = ne[i]){int j = e[i];if ( -- d[j] == 1){vis[j] = vis[t] + 1;q.push(j);}}}int ans = 0;for (int i = 1; i <= n; i ++ )if (vis[i] > m)ans ++ ;cout << ans << endl;}return 0;
}
Gardener and Tree 图论,树,队列相关推荐
- C++SEG TREE线段树的实现算法(附完整源码)
C++SEG TREE线段树的实现算法( C++SEG TREE线段树的实现算法(完整源码(定义,实现,main函数测试) C++SEG TREE线段树的实现算法(完整源码(定义,实现,main函数测 ...
- C++实现trie tree字典树(附完整源码)
实现trie tree字典树 实现trie tree字典树算法的完整源码(定义,实现,main函数测试) 实现trie tree字典树算法的完整源码(定义,实现,main函数测试) #include ...
- 转载——Regression Tree 回归树
本文转载于" 一个拉风的名字"的"Regression Tree 回归树" 1. 引言 AI时代,机器学习算法成为了研究.应用的热点.当前,最火的两类算法莫过于 ...
- Mysql存储引擎之TokuDB以及它的数据结构Fractal tree(分形树)
在目前的Mysql数据库中,使用最广泛的是innodb存储引擎.innodb确实是个很不错的存储引擎,就连高性能Mysql里都说了,如果不是有什么很特别的要求,innodb就是最好的选择.当然,这偏文 ...
- tree traversal (树的遍历) - postorder traversal (后序遍历)
tree traversal (树的遍历) - postorder traversal (后序遍历) 1. tree traversal - 树的遍历 二叉树的遍历 (traversing binar ...
- tree traversal (树的遍历) - preorder traversal (前序遍历)
tree traversal (树的遍历) - preorder traversal (前序遍历) 1. tree traversal - 树的遍历 二叉树的遍历 (traversing binary ...
- tree traversal (树的遍历) - inorder traversal (中序遍历)
tree traversal (树的遍历) - inorder traversal (中序遍历) 1. tree traversal - 树的遍历 二叉树的遍历 (traversing binary ...
- ROS学习|Behavoir Tree(BT树)--c++实现
系列文章目录 Behavoir Tree(BT树)–基本概念 Behavoir Tree(BT树)–c++实现 Behavoir Tree(BT树)–c++实现第二节 目录 系列文章目录 创建行为树 ...
- Memory Analyzer Tool 1 Shallow heap Retained heap dominator tree(控制树)
文章目录 1. shallow heap 2. retained heap 3. dominator tree(控制树) 1. shallow heap 引用原文介绍 :Shallow heap is ...
最新文章
- NAT的配置与相关概念的理解
- 每日一皮:不同的视角看待问题...
- Java1.8 帮助文档
- maven项目下tomcat直接启动不了(LifecycleException)。报错如下截图
- Spring Boot + Thymeleaf 创建web项目
- 鼠标点击实现花瓣雨_每周实验 | 黄金雨
- Flink :刷屏 forceKillApplication over null,Retrying immediately The Client is Stopped
- the railway problem(the example of stack)
- RichEdit控件的使用(未完)
- java连接远程带有密码的mongodb数据库
- Unity3D教程:尽量避免使用foreach
- oracle regexp_instr,Oracle REGEXP_INSTR 用法
- android 平板端应用商店,安卓平板电脑软件商店--乐商店是目前最安全的Android应用商店...
- python 百度cpc点击
- js 身份证号隐藏中间数字
- R语言使用caret包的knnreg函数拟合KNN回归模型:使用predict函数和训练好的模型进行预测推理、使用plot函数可视化线图对比预测值和实际值曲线
- 百度实习两个月小结~
- html表白程序源码 html生日快乐网站制作 HTML生日快乐代码 表白程序源码html 程序员表白代码html
- Windows机制下的游戏编程实例一
- 红图新媒体让你知道提升新媒体广告ROI需要关注的10个指标,