题目链接

Solution

1.先找出树的直径.

2.遍历直径沿途的每一个节点以及它的子树.

3.然后对于每个非直径节点直接统计答案,令直径的两个端点为 \(x_1,x_2\) .

\[Ans=\sum{Max(dis(i,x1),dis(i,x2))}
\]

最后再单独把直径拎出来,单独统计一次就好了.

正确性证明:

redbag 一句话解决:

如果说这其中的某个答案不是最优,那么找的直径不就是错的么。

"跑的飞快。"


Code

#include<bits/stdc++.h>
using namespace std;
const int maxn=200008;
struct sj{
int to,next;
}a[maxn*2];
int head[maxn],size,x,y,road[maxn];
int root,flag,n,cnt,far[5],kk,vis[maxn];
int dis[maxn][3],v[maxn],num;
long long ans;
int ans1[maxn],ans2[maxn],siz;
void add(int x,int y)
{
a[++size].to=y;
a[size].next=head[x];
head[x]=size;
} void dfs(int x)
{
v[x]=1;
if(cnt>kk)
far[root==1?1:2]=x,kk=cnt;
for(int i=head[x];i;i=a[i].next)
{
int tt=a[i].to;
if(!v[tt])
cnt++,dfs(tt);
}
v[x]=0; cnt--;
} void fuck(int x)
{
v[x]=1;
if(x==far[2]){flag=1;} for(int i=head[x];i;i=a[i].next)
{
int tt=a[i].to;
if(!v[tt])
fuck(tt);
if(flag){road[++num]=x;vis[x]=1;break;}
}
v[x]=0;
} void getans(int x)
{
v[x]=1;
for(int i=head[x];i;i=a[i].next)
{
int tt=a[i].to;
if(!v[tt]&&!vis[tt])
getans(tt),ans+=max(dis[tt][1],dis[tt][2]);
}
if(dis[x][1]>dis[x][2])
ans1[++siz]=far[1];
else ans1[++siz]=far[2];
ans2[siz]=x;
v[x]=0;
} void getdis(int x,int to)
{
v[x]=1;
for(int i=head[x];i;i=a[i].next)
{
int tt=a[i].to;
if(!v[tt])
{dis[tt][to]=dis[x][to]+1; getdis(tt,to);}
}
v[x]=0;
} int main()
{
cin>>n;
for(int i=1;i<n;i++)
{
scanf("%d%d",&x,&y),
add(x,y), add(y,x);
}
root=1; dfs(root);
root=far[1]; kk=0;
cnt=0; dfs(root);
fuck(root); getdis(far[1],1); getdis(far[2],2);
for(int i=2;i<num;i++)
getans(road[i]),siz--; for(int i=1;i<=kk;i++)ans+=i;
cout<<ans<<endl;
for(int i=1;i<=siz;i++)
cout<<ans1[i]<<' '<<ans2[i]<<' '<<ans2[i]<<endl;
for(int i=1;i<num;i++)
cout<<far[1]<<' '<<road[i]<<' '<<road[i]<<endl;
}

CF911F Tree Destruction (树的直径,贪心)的更多相关文章

  1. CF911F Tree Destruction 解题报告

    CF911F Tree Destruction 题意翻译 给你一棵树,每次挑选这棵树的两个叶子,加上他们之间的边数(距离),然后将其中一个点去掉,问你边数(距离)之和最大可以是多少. 输入输出格式 输 ...

  2. lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】

    1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

  3. Sonya and Ice Cream CodeForces - 1004E 树的直径, 贪心

    题目链接 set维护最小值贪心, 刚开始用树的直径+单调队列没调出来... #include <iostream>#include <cstdio> #include < ...

  4. Codeforces 804D Expected diameter of a tree(树的直径 + 二分 + map查询)

    题目链接 Expected diameter of a tree 题目意思就是给出一片森林, 若把任意两棵树合并(合并方法为在两个树上各自任选一点然后连一条新的边) 求这棵新的树的树的直径的期望长度. ...

  5. light oj 1094 Farthest Nodes in a Tree(树的直径模板)

    1094 - Farthest Nodes in a Tree problem=1094" style="color:rgb(79,107,114)"> probl ...

  6. SP1437 Longest path in a tree(树的直径)

    应该是模板题了吧 定义: 树的直径是指一棵树上相距最远的两个点之间的距离. 方法:我使用的是比较常见的方法:两边dfs,第一遍从任意一个节点开始找出最远的节点x,第二遍从x开始做dfs找到最远节点的距 ...

  7. [TJOI2017] 城市 (树的直径,贪心)

    题目链接 Solution 这道题,调了我一晚上... 一直80分 >_<|| ... 考虑到几点: 分开任意一条边 \(u\) ,那么其肯定会断成两棵树. 肯定是分开直径上的边最优,否则 ...

  8. [CF911F]Tree Destruction

    题意翻译 给你一棵树,每次挑选这棵树的两个叶子,加上他们之间的边数(距离),然后将其中一个点去掉,问你边数(距离)之和最大可以是多少. 首先我们知道,到一个点距离最远的点是直径的端点.考虑贪心,如果我 ...

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

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

随机推荐

  1. oracle系統表、數據字典介紹與日常問題診斷

    oracle系統表.數據字典介紹與日常問題診斷 數據字典是由唯讀的table和view組成的,產生於$oracle_home\rdbms\admin\catalog.sql.裡面儲存Oracle資料庫 ...

  2. Core BlueTooth官方文档翻译

    本⽂文是苹果<Core Bluetooth Programming Guide>的翻译. 关于Core Bluetooth Core Bluetooth 框架提供了蓝⽛牙低功耗⽆无线设备与 ...

  3. 【转】浅谈对主成分分析(PCA)算法的理解

    以前对PCA算法有过一段时间的研究,但没整理成文章,最近项目又打算用到PCA算法,故趁热打铁整理下PCA算法的知识.本文观点旨在抛砖引玉,不是权威,更不能尽信,只是本人的一点体会. 主成分分析(PCA ...

  4. 【wqs二分 决策单调性】HHHOJ#261. Brew

    第一道决策单调性…… 题目描述 HHHOJ#261. Brew 题目分析 挺好的……模板题? 寄存了先. #include<bits/stdc++.h> typedef long long ...

  5. [BZOJ] 4145: [AMPPZ2014]The Prices

    设\(f[S][i]\)表示考虑到第\(i\)家店,已经买了集合\(S\)内的物品 一个朴素的想法是枚举子集转移 \[ f[S][i]=\min\{f[T][i-1]+cost[S\oplus T][ ...

  6. Python + Bottle + 谷歌搜索Api 实现简单搜索引擎

    1.运行环境 python3 centos7 2.Bottle的使用 使用bottle主要是因为它仅用python自带的库即可实现对web的搭建. bottle源码分析 bottle使用教程 3.代码 ...

  7. Create & use FTP service on Ubuntu(在Ubuntu上搭建并使用FTP服务)

    Check if the FTP service has been installed.(检查是否已安装)   Vsftpd --version  If it has not install,Pres ...

  8. vue-cli3.0相关的坑

    vue-cli3.0相对比vue-cli2.0少了 vue-build.js vue-config.js vue-cli2.0 运行命令 npm run devvue-cli3.0 运行命令 npm ...

  9. python中字符串的一些用法

    一.字符串的拼接:      a=‘123’      b=‘abc’       d=‘hello world’ 1.print(a+b) 2.print(a,b) 3. c=‘ ’.join((a ...

  10. LeetCode(234) Palindrome Linked List

    题目 Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) t ...