cf 1004E Sonya and Ice Cream

银河麒麟V10系统grub崩溃急救指南:从光盘救援到完整恢复(附常见错误排查) 本文详细介绍了银河麒麟V10系统grub崩溃的急救方法,从使用安装光盘进入救援环境到完整恢复系统的全流程。针对不同损坏程度(轻度、中度、严重)提供了分级恢复策略,包括grub2-install和grub2-mkconfig等关键命令的使用,并附有常见错误排查表,帮助用户快速解决引导问题。 阅读详情

一 原题

E. Sonya and Ice Cream
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Sonya likes ice cream very much. She eats it even during programming competitions. That is why the girl decided that she wants to open her own ice cream shops.

Sonya lives in a city with nn junctions and n−1n−1 streets between them. All streets are two-way and connect two junctions. It is possible to travel from any junction to any other using one or more streets. City Hall allows opening shops only on junctions. The girl cannot open shops in the middle of streets.

Sonya has exactly kk friends whom she can trust. If she opens a shop, one of her friends has to work there and not to allow anybody to eat an ice cream not paying for it. Since Sonya does not want to skip an important competition, she will not work in shops personally.

Sonya wants all her ice cream shops to form a simple path of the length rr (1≤r≤k1≤r≤k), i.e. to be located in different junctions f1,f2,…,frf1,f2,…,fr and there is street between fifi and fi+1fi+1 for each ii from 11 to r−1r−1.

The girl takes care of potential buyers, so she also wants to minimize the maximum distance between the junctions to the nearest ice cream shop. The distance between two junctions aa and bb is equal to the sum of all the street lengths that you need to pass to get from the junction aa to the junction bb. So Sonya wants to minimize

maxamin1≤i≤rda,fimaxamin1≤i≤rda,fi

where aa takes a value of all possible nn junctions, fifi — the junction where the ii-th Sonya's shop is located, and dx,ydx,y — the distance between the junctions xx and yy.

Sonya is not sure that she can find the optimal shops locations, that is why she is asking you to help her to open not more than kk shops that will form a simple path and the maximum distance between any junction and the nearest shop would be minimal.

Input

The first line contains two integers nn and kk (1≤k≤n≤1051≤k≤n≤105) — the number of junctions and friends respectively.

Each of the next n−1n−1 lines contains three integers uiui, vivi, and didi (1≤ui,vi≤n1≤ui,vi≤n, vi≠uivi≠ui, 1≤d≤1041≤d≤104) — junctions that are connected by a street and the length of this street. It is guaranteed that each pair of junctions is connected by at most one street. It is guaranteed that you can get from any junctions to any other.

Output

Print one number — the minimal possible maximum distance that you need to pass to get from any junction to the nearest ice cream shop. Sonya's shops must form a simple path and the number of shops must be at most kk.

Examples
input
Copy
6 2
1 2 3
2 3 4
4 5 2
4 6 3
2 4 6
output
Copy
4
input
Copy
10 3
1 2 5
5 7 2
3 2 6
10 6 3
3 8 1
6 4 2
4 1 6
6 9 4
5 2 5
output
Copy
7
Note

In the first example, you can choose the path 2-4, so the answer will be 4.

The first example.

In the second example, you can choose the path 4-1-2, so the answer will be 7.

The second example.

二 分析

给你一棵n个点的树,要求从树上找一条简单路径,路径上的点数不超过k,使得剩余的点到这条路径上的点的最大距离最小。比如图2,选取的点是{1, 2, 4},剩余最远的点是7和8,距离都是7,这是最优解。

可以证明这k个点都必须在树的直径上,为直径上的每个点i求两个值:d1(i)代表i到直径左端点的距离,d2(i)代表去除直径上的点后,i为根的子树中的点到i的最大距离。最多在直径上选k个点,相当于一个长度为k的区间在直径上滑动,每个区间对应的值是:


内层的max就是一个在d2数组上滑动窗口最大值的问题。


三 代码

/*
ID: maxkkibble
LANG: c++
PROB: cf 1004E
*/

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

// #define LOCAL
#define pb push_back
#define fi first
#define se second

typedef pair<int, int> PII;

const int maxn = 1e5 + 5;

int n, k;
vector<PII> g[maxn];

bool vis[maxn];  // useless util calc vector d2
int mx, id, pre[maxn], dis[maxn];

