发现从顶点入手不太方便,我们从这个“公共部分最长”开始考虑问题,因为要求这一条公共部分的链最长,可以联想到树的直径,那么本题就是要求一条类似于直径的东西使两个端点除了直径这一条链之外还有不少于两个的儿子,我们只要把这两个儿子交叉输出一下就得到了本题的答案。
只要两次$dfs$就可以分别求出这一条链的两个端点了,$dfs$的时候维护一下最大和次大两个儿子更新答案即可。
时间复杂度$O(n)$。
Code:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef pair <int, int> pin; const int N = 2e5 + ; int n, res, tot = , head[N], dep[N], fir[N], sec[N];
pin mx(, ); struct Edge {
int to, nxt;
} e[N << ]; inline void add(int from, int to) {
e[++tot].to = to;
e[tot].nxt = head[from];
head[from] = tot;
} inline void read(int &X) {
X = ; char ch = ; int op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} void dfs(int x, int fat, int depth) {
dep[x] = depth;
int son = ;
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(y == fat) continue;
dfs(y, x, depth + );
++son;
if(dep[fir[y]] > dep[fir[x]])
sec[x] = fir[x], fir[x] = fir[y];
else if(dep[fir[y]] > dep[sec[x]]) sec[x] = fir[y];
} if(!son) fir[x] = x;
if(son >= ) {
pin tmp = pin(dep[x], dep[fir[x]] + dep[sec[x]]);
if(tmp > mx) mx = tmp, res = x;
}
} int main() {
read(n);
for(int x, y, i = ; i < n; i++) {
read(x), read(y);
add(x, y), add(y, x);
} dfs(, , );
int ans1 = fir[res], ans2 = sec[res]; mx = pin(, );
memset(fir, , sizeof(fir));
memset(sec, , sizeof(sec));
dfs(res, , );
int ans3 = fir[res], ans4 = sec[res]; printf("%d %d\n%d %d\n", ans1, ans3, ans2, ans4);
return ;
}

CF1073F Choosing Two Paths的更多相关文章

  1. CodeForces 1073F Choosing Two Paths

    Description You are given an undirected unweighted tree consisting of \(n\) vertices. An undirected ...

  2. 贪心/构造/DP 杂题选做Ⅱ

    由于换了台电脑,而我的贪心 & 构造能力依然很拉跨,所以决定再开一个坑( 前传: 贪心/构造/DP 杂题选做 u1s1 我预感还有Ⅲ(欸,这不是我在多项式Ⅱ中说过的原话吗) 24. P5912 ...

  3. Codeforces-Educational Codeforces Round 53题解

    写之前,先发表下感慨:好久没写题解了,也许是因为自己越来越急利了,也可以说是因为越来越懒了. A. Diverse Substring 直接找一找有没有相邻的两个不同的字符即可. B. Vasya a ...

  4. Codeforces Round #114 (Div. 1) E. Wizards and Bets 高斯消元

    E. Wizards and Bets 题目连接: http://www.codeforces.com/contest/167/problem/E Description In some countr ...

  5. 洛谷 P2959 [USACO09OCT]悠闲漫步The Leisurely Stroll

    P2959 [USACO09OCT]悠闲漫步The Leisurely Stroll 题目描述 Bessie looks out the barn door at the beautiful spri ...

  6. Unity Lighting - Choosing a Rendering Path 选择渲染路径(三)

      Choosing a Rendering Path 选择渲染路径 Unity supports a number of rendering techniques, or ‘paths’. An i ...

  7. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  8. [LeetCode] Unique Paths II 不同的路径之二

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  9. [LeetCode] Unique Paths 不同的路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

随机推荐

  1. Spring 实现发送电子邮件的两种方法

     1.通过xml文件配置主要属性: xml文件:test.xml <bean id="mailSender" class="org.springframewor ...

  2. 剑指offer-第六章面试中的各项能力(不用加减乘除做加法)

    //不用加减乘除四则运算,来做加法 //题目:两个数做加法. //思路:用二进制的位运算的思路.第一步:首先两数相加考虑进位.可以用异或. //第二步:两个数相加只考虑进位,并将最后的结果左移.第三步 ...

  3. Java实现7种常见的排序算法

    数据结构中的内部排序:不需要访问外存便能完成,是一个逐步扩大记录的有序序列长度的过程. 可以分为5类: 1.插入排序:直接插入排序,稳定排序,时间复杂度为O(n^2)非递减有序,设置r[0]为哨兵进行 ...

  4. Spring Aware接口---BeanNameAware BeanFactoryAware ApplicationContextAware

    前言 对于应用程序来说,应该尽量减少对spring api的耦合程度,然后有时候为了运用spring提供的一些功能,有必要让bean了解spring容器对其管理的细节信息,如让bean知道在容器中是以 ...

  5. 在vue项目中正确的引入jquery和bootstrap

    <script>标签引入jquery在vue脚手架里并不适用,需要利用webpack引入jquery 一.第一种方法 1:因为已经安装了vue脚手架,所以需要在webpack中全局引入jq ...

  6. IronPython之基本类型

    通过下图展现IronPython的基本类型,便于理解和记忆. 基本数据类型 数据类型 类型 示例 备注 Byte string str ‘hello’ “hello” “””hello””” ‘’’h ...

  7. 开启mac terminal 命令/路径自动补全功能

    用惯了windows命令行工具的按Tab自动补全路径功能后,在mac terminal上敲命令很不习惯.其实mac terminal也有这个功能. 在命令行输入nano .inputrc 进入.inp ...

  8. ReactJS开发环境搭建与相关工具介绍

    现在Web开发的技术几年前相比可谓变化之大.各种各样的框架,各种各样的工具,让Web开发效率更高,开发出来的效果更好.同时带来的是开发环境的复杂度相比以前是成倍的增加.ReatJS框架是现在比较流行的 ...

  9. AngularJS:指令

    ylbtech-AngularJS:指令 1.返回顶部 1. AngularJS 指令 AngularJS 通过被称为 指令 的新属性来扩展 HTML. AngularJS 通过内置的指令来为应用添加 ...

  10. 初学者手册-IDEA常用快捷键

    一.快速创建基于某个接口的类(引入相关包) 左键选择接口名称,使用快捷键Alt+enter,然后实现该类 二.打开选中的文件所在的文件夹 点选需要打开的文件,右键菜单,点击“Show in Explo ...