题意

题目链接

给出一棵树,删除一些边,使得任意联通块内的任意点距离不超过$k$

sol

考场上想的贪心是对的:考虑一棵子树,如果该子树内最深的两个节点的距离相加$>k$就删掉最深的那个点,向上update的时候只返回最深的点的深度

然而却苦于写不出代码。。。

/*

*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<cmath>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/hash_policy.hpp>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define int long long
#define LL long long
#define ull unsigned long long
#define rg register
#define pt(x) printf("%d ", x);
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 22)], *p1 = buf, *p2 = buf;
//char obuf[1<<24], *O = obuf;
//void print(int x) {if(x > 9) print(x / 10); *O++ = x % 10 + '0';}
//#define OS *O++ = ' ';
using namespace std;
//using namespace __gnu_pbds;
const int MAXN = 1e6 + , INF = 1e9 + , mod = 1e9 + ;
const double eps = 1e-;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N, K, ans = ;
vector<int> v[MAXN];
int dfs(int x, int fa) {
if(v[x].size() == ) return ;
vector<int> dis;
for(int i = ; i < v[x].size(); i++) {
int to = v[x][i];
if(to == fa) continue;
dis.push_back(dfs(to, x) + );
}
sort(dis.begin(), dis.end());
while(dis.size() >= ) {
int x = dis[dis.size() - ], y = dis[dis.size() - ];
if(x + y > K)
ans++, dis.pop_back();
else break;
}
return dis.back();
}
main() {
N = read(); K = read();
for(int i = ; i <= N - ; i++) {
int x = read(), y = read();
v[x].push_back(y);
v[y].push_back(x);
}
for(int i = ; i <= N; i++)
if(v[i].size() > ) {dfs(i, ); break;}
printf("%d", ans + );
return ;
}
/*
2 2 1
1 1
2 1 1
*/

cf1042F. Leaf Sets(贪心)的更多相关文章

  1. CF1042F Leaf Sets (贪心+树上构造)

    题目大意:给你一棵树,让你对叶节点分组,保证每组中,任意两个叶节点之间的距离不大于K,求最小的组数 手动yy的贪心竟然对的 对于每个节点,维护一个$ma[i]$,表示在$i$节点的子树内 未被分组的叶 ...

  2. CodeForces 1042 F Leaf Sets 贪心

    Leaf Sets 题意:给你一棵树,树上有n个点,只有一条边的点叫做叶子,现在要求把所有的叶子分组,每个组内的所有叶子的距离都不能大于k. 题解: 我们可以随意找一个不是叶子的节点当做这颗树的根节点 ...

  3. [CF1042F]Leaf Sets

    题意:给定一棵$n$个点的树,将叶子节点分为数个集合使集合里点对最长距离不超过$k$,求最少集合数.($n\le1000000$) 首先我们可以想到,这道题并不是让你构造最优方案,因为只要把所有叶子节 ...

  4. 【CF1042F】Leaf Sets

    [CF1042F]Leaf Sets 题面 洛谷 题解 对于一个根节点\(x\),考虑其子树内的所有\(lca\)为它的叶子节点到它的距离\(d_1<d2<...<d_m\). 那么 ...

  5. CF 1042 F. Leaf Sets

    F. Leaf Sets http://codeforces.com/contest/1042/problem/F 题意: 将所有的叶子节点分配到尽量少的集合,一个可行的集合中两两叶子节点的距离< ...

  6. 「CF1042F」Leaf Sets

    传送门 Luogu 解题思路 比较显然的一种做法: 我们把一个点的子树高度抠出来并排序记为 \(L_i\),找到最大的 \(i\) 使得 \(L_{i-1}+L_i\le K\). 于是我们把前 \( ...

  7. CF722D. Generating Sets[贪心 STL]

    D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets 贪心

    D. Generating Sets 题目连接: http://codeforces.com/contest/722/problem/D Description You are given a set ...

  9. Generating Sets 贪心

    H - Generating Sets Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. 使用showOptionDialog显示多项选择框

    -----------------siwuxie095                             工程名:TestJOptionPane 包名:com.siwuxie095.showdi ...

  2. Spring入门第一课

    今天先不讲Spring是什么. Spring网址:http://projects.spring.io/spring-framework/ Eclipse 安装开发IDE 在Eclipse Market ...

  3. ES Docs-3:Modifying Data

    Modifying Data Indexing/Replacing Documents curl -XPUT 'localhost:9200/customer/external/1?pretty' - ...

  4. js注册

    ScriptManager.RegisterStartupScript(this, GetType(), "", "alert('修改失败!')", true) ...

  5. Vue 全家桶单元测试简要指南

    此文已由作者张汉锐授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. vue 的单元测试环境 按照目前全家桶的情况,是需要 webpack 的支持的.可以直接使用 vue-cli ...

  6. Go语言——没有对象的面向对象编程

    本文译自Steve Francia在OSCON 2014的一个PPT,原作请前往:https://spf13.com/presentation/go-for-object-oriented-progr ...

  7. Codeforces Round #522 Div2C(思维)

    #include<bits/stdc++.h>using namespace std;int a[200007];int b[200007][7];int ans[200007];int ...

  8. 清橙 A1210. 光棱坦克

    A1210. 光棱坦克 时间限制:1.0s   内存限制:512.0MB   总提交次数:   AC次数:   平均分:   将本题分享到:        查看未格式化的试题   提交   试题讨论 ...

  9. spark 机器学习 朴素贝叶斯 实现(二)

    已知10月份10-22日网球场地,会员打球情况通过朴素贝叶斯算法,预测23,24号是否适合打网球.结果,日期,天气 温度 风速结果(0否,1是)天气(0晴天,1阴天,2下雨)温度(0热,1舒适,2冷) ...

  10. dbms_xplan的display_cursor查看执行计划

    准备工作: SQL> conn sys/root as sysdba Connected. SQL> grant select on v_$sql_plan to scott; Grant ...