【解题报告】Tree Destruction CodeForces - 911F(搜索??)

CiteSpace关键词聚类图谱含义详细解析 CiteSpace关键词聚类图谱含义详细解析 回顾上一次推文:CiteSpace关键词共现图谱含义详细解析 其中有一句: 当你人工已经可以很容易的进行归纳后,就不需要再利用CiteSpace聚类功能啦。 我们来看一下上次推文做出来的关键词共现图谱: 人工不好归纳!那怎么聚类呢? 此时便可使用CiteSpace的聚类功能啦! 如下所示:我们可以清晰地看到上边的关键词共现网络聚成了一个个不规则区域... 阅读详情

【解题报告】Tree Destruction CodeForces - 911F(搜索??)

====================================================================

【cf链接】

【vj链接】

======================================================================

题目

You are given an unweighted tree with n vertices. Then n - 1 following operations are applied to the tree. A single operation consists of the following steps:

  1. choose two leaves;
  2. add the length of the simple path between them to the answer;
  3. remove one of the chosen leaves from the tree.
    Initial

Obviously after n - 1 such operations the tree will consist of a single vertex.

Calculate the maximal possible answer you can achieve, and construct a sequence of operations that allows you to achieve this answer!

Input

The first line contains one integer number n (2 ≤ n ≤ 2·105) — the number of vertices in the tree.

Next n - 1 lines describe the edges of the tree in form ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi). It is guaranteed that given graph is a tree.

Output

In the first line print one integer number — maximal possible answer.

In the next n - 1 lines print the operations in order of their applying in format ai, bi, ci, where ai, bi — pair of the leaves that are chosen in the current operation (1 ≤ ai, bi ≤ n), ci (1 ≤ ci ≤ n, ci = ai or ci = bi) — choosen leaf that is removed from the tree in the current operation.

See the examples for better understanding.

Examples

input

3
1 2
1 3

output

3
2 3 3
2 1 1

input

5
1 2
1 3
2 4
2 5

output

9
3 5 5
4 3 3
4 1 1
4 2 2

====================================================================

题目大意

有棵树,按照某规律删除结点并得到一个值。
某规律:找俩点,删掉其中一个
一个值:每次操作找的那俩点的距离

样例输入

第一行:一个n,表示树上有几个点
第2-n行:两个数x,y,表示一条边

输出

第一行:刚刚说的那一个值
第2-n行:三个数x,y,z,x,y表示一条边,z表示删除的那个点

====================================================================

解题思路

如果已经有一条路径(直径)是最长的,那其他点的最长路径一定是到达直径的端点的

假设这两个点叫p1,p2
要求的一个值叫ans

  • 步骤一:找到最长路径(两次dfs)

(从一个点出发,找到它的最长路径,那另一个点一定属于直径的端点)

  • 步骤二:除最长路径上的点全部删掉
  • 步骤三:删掉直径

(步骤二三通过分别从p1,p2遍历得到长度dis[i],dis2[i],用d[i]表示在直径上的某点离p2的距离,然后遍历一次,回溯时删除)
(求ans的过程在遍历之前,直接加d[i]或dis[i]或dis2[i]即可)

===================================================================

AC代码

不建议看这个代码,太乱了,幸好是1A,不然都不知道怎么改……

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#define LL long long
#define maxn 200007
using namespace std;

