传送门

文章目录

  • 题意:
  • 思路:

题意:

思路:

首先有一个贪心策略就是每次都找一个叶子节点最多的点,让后删掉他的kkk个叶子节点,现在我们就来考虑如何模拟这个过程。
我们整一个vector<set<int>>vector<set<int>>vector<set<int>>来存边和叶子,方便删除点,让后再整一个setsetset按照leaf[i].size()leaf[i].size()leaf[i].size()排序,由于是按照可变大小的值来排序的,那么必须要先从setsetset中删除这个点,让后再修改leafleafleaf,再加入这个点才可以,不然出大问题。之后我们就可以模拟删除点了,要提前预处理出来leafleafleaf,而且删除点的时候有肯能某个点也成为叶子节点,这个时候也需要修改leafleafleaf,注意细节就好。

由于我nt了,先该的值再删点,代码已经面目全非,跟题解差不多了。这个题主要还是要找到合适的方法去模拟删边就比较好做辣。

最后还要注意特判k==1k==1k==1的情况。

// Problem: F. Removing Leaves
// Contest: Codeforces - Codeforces Round #656 (Div. 3)
// URL: https://codeforces.com/contest/1385/problem/F
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n,k;
vector<set<int>>v,leaf;struct cmp {bool operator () (int a,int b) const {if(leaf[a].size()==leaf[b].size()) return a<b;return leaf[a].size()>leaf[b].size();}
};int solve() {if(k==1) return n-1;set<int,cmp>s;int ans=0;for(int i=0;i<n;i++) s.insert(i);while(1) {int now=*s.begin();if(leaf[now].size()<k) break;for(int i=1;i<=k;i++) {int to=*leaf[now].begin(); s.erase(now); s.erase(to);leaf[now].erase(to);if(leaf[to].count(now)) leaf[to].erase(now);v[now].erase(to); v[to].erase(now);if(v[now].size()==1) {int ne=*v[now].begin();s.erase(ne); leaf[ne].insert(now);s.insert(ne);}s.insert(now); s.insert(to);}ans++;}return ans;
}int main()
{//  ios::sync_with_stdio(false);
//  cin.tie(0);int _; scanf("%d",&_);while(_--) {scanf("%d%d",&n,&k);v=vector<set<int>>(n);leaf=vector<set<int>>(n);for(int i=1;i<=n-1;i++) {int a,b; scanf("%d%d",&a,&b);a--; b--;v[a].insert(b); v[b].insert(a);}for(int i=0;i<n;i++) if(v[i].size()==1) {leaf[*v[i].begin()].insert(i);}printf("%d\n",solve());}return 0;
}
/**/

Codeforces Round #656 (Div. 3) F. Removing Leaves 贪心 + 模拟相关推荐

  1. Codeforces Round #694 (Div. 2) F. Strange Housing (贪心思维)

    F. Strange Housing 题意 有 nnn 个点和 mmm 条边,对点进行染色.要求一条边的两个点不能都染色,并且删除两端都没有染色的边之后,图连通.请给出一种染色方案. 题解 暴力贪心即 ...

  2. Codeforces Round #699 (Div. 2) F - AB Tree(贪心、树上DP)超级清晰,良心题解,看不懂来打我 ~

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #699 (Div. 2) F - AB Tree Problem ...

  3. 暴力+构造 Codeforces Round #283 (Div. 2) C. Removing Columns

    题目传送门 1 /* 2 题意:删除若干行,使得n行字符串成递增排序 3 暴力+构造:从前往后枚举列,当之前的顺序已经正确时,之后就不用考虑了,这样删列最小 4 */ 5 /************* ...

  4. Codeforces Round #644 (Div. 3) F.Spy-string

    Codeforces Round #644 (Div. 3) F.Spy-string 题目链接 You are given n strings a1,a2,-,an: all of them hav ...

  5. Codeforces Round #849 (Div. 4) F. Range Update Point Query

    Codeforces Round #849 (Div. 4) F. Range Update Point Query 题目大意: 给一串数字,有两个操作: 操作1:将 l − r l-r l−r 的数 ...

  6. Codeforces Round #656 (Div. 3) D. a-Good String

    Codeforces Round #656 (Div. 3) D. a-Good String 题目链接 You are given a string s[1-n] consisting of low ...

  7. Codeforces Round #538 (Div. 2) F. Please, another Queries on Array? 线段树 + 欧拉函数

    传送门 文章目录 题意: 思路: 题意: 给你一个序列aaa,你需要实现两种操作: (1)(1)(1) 将[l,r][l,r][l,r]的aia_iai​都乘rrr. (2)(2)(2) 求ϕ(∏i= ...

  8. Codeforces Round #742 (Div. 2) F. One-Four Overload 构造 + 二分图染色

    传送门 文章目录 题意: 思路: 题意: 给你一个n∗mn*mn∗m的矩形,包含...和XXX,你有两种颜色,你需要给...染色使得每个XXX上下左右相邻的...其两种颜色个数相同,输出一种合法方案. ...

  9. Codeforces Round #740 (Div. 2) F. Top-Notch Insertions 线段树 / 平衡树 + 组合数学

    传送门 文章目录 题意: 思路: 题意: 思路: 考虑最终的序列是什么鸭子的,首先序列肯定单调不降,也就是a1≤a2≤a3≤...≤ana_1\le a_2\le a_3\le ...\le a_na ...

最新文章

  1. 通过了OCP的全部考试后的感受(ZT)
  2. 神策 FM | 科学创造「高价值」人生,终止“瞎忙”式勤奋
  3. 数学--数论--因子和线性筛 (模板)
  4. 定制化WordPress后台的6个技巧
  5. 树莓派Java程序运行_树莓派上Java程序作为linux服务并开机自动启动
  6. (转)Spring Boot(六):如何优雅的使用 Mybatis
  7. html文件很大,webpack打包之后的文件过大如何解决
  8. Cisco Enhanced Object Tracking
  9. ubuntu18机器学习环境安装基于anaconda
  10. 《梦断代码》阅读笔记03
  11. Tomcat8安装、配置与启动
  12. 图层蒙版和图层剪贴路径_剪贴蒙版,PS选择蒙版技巧都在这了
  13. 为什么中国的游戏没有做出像欧美那种自由度很高的RPG游戏?
  14. cmake 添加asan功能
  15. 分布式系统架构系列讲解八(分布式一致性 8):PBFT算法
  16. 安卓报错:E/EGL_adreno: tid 2148: eglSurfaceAttrib(1338): error 0x3009 (EGL_BAD_MATCH)
  17. e城e家携新奥智慧共创美丽枣庄
  18. Unrecognized DataType
  19. 初级会计实务(2020年)——第一章 会计概述
  20. 电脑内存介绍(精心整理)

热门文章

  1. 70%的单身女孩都是这样想的!
  2. 基于 Python 自建分布式高并发 RPC 服务
  3. python find函数_Python 速学!不懂怎么入门python的小白看这篇就够了!
  4. lisp正负调换_坐标提取lisp程序
  5. python浪漫代码_五行Python代码实现批量抠图
  6. navicat循环执行上下两行相减sql语句_SQL语句的优化分析
  7. python变量名称跟着循环,在Python中使用列表中的名称循环创建新变量
  8. oracle重新编译package,如何有效的编译数据库中的失效对象(Package,trigger等)
  9. python反射、闭包、装饰器_python之闭包、装饰器、生成器、反射
  10. redis session java获取attribute_redis里的数据结构