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

    Installation Qualification (IQ) 安装验证,安装时在用户处进行.它提供文件证明用户安装环境所有的规格及参数完全符合制造商的描述和安全 要求. Operation Qual ...

  2. [Luogu1527][国家集训队]矩阵乘法

    luogu 题意 给你一个\(N*N\)的矩阵,每次询问一个子矩形的第K小数.(居然连修改都不带的) \(N\le500,Q\le60000\) sol 整体二分+二维树状数组裸题. 复杂度是\(O( ...

  3. css处理最后一个li

    .relatebar li{width:98px;height:146px;padding:5px;float:left;border-left:1px solid #ccc;} .relatebar ...

  4. spring的<array>标签错误

    1,复习了一下spring xml的配置 单个默认命名空间 我们看到,在配置文件中,beans,bean等元素我们是没有使用命名空间前缀的.重复限定一个要在命名空间中使用的元素或属性可能会非常麻烦.这 ...

  5. PAT1018——最短路加DFS

    http://pat.zju.edu.cn/contests/pat-a-practise/1018 在杭州各个点,有很多自助自行车的点,最大容纳点为CMAX,但比较适合的情况是CMAX/2, 现在从 ...

  6. 如何让公司从SVN改到Git?

    把公司的SVN迁移到GitLab CE(GitLab社区版)原因主要有下面几个: 年青的新人进来,喜欢用git的越来越多 GitLab CE提供了优美的 web 界面,图形化分支结构,更直观的代码审查 ...

  7. 2012_p1 质因数分解 (prime.cpp/c/pas)

    2012_p1 质因数分解 (prime.cpp/c/pas) 时间限制: 1 Sec  内存限制: 128 MB提交: 80  解决: 27[提交][状态][讨论版][命题人:外部导入] 题目描述 ...

  8. 1132 Cut Integer

    题意:略. 思路:注意除数可能为0的情况,不然会导致浮点错误. 代码: #include <iostream> #include <string> using namespac ...

  9. HBuilder使用技巧

    ctrl+ Tab  切换 ctrl + shift + D 查找所写的函数

  10. zabbix的sendEmail配置

    zabbix的sendEmail配置 [root@hongquan scripts]# yum install sendmail[root@hongquan soft]# tar xvzf sendE ...