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

    转自:http://www.cnblogs.com/atskyline/archive/2012/09/20/2694878.html 第一步 引入到Winows API 偷懒直接写在类里 1: [D ...

  2. RESTful Get方式传参json格式后端400 解决方案

    前端采用vue+axios 后端采用spring boot restful 问题: 前端get 请求需要传递array 字段值 后端由于tomcat 版本问题,不支持url接受特殊字符包括 [] {} ...

  3. php sprintf()

    在写php代码的时候,有时候会用到sprintf()这个函数,那么它是怎么用的呢? 学习源头: http://www.w3school.com.cn/php/func_string_sprintf.a ...

  4. Quartz.net 2.x 学习笔记02-Quartz.net 2.x在MVC站点中结合Log4net的使用

    Quartz.net 2.x在MVC站点中结合Log4net的使用 首先新建一个MVC的空站点: 第二步,添加Quartz.net的引用 在搜索处输入quartz.net搜索安装即可(目前是2.3) ...

  5. PHP面向对象深入研究之【对象生成】

    对象 看个例子 <?php abstract class Employee { // 雇员 protected $name; function __construct( $name ) { $t ...

  6. 五 搭建kafka集群

    1 下载    wget http://mirrors.tuna.tsinghua.edu.cn/apache/kafka/2.0.0/kafka_2.12-2.0.0.tgz 2 tar  -zxv ...

  7. php 中php-fpm 的重启、终止操作命令

    php 中php-fpm 的重启.终止操作命令: service nginx restart service php-fpm restart 查看php-fpm进程数:ps aux | grep -c ...

  8. 将Maven项目打包成可执行 jar文件(引用第三方jar)

    使用maven assembly插件完成打包 修改pom: <build> <pluginManagement> <plugins> <!--设置jdk版本, ...

  9. Python 小练习二 数据库MySQL、Redis

    import pymysql,redis def op_mysql(host,user,password,db,sql,port=3306,charset='utf8'): conn = pymysq ...

  10. iis7+的虚拟目录:未能加载程序集“**”。请确保在访问该页之前已经编译了此程序集

    在使用win8系统后,突然想运行iis,于是在windows组件中启用iis,并aspnet_regiis.exe -i注册iis后,于是开始发布了一个站点,一切正常 继而,在该站点下添加虚拟目录,然 ...