void dfs(int x, int fa) {
    pre[x] = fa;
    for (PII e: g[x]) {
        if (e.fi == fa || vis[e.fi]) continue;
        dis[e.fi] = dis[x] + e.se;
        if (dis[e.fi] > mx) mx = dis[e.fi], id = e.fi;
        dfs(e.fi, x);
    }
}

vector<int> dia, d1, d2;
int dia_len;

void calcDiameter() {
    int st, ed;
    mx = 0, id = 1;
    dfs(1, 0);
    st = id;
    mx = 0, id = st;
    dfs(st, dis[st] = 0);
    ed = id, dia_len = mx;
    while (ed != 0) {
        dia.pb(ed);
        d1.pb(dia_len - dis[ed]);
        vis[ed] = true;
        ed = pre[ed];
    }
    for (int i = 0; i < dia.size(); i++) {
        mx = 0, id = dia[i];
        dfs(dia[i], dis[dia[i]] = 0);
        d2.push_back(mx);
    }
}

int main() {
    #ifdef LOCAL
        freopen("e.in", "r", stdin);
    #endif
    ios::sync_with_stdio(false);
    cin >> n >> k;
    for (int i = 0; i < n - 1; i++) {
        int s, e, l;
        cin >> s >> e >> l;
        g[s].pb({e, l});
        g[e].pb({s, l});
    }
    
    calcDiameter();
    
    deque<PII> q;
    k = min(k, (int)dia.size());
    int ans = 1e9 + 5;
    for (int i = 0, j = 0; i <= dia.size() - k; i++) {
        while (j <= i + k - 1) {
            while (!q.empty() && d2[j] > q.back().fi)
                q.pop_back();
            q.push_back({d2[j], j});
            j++;
        }
        while (q.front().se < i) q.pop_front();
        int tmp = max({q.front().fi, d1[i], dia_len - d1[i + k -1]});
        ans = min(ans, tmp);
    }
    cout << ans << endl;
    return 0;
}

ConvNeXt 系列改进:引入大核注意力(LKA)替换 7×7 深度卷积,涨点 2.1% LSKA替换7×7深度卷积,在ImageNet分类任务上实现2.1%的显著提升,参数量仅增加1M。LSKA的四层1D分解将计算复杂度从O(k²)降至O(k),21×21大核的计算量仅相当于原版7×7的1.7倍,实现了感受野与计算成本的完美平衡。部署友好度极高:LSKA模块在TensorRT FP16量化后延迟仅1.2ms,精度损失<0.1%,远优于Transformer类模型。下游任务全面领先。 阅读详情

相关推荐

MS建模mmt

一、流程:1.定义好每个原子力场类型:Edit Sets(分组) 2.加H(一般是仅与Al-O八面体中的Al相连的O原子)、给H分组 3.“make P1”、校核化学式(正确与否) 4.扩成超晶胞(Na-mmt:4*1*1,Ca-mmt:4*2*1,illite:2*1*1,由化学式而定) 5.置换阳离子: mmt:模型中每 32 个 Si 4+有 1 个被 Al 3+ 取...

weixin_30721899的博客 2174

Sonya and Ice Cream(CF-1004E)

Problem Description Sonya likes ice cream very much. She eats it even during programming competitions. That is why the girl decided that she wants to open her own ice cream shops. Sonya lives in a ...

Alex_McAvoy的博客 509

Upwork数据战争:用爬虫与AI预测垄断订单

当普通自由职业者还在“等客户发需求”时,顶尖玩家已用数据预测客户明天需要什么; 当竞争对手熬夜抢单时,你的爬虫早已自动提交提案—— 这不是科幻,而是2025年的现实! 本文将手把手教你用合法工具搭建“数据战情室”,让每个决策都有万亿级数据支撑!

qq_61813593的博客 982

CF1004D Sonya and Matrix

https://www.luogu.com.cn/problem/CF1004D 分析题 首先可以通过镜像和旋转使得0在第二象限(左上方) 也可以发现距离0(x,y)0(x,y)0(x,y)最远的(n,m)(n,m)(n,m)就是给出的最大值,假设为bbb 即b=n+m−x−yb=n+m-x-yb=n+m−x−y 假设000距离(1,1)(1,1)(1,1)的距离为aaa 显然a=x+y−2a=x+y-2a=x+y−2 联立一下可以得到 a+b=n+m−2a+b=n+m-2a+b=n+m−2 n+m−b=x

lahlah的博客 310

从补码原理到实践:RISC-V中addi指令的符号扩展机制详解

