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 ...
随机推荐
- Redhat 6.4 linux系统不重启识别热添加的硬盘方法
1.1 选择虚拟机添加一块硬盘 1.2 查看系统当前磁盘信息 [root@zhongyi-test ~]# ls -l /dev/sd* brw-rw----. 1 root disk 8 ...
- Oracle 11g R2在 win7 64位的安装流程图解【含常见问题解决方案】
ORACLE数据库系统是美国ORACLE公司(甲骨文)提供的以分布式数据库为核心的一组软件产品,是目前最流行的客户/服务器(CLIENT/SERVER)或B/S体系结构的数据库之一.Oracle 11 ...
- SSO 单点登录总结(PHP)
本篇文章根据个人理解的知识整理汇总,如有不足之处,请大家多多指正. 单点登录(SSO--Single Sign On)的应用是很普遍的,尤其在大型网站系统中,比如百度,登录百度账号和,再转到百度经验. ...
- Aizu:2224-Save your cats
Save your cats Time limit 8000 ms Memory limit 131072 kB Problem Description Nicholas Y. Alford was ...
- P1395 会议(求树的重心)
P1395 会议 题目描述 有一个村庄居住着n个村民,有n-1条路径使得这n个村民的家联通,每条路径的长度都为1.现在村长希望在某个村民家中召开一场会议,村长希望所有村民到会议地点的距离之和最小,那么 ...
- curl 编译
curl是利用URL语法在命令行方式下工作的开源文件传输工具.它被广泛应用在Unix.多种Linux发行版中,并且有DOS和Win32.Win64下的移植版本.支持很多协议:FTP, FTPS, HT ...
- elasticsearch索引和映射
目录 1. elasticsearch如何实现搜索 1.1 搜索实例 1.2 es中数据的类型 1.3 倒排索引 1.4 分析与分析器 1.4.1 什么是分析器 1.4.2 内置分析器种类 1.4.3 ...
- shell编程——参数传递
1.Linux Shell参数引用 $0 这个程式的执行名字$n 这个程式的第n个参数值,n=1..9$* 这个程式的所有参数$# 这个程式的参数个数$$ 这个程式的PID$! 执行上一个背景指令的P ...
- viewDidLoad dispatch_sync
- (void)viewDidLoad { [super viewDidLoad]; NSLog(@"1"); dispatch_sync(dispatch_get_main_qu ...
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目6
2014-03-21 21:50 题目:给定一个MxN的二位数组,如果每一行每一列都是升序排列(不代表全展开成一个一维数组仍是升序排列的).请设计一个算法在其中查找元素. 解法:对于这么一个数组,有两 ...