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

    Dijkstra算法虽然好,但是它不能解决带有负权边(边的权值为负数)的图. 接下来学习一种无论在思想上还是在代码实现上都可以称为完美的最短路径算法:Bellman-Ford算法. Bellman-F ...

  2. CentOS系统内核、操作系统位数以及系统参数查看

    2016-07-29 一.系统内核的查看方法 1.uname -a 显示详细的内核信息 Linux localhost.localdomain 3.10.0-229.el7.x86_64 #1 SMP ...

  3. MySQL连接查询、联合查询、子查询

    参考地址:http://blog.csdn.net/u011277123/article/details/54863371 1.MySQL连接查询 连接查询:将多张表(>=2)进行记录的连接(按 ...

  4. 2017中国大学生程序设计竞赛 - 女生专场(Graph Theory)

    Graph Theory Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)To ...

  5. jQuery笔记——选择器

    jQuery 最核心的组成部分就是:选择器引擎.它继承了 CSS 的语法,可以对 DOM 元 素的标签名.属性名.状态等进行快速准确的选择,并且不必担心浏览器的兼容性 常规选择器 根据id选择元素就是 ...

  6. AJAX中的跨域问题:什么是跨域?如何解决跨域问题?

    域不一样的,即为跨域,包括(协议,域名,端口号) 1. 指定允许其他域名访问 header('Access-Control-Allow-Origin:*'); 2.使用jsonp

  7. CVE-2017-8464(震网三代)复现

    开启msf root@sch01ar:~# msfconsole 设置模块 msf > use exploit/windows/fileformat/cve_2017_8464_lnk_rce ...

  8. storm和kafka整合

    storm和kafka整合 依赖 <dependency> <groupId>org.apache.storm</groupId> <artifactId&g ...

  9. ubuntu安装nginx踩坑

    ubuntu安装nginx 安装nginx tar -zxvf nginx-1.15.5.tar.gz -C /usr/local/src 解压 cd /usr/local/src/nginx-1.1 ...

  10. Windows nvidia显卡的设置方法

    1.直接右下角打开N卡控制面板托盘按钮,一般来说都是自动隐藏在小箭头里的,点开就可以显示隐藏的图标了.[插入图片“右下角小箭头”]2.开始菜单 - 控制面板 ,可以看到“显卡控制面板”这个选项,进去就 ...