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

原文地址:https://www.cnblogs.com/ppprseter/p/9770412.html

时间: 2024-08-30 15:07:56

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

【原创】leetCodeOj --- Binary Search Tree Iterator 解题报告

时间挤挤总是有的 太久不做题,脑子都生锈了.来道水题练练手 题目地址: https://leetcode.com/problems/binary-search-tree-iterator/ 题目内容: Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the

LeetCode: Binary Search Tree Iterator 解题报告

Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in

【LeetCode】257. Binary Tree Paths 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51474128 Subject 出处:https://leetcode.com/problems/binary-tree-paths/ Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-le

[LeetCode]Implement Trie(Prefix Tree),解题报告

目录 目录 概述 Trie树基本实现 定义Trie树节点 添加操作 查询word是否在Trie树中 AC完整代码 概述 Trie树,又称为字典树.单词查找树或者前缀树,是一种用于快速检索的多叉数结构.例如,英文字母的字典树是26叉数,数字的字典树是10叉树. Trie树的基本性质有三点,归纳为: 根节点不包含字符,根节点外每一个节点都只包含一个字符. 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串. 每个节点的所有子节点包含的字符串不相同. Trie树基本实现 我们通过Lee

CF911F Tree Destruction

题意翻译 给你一棵树,每次挑选这棵树的两个叶子,加上他们之间的边数(距离),然后将其中一个点去掉,问你边数(距离)之和最大可以是多少. 题目描述 You are given an unweighted tree with n n n vertices. Then n−1 n-1 n−1 following operations are applied to the tree. A single operation consists of the following steps: choose t

【百度之星2014~复赛)解题报告】The Query on the Tree

声明 笔者最近意外的发现 笔者的个人网站http://tiankonguse.com/ 的很多文章被其它网站转载,但是转载时未声明文章来源或参考自 http://tiankonguse.com/ 网站,因此,笔者添加此条声明. 郑重声明:这篇记录<[百度之星2014~复赛)解题报告]The Query on the Tree>转载自 http://tiankonguse.com/ 的这条记录:http://tiankonguse.com/record/record.php?id=673 前言

【百度之星2014~复赛 解题报告~正解】The Query on the Tree

声明 笔者近期意外的发现 笔者的个人站点http://tiankonguse.com/ 的非常多文章被其他站点转载.可是转载时未声明文章来源或參考自 http://tiankonguse.com/ 站点,因此.笔者加入此条声明. 郑重声明:这篇记录<[百度之星2014~复赛 解题报告~正解]The Query on the Tree>转载自 http://tiankonguse.com/的这条记录:http://tiankonguse.com/record/record.php?id=674

【LeetCode】Symmetric Tree 解题报告

Symmetric Tree 解题报告 [LeetCode] https://leetcode.com/problems/symmetric-tree/ Total Accepted: 106639 Total Submissions: 313969 Difficulty: Easy Question Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For

【LeetCode】103. Binary Tree Zigzag Level Order Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51524241 Subject 出处:https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ri