codeforces 911 F. Tree Destruction

模板 - 树上问题(树的直径、动态查询树的直径、树的重心) 整理的算法模板合集: ACM模板 目录一、树的直径树形DP两次DFS / BFS(找到直径的两个端点)二、动态修改树的边权并求每个时刻的直径(线段树)三、树的重心 一、树的直径 树的直径满足如下性质: 若有多条直径,则所有的直径之间皆有公共点。 直径的两端一定是叶子。 树中距离某一直径端点最远的点,至少有一个是该直径的另一个端点。 对于树上任意一个点,与之距离最远的一个点,至少有一个直径的端点。 给你一个无权无向的树。编写程序以输出该树中最长路径(从一个节点到另一个节点)的长度。在这 阅读详情

题目链接

题意:

给定一棵树,每次可以选定树上的两个叶子,并删去其中的一个。答案每次加上两个选定的叶子之间的距离。求最后答案的最大值。

思路

首先,距离树上的任意一个节点最远的节点一定是某条直径的某个端点。

首先求出树的某一条直径,令其端点分别为L, R。把L看成树的根,那么R一定是叶子结点。对于那些非直径上的点,离他们最远的点肯定是L或R中的一个(可能也有其他的,但是L或R肯定已经最大了)。所以依次把这些非直径上的点删掉,删掉的时候在L和R中选择一个就行了。最后把直径删掉即可。

求直径可以两次bfs求,第一次从任意一个节点开始bfs,得到距离这个节点最远的那个节点一定是某条直径的某个端点,把这个端点记作p1,然后再从p1开始bfs,再求得该直径的另一个端点p2,然后直径就出来了。

代码写的很乱......

最开始写的代码,一直wa2,本地测试的结果和交上去的不一样,让希哥哥来找一下错误,发现是比较 ans1[x] 和 ans2[y] 的大小时写成了 ans1 和 ans2 ,然后改了一下,过了第2组数据,但是wa33,发现数据很大,爆 int 了,然后把结果改成 long long,还是 wa33,然后把所有数据类型全改成 long long ,过了。

希哥哥还是强啊

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<sstream>
#include<cstdlib>
#include<time.h>
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define repp(i,n,a) for(int i=n;i>=a;i--)
#define ll long long
#define ull unsigned long long
#define mem(a,x) memset((a),(x),sizeof ((a)))//x只能是0或-1或false或true
#define debug(x) cout<<"X: "<<(x)<<endl
#define de cout<<"************"<<endl
#define lowbit(x) ((x)&(-x))
#define lson rt<<1
#define rson rt<<1|1
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) a*b/(__gcd(a,b))
//#define inf 0x3f3f3f3f//1e9+6e7
#define inf 1000000000000000000
#define Eps 1e-8
#define Mod 1e9+7
#define maxn 100010
const double pi=acos(-1.0);
using namespace std;
const int N=200010;
ll n,m;
ll tot=0;
ll ver[N<<1],nex[N<<1],head[N],vis[N];
ll ans1[N],ans2[N];
ll a[N];
ll d[N],d1[N],d2[N];
ll p1,p2;
ll pre[N];
void add(ll x,ll y)
{
    ver[++tot]=y;
    nex[tot]=head[x];
    head[x]=tot;
}
ll bfs(ll p)
{
    rep(i,0,n)
        {vis[i]=0;d[i]=inf;}
    queue<ll> q;
    while(!q.empty())
        q.pop();
    q.push(p);
    vis[p]=1;
    d[p]=0;
    while(!q.empty())
    {
        ll x=q.front();
        q.pop();
        for(ll i=head[x];i;i=nex[i])
        {
            ll y=ver[i];
            d[y]=min(d[y],d[x]+1);
            if(vis[y]==0)
                {vis[y]=1;q.push(y);}
        }
    }
    ll maxx=-1,ret;
    rep(i,1,n)
    {
        if(d[i]>maxx)
        {
            ret=i;
            maxx=d[i];
        }
    }
    return ret;
}
void bfs1(ll p)
{
    rep(i,1,n)
        {ans1[i]=inf;vis[i]=0;}
    queue<ll> q;
    q.push(p);
    vis[p]=1;
    ans1[p]=0;
    while(!q.empty())
    {
        ll x=q.front();
        q.pop();
        vis[x]=1;
        for(ll i=head[x];i;i=nex[i])
        {
            ll y=ver[i];
            ans1[y]=min(ans1[y],ans1[x]+1);
            if(vis[y]==0)
                q.push(y);
        }
    }
}
void bfs2(ll p)
{
    rep(i,1,n)
        {ans2[i]=inf;vis[i]=0;}
    queue<ll> q;
    q.push(p);
    vis[p]=1;
    ans2[p]=0;
    while(!q.empty())
    {
        ll x=q.front();
        q.pop();
        vis[x]=1;
        for(ll i=head[x];i;i=nex[i])
        {
            ll y=ver[i];
            ans2[y]=min(ans2[y],ans2[x]+1);
            if(vis[y]==0)
                q.push(y);
        }
    }
}
void bfss()
{
    queue<ll> q;
    q.push(p1);
    rep(i,1,n)
        {vis[i]=0;d[i]=inf;}
    vis[p1]=1;
    d[p1]=0;
    while(!q.empty())
    {
        ll x=q.front();
        q.pop();
        vis[x]=1;
        for(ll i=head[x];i;i=nex[i])
        {
            ll y=ver[i];
            if(d[y]>d[x]+1)
            {
                d[y]=d[x]+1;
                pre[y]=x;
            }
            if(vis[y]==0)
                q.push(y);
        }
    }
}
void dfss(ll x)
{
    vis[x]=1;
    ll f=0;
    for(ll i=head[x];i;i=nex[i])
    {
        ll y=ver[i];
        if(y==p2)
            {f++;continue;}
        if(vis[y]==0)
            {f++;dfss(y);}
    }
    if(f==0||a[x]==0)
    {
        if(ans1[x]>ans2[x])
            cout<<p1<<" "<<x<<" "<<x<<endl;
        else
            cout<<p2<<" "<<x<<" "<<x<<endl;
        return ;
    }
}
int main()
{
    cin >> n;
    m=n-1;
    ll x,y;
    rep(i,1,m)
    {
        cin >> x >> y;
        add(x,y);
        add(y,x);
    }
    p1=bfs(1);
    p2=bfs(p1);
   // cout<<"*****"<<p1<<" "<<p2<<endl;       
    bfs1(p1);
    bfs2(p2);
    rep(i,1,n)
        a[i]=0;
    a[p1]=1;
    a[p2]=1;
    bfss();
    ll pp=p2;
    while(pre[pp]!=pp)
    {
        a[pre[pp]]=1;
        pp=pre[pp];
    }
    ll ans=0;
    ll cnt=0;
    rep(i,1,n)
    {
        if(a[i])
            {cnt++;continue;}
        ans+=max(ans1[i],ans2[i]);
    }
    ans+=(cnt*(cnt-1)/2);
    cout<<ans<<endl;
    rep(i,1,n)
        vis[i]=0;
    dfss(p1);
    pp=p2;
    rep(i,1,cnt-1)
    {
        cout<<p1<<" "<<pp<<" "<<pp<<endl;
        pp=pre[pp];
    }
    return 0;
}

 

