发现从顶点入手不太方便,我们从这个“公共部分最长”开始考虑问题,因为要求这一条公共部分的链最长,可以联想到树的直径,那么本题就是要求一条类似于直径的东西使两个端点除了直径这一条链之外还有不少于两个的儿子,我们只要把这两个儿子交叉输出一下就得到了本题的答案。
只要两次$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. 为什么要编写轻量级的View Controller??

    1.作为iOS项目中最大的文件,ViewControllers中的代码复用率几乎是最低的2.重量级的View COntroller加大了测试的复杂度.所以关注ViewController的瘦身,把业务 ...

  2. cnn(卷积神经网络)比较系统的讲解

    本文整理了网上几位大牛的博客,详细地讲解了CNN的基础结构与核心思想,欢迎交流. [1]Deep learning简介 [2]Deep Learning训练过程 [3]Deep Learning模型之 ...

  3. Dawn 阿里开源前端开发构建工具

    Dawn 取「黎明.破晓」之意,原为「阿里云·业务运营团队」内部的前端构建和工程化工具,现已完全开源.它通过pipeline 和 middleware 将开发过程抽象为相对固定的阶段和有限的操作,简化 ...

  4. JsQuick--个人封装的Js库

    JsQuick 该库为本人封装的Js库,尚未进行浏览器兼容 /** * 快速框架 版本:1.0.0 * 日期:2015.02.26 * 作者:简楚恩 */ /** * 快速获取控件类 */ var $ ...

  5. Oracle变量的定义、赋值及使用

    首先我们来看看代码,然后我们在说明和解释代码: declare l_dept ; currtime date := sysdate; l_nam ) :),'yyyymmdd'); -- to_cha ...

  6. 笔记:开源协议 Apache 2 和 GPL 兼容

    笔记:开源协议 Apache 2 和 GPL 兼容 Apache 2 和 GPL v3 兼容. GPL 分了很多版本,LGPL 为最宽松的 GPL,而 AGPL 为最严格的 GPL 协议. Linux ...

  7. 冒泡排序的JavaScript实现

    1. 普通冒泡 思想 假设有n个数,按从小到大排序: 进行n-1次外循环,每次外循环会排好当前处理的数中的最大数,即进行第一次外循环排好所有数中的最大数,进行第二次外循环排好所有数中的次大数....直 ...

  8. Linux环境变量从用户配置改为系统配置

    部署了一个新的tomcat到一个新的用户下,发下启动失败了 /home/personal/apache-tomcat/bin/catalina.sh: line 434: /usr/lib/jvm/j ...

  9. Use the dkms from EPEL when install CUDA Toolkits on CentOS

    ###Use the dkms from EPEL. yum install epel-release yum install dkms # download the rpm from the NVi ...

  10. redis和phpredis扩展的安装

    redis的安装https://code.google.com/p/redis/downloads/list下载redisredis-2.6.13.tar.gztar -xvzf redis-2.6. ...