CF911F Tree Destruction 解题报告
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 解题报告的更多相关文章
- [Codeforces 911F] Tree Destruction 解题报告(贪心)
题目链接: http://codeforces.com/contest/911/problem/F 题目大意: 给你一棵树,每次挑选这棵树的两个度数为1的点,加上他们之间的边数(距离),然后将其中一个 ...
- 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)
[LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...
- 【LeetCode】663. Equal Tree Partition 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】998. Maximum Binary Tree II 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【LeetCode】968. Binary Tree Cameras 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】515. Find Largest Value in Each Tree Row 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
- 【LeetCode】513. Find Bottom Left Tree Value 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS Date 题目地址:https:// ...
- 【LeetCode】563. Binary Tree Tilt 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】257. Binary Tree Paths 解题报告(java & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...
随机推荐
- lintcode_397_最长上升连续子序列
最长上升连续子序列 描述 笔记 数据 评测 给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列.(最长上升连续子序列可以定义为从右到左或从左到 ...
- Webpack + Vue 多页面项目升级 Webpack 4 以及打包优化
0. 前言 早在 2016 年我就发布过一篇关于在多页面下使用 Webpack + Vue 的配置的文章,当时也是我在做自己一个个人项目时遇到的配置问题,想到别人也可能遇到跟我同样的问题,就把配置的思 ...
- PHP小练习题
前几天在百度知道里面看到有位网友询问如何制作一下的小程序:用php语言设计一个小程序,计算今天到达下月的天数.全部输出这些天数,并使得每天的日期以三种颜色循环显示,设置三个表单,让用户选择字体颜色,然 ...
- python中函数的不定长参数
例1: #定义一个含有不定长参数的函数,本例第三个参数*args def sum_nums(a,b,*args): print('_'*30) print(a) print(b) print(args ...
- C语言进阶—— 逻辑运算符分析15
印象中的逻辑运算符: ---学生:老师,在我的印象中,逻辑运算符用在条件判断的时候,真挺简单的,还有必要深究吗? ---老师:逻辑运算符确实在条件判断的时候用的比较多,但是并不能说简单... 请思考下 ...
- idea debug启动项目慢或者启动不了
使用debug无法启动项目但是使用run就可以启动程序,而且启动比以前的debug模式快的多 原因: 启动不了的原因是在项目中的方法上打了断点,导致项目无法继续编译 取消方法断点就可以了 在idea官 ...
- HDU 6386 Age of Moyu
Problem Description Mr.Quin love fishes so much and Mr.Quin’s city has a nautical system,consisiting ...
- 笔记-python-多线程-深入-1
笔记-python-多线程-深入-1 1. 线程池 1.1. 线程池:控制同时存在的线程数量 threading没有线程池,只能自己控制线程数量. 基本有两种方式: 每间隔一段时间创建 ...
- Hibernate---开发环境搭建
下载安装Hibernate 在官网http://hibernate.org/tools/上下载eclipse安装插件的文件/安装插件的地址.点击download选择JBoss Tools,选择Arti ...
- Pascal小游戏 随机函数
一个被人写滥了的小程序,新手学习,Pascal By Chaobs 初学者可以用它来学习随机函数的运用,当然你完全可以自己写一个随机函数. var player1,player2:longint; ...