首页 > 其他分享 >Educational Codeforces Round 136 (Rated for Div. 2) D. Reset K Edges

Educational Codeforces Round 136 (Rated for Div. 2) D. Reset K Edges

时间:2024-09-20 15:01:21浏览次数:1  
标签:Reset Educational Rated int mid 深度 using dis

这道题目我们可以考虑二分做,二分出最终的深度,然后尝试是否能使用不超过\(k\)次操作使得深度符合条件。

考虑如何和判断,我们可以从根节点开始搜索,如果当前点的深度为\(mid + 1\),就对当前点进行操作。但很可惜,这种贪心方法可以很容易的举出反例,比如深度为\(mid\)的点下面有很多个叶子,此时不应该是修改叶子,而是修改深度为\(mid\)的点更优。

考虑反过来贪心,如果我们从叶子开始搜索,如果当前的点的深度为\(mid - 1\)且父节点不是根,则把当前点插到根上。

注意到题目保证了\(p_i < i\),所以我们直接倒序枚举就好了。

#include<bits/stdc++.h>

using namespace std;

using i32 = int32_t;
using i64 = long long;

const int mod = 1e9 + 7;

void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> p(n + 1);
    for (int i = 2; i <= n; i++)
        cin >> p[i];
    int l = 1, r = n - 1, res = -1;
    while (l <= r) {
        int mid = (l + r) / 2, cnt = 0;
        vector<int> dis(n + 1);
        for (int i = n; i > 1; i--) {
            if (dis[i] == mid - 1 and p[i] != 1) cnt++;
            else dis[p[i]] = max(dis[p[i]], dis[i] + 1);
        }
        if (cnt <= k) res = mid, r = mid - 1;
        else l = mid + 1;
    }
    cout << res << "\n";
}

i32 main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int T;
    cin >> T;
    while (T--)
        solve();
    return 0;
}

标签:Reset,Educational,Rated,int,mid,深度,using,dis
From: https://www.cnblogs.com/PHarr/p/18422546

相关文章

  • Analysis of Code and Test-Code generated by Large Language Models
    本文是LLM系列文章,针对《AnalysisofCodeandTest-CodegeneratedbyLargeLanguageModels》的翻译。大型语言模型生成的代码和测试代码的分析摘要1引言2方法3进行实验4测试结果的评估5讨论6相关工作7结论和未来工作摘要ChatGPT和Copilot等......
  • 【USB3.0协议学习】Topic3·三种Reset Events分析
    USB3.0中的三种ResetEvents1.PowerOnResetPowerOnReset被用来代指上电复位,当一个device接入到roothub或者外置hub的时候,该device检测到Vbus信号从无效变为有效,会自动执行复位。(注意,selfpowereddevice不通过Vbus供电,但是Vbus发生转变的时候它同样会执行复位)1.1软件设置P......
  • Git缓冲区理解:`index`,`add`和`reset`,`staged`和`unstaged`
    在git里面,有一个叫index的区域,你把东西加到那里叫add,把东西再从哪里撤回来叫reset;已经在里面的我们形容它是staged,还没有加进去的我们形容它是unstaged。其实index区就是一个纯粹的缓冲区,也叫stagingarea,是正式提交之前给我们的一个缓冲,还有犹豫的余地。因为一旦正式commit提交......
  • 开发nodejs RESETful api 创建项目流程
    开发nodejsRESETfulapi创建项目流程1.安装vm-windows、node.js和npm安装Node.js时,建议使用版本管理器,因为版本变更速度非常快。你可能需要根据所使用的不同项目的需要在多个Node.js版本之间进行切换。Node版本管理器(通常称为nvm)是安装多个版本的Node.js的最......
  • FVFL: A Flexible and Verifiable Privacy-Preserving Federated Learning Scheme--FV
    FVFL:AFlexibleandVerifiablePrivacy-PreservingFederatedLearningScheme--FVFL:一种灵活且可验证的隐私保护联邦学习方案来源导读AbstractIntroductionProblemStatementA.ProblemDefinitionB.ThreatModelandGoalsPreliminariesA.FederatedLearning(......
  • kex_exchange_identification: read: Connection reset
    问题截图解决手贱把这个禁用了(打开就行)其它参考https://stackoverflow.com/questions/69394001/how-can-i-fix-kex-exchange-identification-read-connection-reset-by-peer......
  • requests.exceptions.ConnectionError: (‘Connection aborted .’, ConnectionResetE
    requests.exceptions.ConnectionError:(‘Connectionaborted.’,ConnectionResetError(10054,"远程主机强迫关闭了一个现有的连接。',None,1656,None)欢迎来到英杰社区https://bbs.csdn.net/topics/617804998        欢迎来到我的主页,我是博......
  • A Comprehensive Survey of Accelerated Generation Techniques in Large Language Mo
    本文是LLM系列文章,针对《AComprehensiveSurveyofAcceleratedGenerationTechniquesinLargeLanguageModels》的翻译。大型语言模型中加速生成技术的全面调查摘要1引言2推测解码3早退4非自回归模型5讨论和局限性6结论摘要尽管在大型语言模型(L......
  • 忘记PbootCMS后台用户账号密码时进行重置resetpw.php
    <?php/***@copyright(C)2016-2099HnaoyunInc.*@licenseThisisnotafreeware,useissubjecttolicenseterms*@authorXingMeng*@[email protected]*@date2018年11月17日*重置PbootCMS用户密码*///设置字符集编码、IE文档模式header('Co......
  • Educational Codeforces Round 169(A-D)
    A.ClosestPoint        给你一组点。你必须在这个集合中加入一个整数的点,使它与集合中现有的每一个点不同,并且它成为与集合中每一个点**最近的点。这可能吗?(输入yesorno)    一道思路题,简单思考可以发现,如果数字超过两个,那么这题答案就是NO。当两个数字的......