vector <int> edge[maxn];
int dis[maxn],vis[maxn],d[maxn],p1,p2,dis2[maxn],print[maxn];//print是方便输出的,不影响做题
void dfs1(int x)//求x到各点的距离存在dis数组里
{
    for(int i=0;i<edge[x].size();i++)
    {
        if(!vis[edge[x][i]])
        {
            vis[edge[x][i]]=1;
            dis[edge[x][i]]=dis[x]+1;
            dfs1(edge[x][i]);
        }
    }
}
void dfs3(int x)//求x到各点的距离存在dis2数组里
{
    for(int i=0;i<edge[x].size();i++)
    {
        if(!vis[edge[x][i]])
        {
            vis[edge[x][i]]=1;
            dis2[edge[x][i]]=dis2[x]+1;
            dfs3(edge[x][i]);
        }
    }
}
int dfs2(int x)//求x到直径上各点的距离,存在d数组里
{
    if(x==p2) return d[x]=1;
    for(int i=0;i<edge[x].size();i++)
    {
        if(!vis[edge[x][i]])
        {
            vis[edge[x][i]]=1;
            if(dfs2(edge[x][i]))
            {
                d[x]=d[edge[x][i]]+1;
                return 1;
            }
        }
    }
    return 0;
}
int dfs4(int x)//输出除直径外的点
{//printf("A");
    for(int i=0;i<edge[x].size();i++)
    {//printf("B");
        if(!vis[edge[x][i]])
        {
            vis[edge[x][i]]=1;
            dfs4(edge[x][i]);
            if(d[edge[x][i]]==0)
            {//printf("D");
                if(dis[edge[x][i]]>=dis2[edge[x][i]]) printf("%d %d %d\n",p1,edge[x][i],edge[x][i]);
                else printf("%d %d %d\n",p2,edge[x][i],edge[x][i]);
            }

        }
    }
}
int main()
{
    int n,i,j,x,y,maxlen;
    scanf("%d",&n);
    for(i=1;i<n;i++)
    {
        scanf("%d%d",&x,&y);
        edge[x].push_back(y);
        edge[y].push_back(x);
    }
    vis[1]=1;
    dis[1]=0;
    dfs1(1);
    maxlen=-1;
    for(i=1;i<=n;i++)
    {
        if(maxlen<dis[i])
        {
            maxlen=dis[i];
            p1=i;
        }
        vis[i]=0;
    }
    vis[p1]=1;
    dis[p1]=0;
    dfs1(p1);
    maxlen=-1;
    for(i=1;i<=n;i++)
    {
        if(maxlen<dis[i])
        {
            maxlen=dis[i];
            p2=i;
        }
        vis[i]=0;
    }
    //printf("p1=%d p2=%d \n",p1,p2);
    dfs2(p1);
    dis2[p2]=0;
    for(i=1;i<=n;i++)
        vis[i]=0;
    vis[p2]=1;
    dfs3(p2);
    LL ans=0;
    for(i=1;i<=n;i++)
    {
        //printf("%d %d %d\n",dis[i],dis2[i],d[i]);
        if(!d[i]) ans+=max(dis[i],dis2[i]);
        else ans+=d[i]-1,print[d[i]]=i;
    }
    printf("%lld\n",ans);
    for(i=1;i<=n;i++)
        vis[i]=0;
    vis[p1]=1;
    dfs4(p1);
    for(i=1;i<d[p1];i++)
    {
        printf("%d %d %d\n",p1,print[i],print[i]);//print记录的是那条直径
    }
    return 0;
}
【Python机器学习】决策树与随机森林:解释性与鲁棒性的平衡 本文深入探讨了决策树和随机森林在解释性与鲁棒性之间的权衡。决策树以其完美的可解释性、快速的推理速度和无需特征标准化等优势,在许多场景中仍然是首选算法。一个经过合理剪枝的决策树,其每个决策路径都可以转化为业务规则,这在金融风控、医疗诊断等对透明度有严格要求的领域无可替代。然而,单棵决策树的高方差和容易过拟合的特性限制了其泛化能力。随机森林通过集成学习的思想,巧妙地解决了这个矛盾。它维持了相对较好的解释性(通过特征重要性、OOB估计等方式),同时通过多棵树的投票或平均,大幅降低了模型的方差。 阅读详情

相关推荐

基于DE2-115红外解码实验

基于DE2-115红外解码实验   一、   功能描述 在同一个遥控电路中通常要使用实现不同的遥控功能或区分不同的机器类型, 这样就要求信号按一定的编码传送,编码则会由编码芯片或电路完成。对应于编码芯片通常会有相配对的解码芯片或包含解码模块的应用芯片。在实际的产品设计或业余电子制作中,编码芯片并一定能完成我们要求的功能,这时我们就需要了解所使用的编码芯片到底是如何编码的。只有知道编码方式,我...