从Simulink仿真到DSP28335真机部署:PID闭环控制快速移植指南 本文详细介绍了如何将Simulink中的PID闭环控制算法快速移植到DSP28335嵌入式平台。通过模型与代码映射、浮点与定点数选择、定时器中断配置、物理输出实现以及调试技巧五个关键步骤,帮助工程师高效完成从仿真到真机部署的全过程,特别适合需要实现精确控制的嵌入式开发者。 阅读详情

相关推荐

DIY音频必备:手把手教你用AB类功放模块打造高保真桌面音响(含电路图)

本文详细指导如何利用AB类功放模块DIY高保真桌面音响,涵盖选型指南、核心元器件清单、电路搭建与焊接工艺,以及系统调试与音质优化技巧。AB类功放以其接近A类功放的音质和B类功放的效率,成为音频爱好者的理想选择,适合有一定电子基础的实践者。

weixin_42665725的博客 443

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

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

weixin_30912051的博客 170

属性基加密(Attribute-based Encryption, ABE)

KP-ABE中,用户的密钥定义了访问策略,密文的属性与策略进行匹配。CP-ABE中,密文定义了访问策略,用户的属性与策略进行匹配。

2201_75392924的博客 1830

CF911F Tree Destruction(树的直径)

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

qq_62539009的博客 461

pytorch gpu运行安装重点(CUDA12.3)

pytorch gpu加速

weixin_45597212的博客 3万+

从入门到实战:openEuler 系统管理与运维笔记(持续更新)

本文为openEuler系统管理与运维的实战笔记,从CentOS迁移者视角切入,详细介绍了openEuler的生态兼容性、内核优化及安全特性。内容涵盖系统安装规划、虚拟机部署、基础命令操作、文件系统管理以及用户权限配置等核心运维技能,旨在帮助读者快速上手并掌握这一企业级操作系统的日常管理。

weixin_29207149的博客 222

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

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

weixin_30888413的博客 169

用R语言做WGCNA分析全步骤一(附代码解读)【转载】