本文深入解析了RISC-V架构中addi指令的符号扩展机制。从补码的数学原理出发,详细阐述了12位立即数在指令编码中如何通过符号位进行扩展,并结合lui指令,揭示了加载任意32位立即数的正确方法与实践陷阱,帮助开发者编写高效可靠的底层代码。

weixin_29161719的博客 139

A算法的蒙特卡洛树搜索

A*算法的蒙特卡洛树搜索 作者:禅与计算机程序设计艺术 1. 背景介绍 在人工智能和游戏开发领域,搜索算法是一个非常重要的研究方向。其中A算法和蒙特卡洛树搜索是两种广泛应用的经典算法。A算法是一种启发式搜索算法,通过评估各个节点到目标节点的启发式代价来引导搜索方向,在

AI天才研究院 1735

E. Sonya and Ice Cream【树的直径+单调队列】

E. Sonya and Ice Cream 题意:n个节点的树,选k个连续的一条路上的顶点,最小化最大距离,最大距离的定义为:max(其他点到这k个点的距离) [还有一个名字叫偏心距] 思路: k个点必定在树的直径上,证明:至少有1个点会在直径上,接下来选1个点,相邻的直径点还是其他分支的点?那么必定是相邻的直径点,因为右边部分的直径仍然是右边树的直径。那么很明显要去枚举连续的k个子区间,...

Haipai1998的博客 696

智能车图像处理5-入门级分析4 ---环岛的状态机与补线

PS :入门级文章适合新手,如果已经有基础可以去看进阶篇文章。

taiyuezyh的博客 8232

codeforce 1004 E. Sonya and Ice Cream(树的直径)

E. Sonya and Ice Creamtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputSonya likes ice cream very much. She eats it even during programming competi...

albertluf的博客 437

非洲秃鹫优化算法(AVOA)详解:从原理到实战

非洲秃鹫优化算法(AVOA)是一种模拟秃鹫觅食行为的元启发式算法。该算法通过数学建模秃鹫的群体分工、饥饿感知和食物竞争等行为,将优化问题转化为智能觅食过程。AVOA包含四个关键阶段:(1)种群初始化与最佳秃鹫选择;(2)饥饿率控制探索与开发平衡;(3)探索阶段模拟远距离飞行搜索;(4)开发阶段模拟食物源附近的竞争行为。算法采用饥饿率函数实现自适应搜索,初期偏向全局探索,后期转向局部开发。实验表明AVOA在标准测试函数和工程设计问题上表现出优越性能,其创新性在于将秃鹫独特的生物学特性转化为高效的优化机制。

qq_42212808的博客 228

117、HDR多帧融合算法:曝光序列、运动鬼影消除与合成权重的高效实现

HDR多帧融合算法实战经验总结 本文分享了HDR多帧融合算法在实际应用中的关键技术和优化经验。主要内容包括: 曝光序列设计:强调动态调整曝光时间比、帧间间隔和拍摄顺序的重要性,特别指出短-中-长的拍摄顺序优势。 运动鬼影消除:比较了三种运动检测方法,推荐梯度域检测方案,并强调在RAW域进行检测的必要性。 合成权重设计:提出基于高斯型权重函数的多因素考量方法,包括基础权重、运动权重和噪声权重的综合处理策略。 高效实现方案:提供NEON指令集加速的具体代码示例,包括梯度计算、权重查表和融合处理等关键步骤。 实用

zzfmlxs的博客 65

CodeForces 1004E Sonya and Ice Cream 贪心+树的直径+二分

E. Sonya and Ice Creamtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputSonya likes ice cream very much. She eats it even during programming competi...

weixin_39399748的博客 652

部署与可视化系统:移动端跟踪系统:YOLOv8 + ByteTrack 结合 Android CameraX 实现实时行人计数 App

本文详细介绍了在Android移动端实现实时行人检测、跟踪与计数系统的完整方案。系统采用YOLOv8+ByteTrack技术栈,基于NCNN推理引擎,无需云端算力即可在手机端侧完成全流程处理。文章从技术选型、系统架构、CameraX集成等多个维度进行深入解析,重点阐述了移动端部署面临的工程挑战和优化策略。通过四层管道模型设计,实现了从图像采集到目标跟踪的高效处理流程,并采用C++ NDK开发确保性能。该系统在中端手机上可达到10FPS,旗舰机型可达30FPS,为移动端实时目标跟踪提供了可落地的解决方案。