乘风的博客 2516

codeforces911F - Tree Destruction

题面在这里题意:给一棵树,执行n次操作,每次操作流程如下: 1. 选两个叶子节点 2. 将答案加上这两个节点的距离 3. 删去任意一个节点要求最后答案最大。 问最大的答案以及构造出操作步骤。做法:一个简单的想法就是找一个深度最深的叶子,去更新别的叶子。 我们来看具体的情况。假设是下面这张图。红色的点就是深度最深的点,绿色是深度次深的点,且他们是处在根的不同儿子所在的子树。 于是对于最右边

juruo? juruo! 598

ICC II 时钟树综合(CTS)前奏:从约束定义到NDR规则的全方位设置指南

本文详细介绍了ICC II中时钟树综合(CTS)的前期准备工作,包括约束定义、NDR规则设置和时钟平衡点控制等关键步骤。通过实际案例和代码示例,指导工程师如何优化时钟偏斜、插入延迟和信号质量,确保芯片设计的时序收敛和性能稳定。特别强调了非默认布线规则(NDR)在时钟网络设计中的重要性。

weixin_42530458的博客 212

codeforces 911 F. Tree Destruction

题目链接 题意: 给定一棵树,每次可以选定树上的两个叶子,并删去其中的一个。答案每次加上两个选定的叶子之间的距离。求最后答案的最大值。 思路 首先,距离树上的任意一个节点最远的节点一定是某条直径的某个端点。 首先求出树的某一条直径,令其端点分别为L, R。把L看成树的根,那么R一定是叶子结点。对于那些非直径上的点,离他们最远的点肯定是L或R中的一个(可能也有其他的,但是L或R肯定已经最大...

flyzer的博客 369

Starlink卫星动力学系统仿真建模前传1-时空系统介绍

对于在轨道上运动的航天器由于速度接近第一宇宙速度,为了获取准确的位置信息对时间的精度要求极高,为了准确描述航天器的运动规律,本节对卫星仿真建模中用到的各种时间系统进行介绍。卫星轨道平面为坐标平面,坐标原点位于卫星系统质心,轴由卫星系统质心指向地心(当地垂线),轴在轨道面内垂直于轴并指向卫星的前进方向,轴同、轴构成右手系,指向轨道面负法线方向。不同坐标系下的建立的运动形式与复杂程度不同,会直接影响运动方程的求解繁简程度,因此,选取适当的坐标系具有重要的意义。定义星体坐标系,该坐标系简记为系,与卫星本体固连。

xspace的博客 1439

从零开始用Plaxis 2D设计深基坑支护结构:含地锚与桩墙实战教程

本文提供了一份基于Plaxis 2D的深基坑支护结构设计实战教程。教程以钻孔灌注桩排桩结合预应力锚索的典型支护体系为例,详细阐述了从地质模型建立、材料参数定义(重点介绍硬化土模型)、施工阶段模拟(含开挖、锚索施加预应力和地下水渗流固结耦合分析)到结果解读与设计优化的全流程。旨在帮助岩土工程师掌握数值模拟核心,实现从经验设计到量化分析的跨越。

kotlin6android的博客 505

Tree Destruction codeforces 911F

题目大意:给出一棵树,进行n-1次操作。每次操作包括找两个点,距离加到ans中,再删除其中一个点,n-1次操作后将只剩1个点,求最大ans。题解:先求出树的直径,两个端点为p1、p2,操作分2种。第一种,删除非直径上的点(a、b、c),如图                            操作时,选择的点对为{a,max(p1,p2)},max(p1,p2)表示a到p1,p2距离最大的点,删...

Spy97的博客 412

Jenkins中文显示不全?三步搞定Locale插件+汉化包的正确安装姿势

本文针对Jenkins界面中文显示不全问题,提供从原理到实战的深度解决方案。文章详细解析了Locale插件与中文语言包的核心机制,并分步指导如何通过配置JVM环境、正确安装插件及执行深度排查,彻底解决汉化顽疾,实现Jenkins界面的完美中文显示。

weixin_42611310的博客 260

Codeforces 911F Tree Destruction(贪心 && 树的直径)

题目链接Tree Destructi 题意 给定一棵树,每次可以选定树上的两个叶子,并删去其中的一个。答案每次加上两个选定的叶子之间的距离。   求最后答案的最大值。 首先求出树的某一条直径,令其端点分别为L, R。 把L看成树的根,那么R一定是叶子结点。 对于那些非直径上的点,离他们最远的点肯定是L或R中的一个(可能也有其他的,但是L或R肯定已经最大了) 所以依次把这些非...

weixin_30912051的博客 170

Odoo 入门 | 什么是 Odoo?

Odoo是一套全球开源的ERP/CRM系统,最初的名字为TinyERP,08年5月之后称之为OpenERP,之后又改为Odoo。

洛阳泰山的博客 6209

FFMPEG音频开发: Linux下采集摄像头(使用V4L2框架)数据录制成MP4视频保存到本地

一、环境介绍 操作系统介绍:ubuntu 18.04 FFMPEG版本: 4.4.2 摄像头: USB摄像头、虚拟机挂载本机自带摄像头 二、FFMPEG与X264下载编译 X264下载地址: http://www.videolan.org/developers/x264.html FFMPEG下载地址:https://ffmpeg.org/download.html Yasm ...

8873

[Codeforces 911F] Tree Destruction 解题报告(贪心)

题目链接: http://codeforces.com/contest/911/problem/F 题目大意: 给你一棵树,每次挑选这棵树的两个度数为1的点,加上他们之间的边数(距离),然后将其中一个点去掉,问你边数(距离)之和最大可以是多少. 要求输出每次选择的两个点和删掉的点 题解: 贪心题,策略是每次选择一个度数为1的点和距离它较远的直径的端点(显然直径的端点度数为1),然后把这...

weixin_30480651的博客 198

小梅哥Xilinx FPGA学习笔记11——亚稳态

亚稳态就是D触发器输入信号在其数据窗口期内发生变化,导致D触发器的输出进入一段时间的不稳定状态,有可能发生振荡,并最终随机稳定在高电平或者低电平。

m0_51430584的博客 631

Codeforces 911F Tree Destruction

Tree Destruction 先把直径扣出来, 然后每个点都和直径的其中一端组合, 这样可以保证是最优的。 #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL&g...

weixin_30236595的博客 105

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

Devexpress16.2.6破解工具

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

Educational Codeforces Round 35 F. Tree Destruction 树的直径

题目链接: https://codeforces.com/contest/911/problem/F 题意: 做法: 代码 #include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) #define rep_e(i,u,v) for(int i=head[u],v=to[i];~i;i=nex[i],v=to[...

人生如戏 238

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

Educational Codeforces Round 35 (Rated for Div. 2) F. Tree Destruction(dfs)

题目链接:http://codeforces.com/contest/911/problem/F 策略就是先删除非直径的点,然后删除直径上的点,dfs就行 代码: #include #define pb push_back using namespace std; typedef long long ll; const int MAXN=2e5+5; struct edge

bibibibi的博客 244

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

CF911F Tree Destruction(树的直径)

Problem - F - Codeforces 题意: 给一棵树,每次加上两个叶节点的距离并删除其中一个叶节点,问最大总距离和是多少。 题解: 叶节点的最大距离肯定是直径两端的距离;那么直径之外的点与其他节点的最远距离肯定在直径的某一端。那么我们首先把直径和其两端点求出来,再加上此节点到那个端点的距离;这个距离可用两个dfs 求直径端点时顺便求出;再考虑直径上的点,每次删掉一端的点就好了,sum+=; /*keep on going and never give up*/ #include&l

qq_62539009的博客 461

[CF911F]Tree Destruction

题目大意:给你一棵树,每次挑选这棵树的两个叶子,把答案加上他们之间的距离,然后将其中一个点去掉,问你距离之和最大可以是多少。要求输出方案 题解:求出直径和直径上的点,因为树上一个点可以提供的最大贡献为它到直径中的一个端点的距离,于是把不在直径上的点先删去(注意要按深度一个个删,因为要求删叶子)。 卡点:1.没有按深度从大到小删,删除了非叶子节点   2.爆$long\;long$ C...

weixin_33753845的博客 224

D. Destruction of a Tree

D. Destruction of a Tree time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given a tree (a graph withnvertices andn - 1e...

black_horse2018的博客 208

Codeforces.911F.Tree Destruction(构造 贪心)

题目链接 \(Description\) 一棵n个点的树,每次可以选择树上两个叶子节点并删去一个,得到的价值为两点间的距离 删n-1次,问如何能使最后得到的价值最大,并输出方案 \(Solution\) 树上距离,求最大,可以考虑下树的直径 假如已知树的直径u->v,那么任意一点x到达其他点的最远距离就是u,v中一点(如果不是这样,那直径一定可以更长而不是uv) 假设x距u最远,那肯定是删x...

weixin_30888413的博客 169

Destruction of a Tree (dfs+思维)

Destruction of a TreeYou are given a tree (a graph with n vertices and n - 1 edges in which it's possible to reach any vertex from any other vertex using only its edges).A vertex can be destroyed if t...

codeswarrior的博客 466

codeforce 911F

传送门:http://codeforces.com/problemset/problem/911/F题意: 给你一棵树,每次选两个点,答案加上这两个点的距离,然后删除其中一个点,重复直到只剩下一个点,求最大的答案及每次的操作 题解: (1)树的直径:树上最长的简单路径即为树的直径 (2)选择一个在直径外的点,与它距离最远的点一定是直径的两个端点之一 (3)每次从直径外找一个点,直

u011237384的专栏 345

G. Tree Destruction 【Codeforces Round 991 (Div. 3)】

Codeforces Round 991 (Div. 3) G. Tree Destruction DFS及其变种(dfs and similar)动态规划(dp)树形结构(trees)

csdn可能自动偷偷把博客设为vip可见,可以联系我改回去 382

Destruction of a Tree CodeForces - 964D

题面题意给出一棵树,你可以摧毁其中度数为偶数的点,同时与之相连的点将被一起摧毁,问能否摧毁所有点,若能,还要输出方案.做法首先可以发现点数为偶数的肯定不行,因为每次删除偶数条边,因此对于一对父子,若子节点的字子树大小为奇数,则肯定先摧毁其父节点再摧毁它,反之先摧毁其父节点再摧毁它,我们可以据此得出父子间的先后关系,再进行拓扑排序即可得出答案.代码#include<iostream> #include

yzyyylx的博客 814

Codeforces 911F 贪心

分析:先求出树的直径,以这个直径上的节点作为主干。 假设两个端点分别为x,y,求其他任意叶子节点z到另一个叶子节点的最长距离即为max{ d(x,z), d(y,z) } 该解法的正确性我无法给出证明,但画图看看应该就不难理解的。 代码如下: #include #include #include using namespace std; typedef long long LL;

勇敢前进的ACMer 1175

CF911F Tree Destruction (树的直径,贪心)

题目链接 Solution 1.先找出树的直径. 2.遍历直径沿途的每一个节点以及它的子树. 3.然后对于每个非直径节点直接统计答案,令直径的两个端点为 \(x_1,x_2\) . \[Ans=\sum{Max(dis(i,x1),dis(i,x2))}\] 最后再单独把直径拎出来,单独统计一次就好了. 正确性证明: redbag 一句话解决: 如果说这其中的某个答案不是最优,那么找的直径不就是...

weixin_30772105的博客 160
上一篇: 最长递增子序列
下一篇: 【模板】Dijkstra \ Floyed \ Prim
xuziling_
博客等级 码龄9年 1粉丝 58原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值