题解 CF1294F 【Three Paths on a Tree】
\]
打比赛的时候先开了 F 题(雾
然后一眼看出 F 题结论,最后居然因为没有判重,交了三次才过。
\]
给出一棵无权树(可理解为边权为 \(1\) ),你需要选取三个点 \(a,b,c\) ,最大化 \(a,b\) 和 \(b,c\) 和 \(a,c\) 的简单路径的并集的长度。
输出这个最大长度和 \(a,b,c\) 。
\]
有一个结论:
必定会有一组最优解,使得 \(a,b\) 是树直径上的端点。
这个结论我现在暂时不会证明,大家可以去看看其他 \(dalao\) 的证明或是自己给出证明 \(>v<\) 。
\(~\)
那我们可以套路地去把树直径两端点求出来,这里不推荐用 树形dp ,推荐大家用 两次搜索 求出树直径端点。
确定了 \(a,b\) ,接下来我们只要去找到最优的 \(c\) ,就可以最大化答案了。
此时我们注意到:\(a,b\) 和 \(b,c\) 和 \(a,c\) 的简单路径的并集的长度其实就是 \(\frac{dis(a,b)+dis(b,c)+dis(a,c)}{2}\) 。
此时 \(dis(a,b)\) 已经确定了,当 \(dis(b,c)+dis(a,c)\) 的值取到最大,那么整个式子取最大。
把 \(a,b\) 到所有点的简单路径距离求出来,去枚举这个最优的 \(c\) 即可,枚举的过程中记得判与 \(a,b\) 相同的情况。
\]
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define RI register int
using namespace std;
inline int read()
{
int x=0,f=1;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-f;s=getchar();}
while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
return x*f;
}
const int N=200100,M=400100;
int n;
int tot,head[N],ver[M],edge[M],Next[M];
void add(int u,int v,int w)
{
ver[++tot]=v; edge[tot]=w; Next[tot]=head[u]; head[u]=tot;
}
int d[N],vis[N];
int pos;
void bfs(int sta)
{
memset(d,0,sizeof(d));
memset(vis,0,sizeof(vis));
queue<int>q;
q.push(sta);
vis[sta]=1;
while(q.size())
{
int u=q.front();q.pop();
for(RI i=head[u];i;i=Next[i])
{
int v=ver[i],w=edge[i];
if(vis[v])continue;
d[v]=d[u]+w;
vis[v]=1;
if(d[v]>d[pos])pos=v;
q.push(v);
}
}
}
int p1,p2;
int ans;
int tmp1[N],tmp2[N];
int main()
{
n=read();
for(RI i=1;i<n;i++)
{
int u=read(),v=read();
add(u,v,1),add(v,u,1);
}
bfs(1);
p1=pos;
bfs(p1);
p2=pos;
for(RI i=1;i<=n;i++)
tmp1[i]=d[i];
bfs(p2);
for(RI i=1;i<=n;i++)
tmp2[i]=d[i];
pos=0;
for(RI i=1;i<=n;i++)
if(tmp1[i]+tmp2[i]>tmp1[pos]+tmp2[pos]&&i!=p1&&i!=p2)pos=i;
ans=(tmp1[p2]+tmp1[pos]+tmp2[pos])/2;
printf("%d\n",ans);
printf("%d %d %d\n",p1,p2,pos);
return 0;
}
\]
题解 CF1294F 【Three Paths on a Tree】的更多相关文章
- hdu 4912 Paths on the tree(树链拆分+贪婪)
题目链接:hdu 4912 Paths on the tree 题目大意:给定一棵树,和若干个通道.要求尽量选出多的通道,而且两两通道不想交. 解题思路:用树链剖分求LCA,然后依据通道两端节点的LC ...
- HDU 4912 Paths on the tree(LCA+贪心)
题目链接 Paths on the tree 来源 2014 多校联合训练第5场 Problem B 题意就是给出m条树上的路径,让你求出可以同时选择的互不相交的路径最大数目. 我们先求出每一条路径 ...
- codeforce F - Three Paths on a Tree
F. Three Paths on a Tree time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- CF741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths (dsu on tree) 题解
先说一下dsu算法. 例题:子树众数问题. 给出一棵树,每个点有点权,求每个子树中出现次数最多的数的出现次数. 树的节点数为n,\(n \leq 500000\) 这个数据范围,\(O(n \sqrt ...
- CF 741D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths [dsu on tree 类似点分治]
D. Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths CF741D 题意: 一棵有根树,边上有字母a~v,求每个子树中最长的边,满 ...
- [LeetCode]题解(python):145-Binary Tree Postorder Traversal
题目来源: https://leetcode.com/problems/binary-tree-postorder-traversal/ 题意分析: 后序遍历一棵树,递归的方法很简单,尝试用非递归的方 ...
- [LeetCode]题解(python):144-Binary Tree Preorder Traversal
题目来源: https://leetcode.com/problems/binary-tree-preorder-traversal/ 题意分析: 前序遍历一棵树,递归的方法很简单.那么非递归的方法呢 ...
- [LeetCode]题解(python):124-Binary Tree Maximum Path Sum
题目来源: https://leetcode.com/problems/binary-tree-maximum-path-sum/ 题意分析: 给定一棵树,找出一个数值最大的路径,起点可以是任意节点或 ...
- 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...
随机推荐
- js原生深拷贝
/*****************************************************************************************/ 原生js实现深拷 ...
- MongoDB高级用法
MongoDB高级查询用法大全 转载 http://blog.163.com/lgh_2002/blog/static/440175262012052116455/ 详见官方的手册:http://ww ...
- 【转】15个超炫的HTML5效果
英文原文:http://www.hongkiat.com/blog/15-html5-experiments/ 翻译:iteye 乔布斯没有给Flash任何机会,微软新推出的Windows 8 ...
- ThreadLocal = 本地线程?
一.定义 ThreadLocal是JDK包提供的,从名字来看,ThreadLocal意思就是本地线程的意思. 1.1 是什么? 要想知道他是个啥,我们看看ThreadLocal的源码(基于JDK 1. ...
- 通过例子进阶学习C++(四)计算2的64次方,不服写写看
本文是通过例子学习C++的第四篇,通过这个例子可以快速入门c++相关的语法. 1.乍一看题目非常简单,简单思考一下,可以通过for循环实现: #include <iostream> u ...
- 公司没有 DBA,Mysql 运维自己来
如果你的公司有 DBA,那么我恭喜你,你可以无视 Mysql 运维.如果你的公司没有 DBA,那你就好好学两手 Mysql 基本运维操作,行走江湖,防身必备. 环境:CentOS7 版本: 一.虚拟机 ...
- 为WPF, UWP 及 Xamarin实现一个简单的消息组件
原文地址:Implementing a simple messenger component for WPF, UWP and Xamarin 欢迎大家关注我的公众号:程序员在新西兰了解新西兰IT行业 ...
- docker-主从服务部署
欢迎访问我的博客http://www.liyblog.top 我的博客里会有更详细的信息,而且留言必回,手把手给你解释不懂的地方 1.mysql部署 mysql镜像拉取 docker pull ...
- isStatic:检测数据是不是除了symbol外的原始数据
function isStatic(value) { return( typeof value === 'string' || typeof value === 'number' || typeof ...
- html包含html文件的方法
我们在写asp页面的时候,常常使用include命令来包含公共文件.由于这个方法用起来非常方便,于是很多人在HTML页面里尝试使用include,但是发现根本就不起作用.这是因为,include是VB ...