ZXSXJ的博客 91

CodeForces-1004E Sonya and Ice Cream(树型DP+RMQ)

E. Sonya and Ice Cream time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Sonya likes ice cream very much. She eats it even during p...

十分残念的博客 608

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

Choosing Ice Cream

You are standing in the supermarket in front of the freezers. You have a very tough task ahead of you: you have to choose what type of ice cream you want for after dinner that evening. After a while, ...

Dybala的博客 308

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

Codeforces Round #495 (Div. 2) - E. Sonya and Ice Cream

Codeforces Round #495 (Div. 2) - E. Sonya and Ice Cream Codeforces Round #495 (Div. 2) - E. Sonya and Ice Cream Description Input Output Examples Input Ouput Input Output 题意 题解 代码 Descri...

Simon的博客,已转移至 https://www.cnblogs.com/--Simon/ 485

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

cf 1004D Sonya and Matrix

一 原题D. Sonya and Matrixtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputSince Sonya has just learned the basics of matrices, she decided to play wi...

max_kibble的博客 390

Sonya and Matrix(CF-1004D)

Problem Description Since Sonya has just learned the basics of matrices, she decided to play with them a little bit. Sonya imagined a new type of matrices that she called rhombic matrices. These ma...

Alex_McAvoy的博客 658

Codeforces Round #495 (Div. 2)E. Sonya and Ice Cream

在树上找k个点,使树上每个点距能够找到的点距离最短。首先这k个点肯定是在直径上,其次怎么在直径上找k个点,我们可以用滑动窗口,学滑动窗口就要回单调队列,这玩意自己以前一直不会,最近才写题的时候遇到的。哎。首先用单调队列判断队尾元素是否该继续留在里面,如果不留在里面,就弹出来,然后把当前元素塞进去,然后判断队首元素是否超出滑动窗口的长度限制,如果超出,就弹出队首元素。代码中的dfs是用来找当前这个点...

yqdjl6的博客 256

Free Ice Cream(水题)

题目链接:http://codeforces.com/problemset/problem/686/A 题意分析: 水题,输入n,x,之后n行每行输入一个字符ch 和一个数字in 根据加减符号进行对应的加减运算,如果x 求最终x的值以及不能进行减法运算的in数量。 错因: 第一次交错了,这让我很吃惊,怎么看也不觉得写的有问题,回头看了一下数据范围,果断改成long long后过了。

smwqd_yehua_cx的博客 586

codeforces E. Sonya and Ice Cream(单调队列+滑窗+树的直径)

E. Sonya and Ice Cream time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Sonya likes ice cream very much. She eats it...

weixin_30653097的博客 208

Codeforces 1004E

题意略。 思路: 这k个点应该放在这棵树的直径上,并且能连成一条路径。如果k比树的直径上的点要多,那么我们就不用把这k个点都用上, 只需要把这棵树直径上所有的点都覆盖上就行了。如果k比树的直径上的点要少,那么我们尽量使这k个点放在这个直径的中心位置。 ...

weixin_30537451的博客 147

[codeforces] 1004 E. Sonya and Ice Cream

[codeforces] 1004 E. Sonya and Ice Cream 题目链接: E. Sonya and Ice Cream 题目大意: 给一个n(1≤n≤105)n(1≤n≤105)n (1 \le n \le 10^5)个点的树,每一条边有一个长度l(1≤l≤104)l(1≤l≤104)l(1 \le l \le 10^4), 现在让你找出一最长长度为k(1≤k≤...

ZZZZone的博客 550

二分+树的直径 [Sdoi2011]消防

问题 D: [Sdoi2011]消防 时间限制: 1 Sec 内存限制: 512 MB 提交: 12 解决: 6 [提交][状态][讨论版] 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000)。 这个国家的人对火焰有超越宇宙的热情,所以这个国家最兴旺的行业是消防业。由于政府对国民的热情忍无可忍(大量的消防经

蒟蒻QTY 657

cf 923C Perfect Security

一 原题 C. Perfect Security time limit per test 3.5 seconds memory limit per test 512 megabytes input standard input output standard output Alice has a very important

max_kibble的博客 1497

cf 923B Producing Snow

一 原题 C. Producing Snow time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Alice likes snow a

max_kibble的博客 1336
上一篇: cf 1004D Sonya and Matrix
下一篇: poj 2823 Sliding Window
max_kibble
博客等级 码龄11年 2粉丝 110原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值