代码逐句分析一、文章来源二、基因共表达网络构建及模块识别1.数据导入、清洗及预处理2.检查过度缺失值和离群样本3.聚类做离群样本检测4.载入临床特征数据三、自动构建网络及识别模块1.确定合适的软阈值:网络拓扑分析2.一步构建网络和识别模块 一、文章来源 初学WGCNA,觉得博主的代码写的很不错,但其中很多代码在第一遍看的时候有很多地方不理解,后查阅了很多资料,终于看明白了,于是写了一篇笔记,记录自己的学习心得,有不准确的地方,还望各位大佬们不吝赐教~ 原文链接:WGCNA简明指南 二、基因共表达网络构建及模

qq_43138237的博客 2万+

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

S7-1200五轴伺服系统PLC控制方案与调试技巧

工业自动化领域中,多轴伺服控制是实现精密加工的核心技术。基于PROFINET实时通信协议和PLCopen运动控制标准,现代PLC(如西门子S7-1200)能够实现多轴高精度同步控制。其技术原理是通过等时同步模式确保各轴运动的时序一致性,结合PID调节和电子齿轮比实现位置闭环。这种方案在数控机床、3D打印等场景具有重要应用价值。本文以五轴联动系统为例,详细解析了S7-1200与V90伺服驱动器的硬件组态方案,包括PROFINET环网配置和IRT同步设置。在软件层面,重点介绍了基于TIA Portal的工艺对象

weixin_34332905的博客 415

Sparse4Dv3 代码复现

打包数据集的元信息和标签,并生成所需的.pkl文件。最后还输出了很多跟踪相关的东西...然后通过K-means生成锚点。编译可变形聚合CUDA算子。安装motmetrics。

m0_60857098的博客 4487

PTA_07_03_树2_list leaves(详细讲解)

PTA_07_03_树2_list leaves Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. 给定一棵树,你应该按照从上到下,从左到右的顺序列出所有的叶子。 输入规格 Each input file contains one test case. For each case, the first line gives a positive intege

sxycylq的博客 591

传感器应用案例:压力传感器在工业自动化中的应用_(13).压力传感器的数据采集与处理

数据采集系统(Data Acquisition System, DAS)是连接传感器和控制系统的重要桥梁。它包括硬件(如数据采集卡、信号调理器)和软件(如数据采集软件、数据处理算法)两部分。信号调理:将传感器输出的模拟信号转换为适合AD转换的信号。AD转换:将模拟信号转换为数字信号。数据传输:将数字信号传输到控制系统或上位机。存储:将采集的数据存储在本地或远程数据库中。实时性:保证数据的实时性和准确性。压力传感器的数据采集与处理是工业自动化中不可或缺的一部分。

2401_87715305的博客 1620

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的博客 104

深度学习工具包 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")

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

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

【解题报告】Tree Destruction CodeForces - 911F(搜索??) ==================================================================== 【cf链接】 【vj链接】 ==================================================================

xuziling_的博客 667

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

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

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

codeforces911F - Tree Destruction

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

juruo? juruo! 598

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

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

weixin_30480651的博客 198

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

树的直径1——树的直径

树中最远的两个点的距离是“树的直径”。题目:树的直径1。“树的直径”的定义。关于“树的直径”的性质。查找“树的直径”的方法。

gaomuchen_120309的博客 502

[CF911F]Tree Destruction

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

weixin_33753845的博客 224

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

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

weixin_30772105的博客 160

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

题目链接:点击打开链接 题意:给一棵树(n个顶点,n-1条边),初始化一个值为0,每次选择树中两个顶点并删去其中一个,并加这两点的路径长加到该值上,直到删完所有边,求该值达到最大时的操作顺序和该值大小。 思路:找出树的直径,并对直径外的点从外到内依次删除(这里我粗暴的用了排序,路径长大的先输出,可优化),最后从直径的一个端点依次删除直径上的其他点,这样就能达到最大值。 AC代码如下:(950

LFhase‘s Blog 449

codeforces 963B Destruction of a Tree

http://www.elijahqi.win/archives/3449 题目描述 You 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 ver...

Elijahqi 307

CF911F Tree Destruction 解题报告

CF911F Tree Destruction 题意翻译 给你一棵树,每次挑选这棵树的两个叶子,加上他们之间的边数(距离),然后将其中一个点去掉,问你边数(距离)之和最大可以是多少. 输入输出格式 输入格式: The first line contains one integer number n \(n\) ( \(2 \le n \le 2 \times 10^{5}\) ) — the nu...

weixin_30819085的博客 288

leetcode — 1245.树的直径

链接:树的直径__牛客网 给定一棵树,求出这棵树的直径,即树上最远两点的距离。 包含n个结点,n-1条边的连通图称为树。 示例1的树如下图所示。其中4到5之间的路径最长,是树的直径,距离为5+2+4=11 输入 6,[[0,1],[1,5],[1,2],[2,3],[2,4]],[3,4,2,1,5] 输出 11 ...

柚子 1335
上一篇: 马拉车 ZOJ 4110
下一篇: codeforces 915 E 896 C 珂朵莉树
flyzer
博客等级 码龄9年 27粉丝 146原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值