CF911F Tree Destruction

题意翻译

给你一棵树,每次挑选这棵树的两个叶子,加上他们之间的边数(距离),然后将其中一个点去掉,问你边数(距离)之和最大可以是多少.

输入输出格式

输入格式:

The first line contains one integer number n \(n\) ( \(2 \le n \le 2 \times 10^{5}\) ) — the number of vertices in the tree.

Next \(n-1\) lines describe the edges of the tree in form \(a_{i}\),\(b_{i}\)( \(1<=a_{i}\), \(b_{i}<=n\) , \(a_{i} \not= b_{i}\) ). It is guaranteed that given graph is a tree.

输出格式:

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 \(a_{i},b_{i},c_{i}\) , where \(a_{i},b_{i}\)— pair of the leaves that are chosen in the current operation ( \(1 \le a_{i}\) ,\(b_{i} \le n\) ), \(c_{i}\) ( \(1 \le c_{i} \le n\) , \(c_{i}=a_{i}\) or \(c_{i}=b_{i}\) ) — choosen leaf that is removed from the tree in the current operation.

See the examples for better understanding.


给了一个贪心的思路:不会产生比最好情况下还要差的结果(由最优推最优)

首先如果只有一条链,答案是很显然的。

如果链外有点,点到链的某个端点一定是所有情况的最优贡献,并且删去链外的点对链本身没有影响。

所以策略就是找到直径的那条链,一个一个删外面的点,最后删直径的。


Code:

#include <cstdio>
#define ll long long
const int N=2e5+10;
int Next[N<<1],to[N<<1],head[N],cnt;
void add(int u,int v)
{
to[++cnt]=v,Next[cnt]=head[u],head[u]=cnt;
}
int mx=-1,l,r;
void dfs1(int now,int fa,int d)
{
if(mx<d) mx=d,l=now;
for(int i=head[now];i;i=Next[i])
{
int v=to[i];
if(v!=fa)
dfs1(v,now,d+1);
}
}
int pre[N];
void dfs2(int now,int fa,int d)
{
if(mx<d) mx=d,r=now;
for(int i=head[now];i;i=Next[i])
{
int v=to[i];
if(v!=fa)
pre[v]=now,dfs2(v,now,d+1);
}
}
ll sum;
int ans[N][2],is[N],n,opt;
void dfs0(int now,int fa,int d,int s)
{
for(int i=head[now];i;i=Next[i])
{
int v=to[i];
if(is[v]||v==fa) continue;
dfs0(v,now,d+1,s);
}
if(!is[now]) ans[++opt][0]=now,ans[opt][1]=s,sum+=1ll*d;
}
int main()
{
scanf("%d",&n);
for(int u,v,i=1;i<n;i++)
{
scanf("%d%d",&u,&v);
add(u,v),add(v,u);
}
dfs1(1,0,0);
mx=-1;
dfs2(l,0,0);
int now=r,cnt1=0,cnt0=0;
while(now)
++cnt1,is[now]=1,now=pre[now];
now=r;
while(now)
{
++cnt0;
if(((cnt0-1)<<1)>cnt1-1)
dfs0(now,0,cnt0-1,r);
else
dfs0(now,0,cnt1-cnt0,l); now=pre[now];
}
now=r,cnt0=0;
while(now)
{
++cnt0;
ans[++opt][0]=now,ans[opt][1]=l,sum+=1ll*(cnt1-cnt0);
now=pre[now];
}
printf("%lld\n",sum);
for(int i=1;i<n;i++)
printf("%d %d %d\n",ans[i][0],ans[i][1],ans[i][0]);
return 0;
}

2018.10.11

CF911F Tree Destruction 解题报告的更多相关文章

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

    题目链接: http://codeforces.com/contest/911/problem/F 题目大意: 给你一棵树,每次挑选这棵树的两个度数为1的点,加上他们之间的边数(距离),然后将其中一个 ...

  2. 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)

    [LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...

  3. 【LeetCode】663. Equal Tree Partition 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  4. 【LeetCode】998. Maximum Binary Tree II 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  5. 【LeetCode】968. Binary Tree Cameras 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【LeetCode】515. Find Largest Value in Each Tree Row 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...

  7. 【LeetCode】513. Find Bottom Left Tree Value 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS Date 题目地址:https:// ...

  8. 【LeetCode】563. Binary Tree Tilt 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  9. 【LeetCode】257. Binary Tree Paths 解题报告(java & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...

随机推荐

  1. Zeppelin interperter 模式设置总结图解2

    该配置是在zeppelin的Interpreter的后台配置文件:conf/Interpreter.json spark Interpreter的模块定义那里.特别感谢开发团队组长大神的提示,深入挖掘 ...

  2. oracle下表空间、用户创建以及用户授权流程

    Oracle,表空间的建立.用户创建以及用户授权 主要是以下几步,挑选需要的指令即可: # 首先以scott用户作为sysdba登陆 conn scott/tiger as sysdba #创建用户 ...

  3. 描述linux目录结构以及目录结构命名规定

    FHS全称(Filesystem Hierarchy Standard),中文意思是目录层次标准,是linux的目录规范标准. 详情点击查看 FHS定义了两层规范: 第一层:“/”目录下的各个目录应该 ...

  4. php 微信公众号图文消息回复的实现 与access_token

    //代码如下 <?phpclass IndexAction extends Action { public function __construct(){ } public function i ...

  5. manjaro无法使用ifconfig查ip

    manjaro中自带的查看网络的命令是: ip addr 可以了解一下ip命令都有哪些功能 如果还是想要 ifconfig 需要安装net-tools 安装命令: sudo pacman -S net ...

  6. [Luogu1341]无序字母对(欧拉回路)

    按题意给定字符串建无向图,找欧拉回路 按照定义,当没有奇数度点或者只有2个奇数度点时才有欧拉回路 Code #include <cstdio> #include <algorithm ...

  7. 財務会計管理(FI&CO)

    FI(財務会計)系のSAP DBテーブル.随時更新していきます. [勘定コードマスタ]SKA1: 勘定コードマスタ(勘定コード表データ)SKB1: 勘定コードマスタ(会社コードデータ)SKAT: テキ ...

  8. Weblogic Linux jar包安装

    环境/工具: 系统:CentOS 7 JDK:Oracle JDK fmw_12.2.1.2.0_wls.jar 0x01.新建普通用户weblogic 在Linux环境下建议使用普通用户安装,web ...

  9. div嵌套img高度不相同

    div中嵌套img,如果div里嵌套一个img元素且div的高度是由img的高度来撑开,那么div的高度总会比img的高度多3px. 可以明显看到div实际高度高出img高度3px.为了解决此问题,我 ...

  10. HttpMessageConverter进行加密解密

    技术交流群: 233513714 使用自定义HttpMessageConverter对返回内容进行加密 今天上午技术群里的一个人问” 如何在 Spring MVC 中统一对返回的 Json 进行加密? ...