D. Two Paths

题目连接:

http://codeforces.com/contest/14/problem/D

Description

As you know, Bob's brother lives in Flatland. In Flatland there are n cities, connected by n - 1 two-way roads. The cities are numbered from 1 to n. You can get from one city to another moving along the roads.

The «Two Paths» company, where Bob's brother works, has won a tender to repair two paths in Flatland. A path is a sequence of different cities, connected sequentially by roads. The company is allowed to choose by itself the paths to repair. The only condition they have to meet is that the two paths shouldn't cross (i.e. shouldn't have common cities).

It is known that the profit, the «Two Paths» company will get, equals the product of the lengths of the two paths. Let's consider the length of each road equals 1, and the length of a path equals the amount of roads in it. Find the maximum possible profit for the company.

Input

The first line contains an integer n (2 ≤ n ≤ 200), where n is the amount of cities in the country. The following n - 1 lines contain the information about the roads. Each line contains a pair of numbers of the cities, connected by the road ai, bi (1 ≤ ai, bi ≤ n).

Output

Output the maximum possible profit.

Sample Input

4

1 2

2 3

3 4

Sample Output

1

Hint

题意

给你一棵树,让你选择两条不相交的路径,使得长度的乘积最大。

题解:

枚举删除哪条边,然后再跑树形dp就好了。

感觉可以O(n),但是这个复杂度我就懒得去想了

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 206;
vector<int> E[maxn];
int n,s;
int dfs(int x,int pre)
{
int r1=0,r2=0,t=0;
for(int i=0;i<E[x].size();i++)
{
int v=E[x][i];
if(v==pre)continue;
t=max(dfs(v,x),t);
if(s>r1)r2=r1,r1=s;
else r2=max(s,r2);
}
t=max(t,r1+r2);
s=r1+1;
return t;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
E[x].push_back(y);
E[y].push_back(x);
}
int ans = 0;
for(int i=1;i<=n;i++)
{
for(int j=0;j<E[i].size();j++)
{
int a = dfs(E[i][j],i);
int b = dfs(i,E[i][j]);
ans=max(a*b,ans);
}
}
cout<<ans<<endl;
}

Codeforces Beta Round #14 (Div. 2) D. Two Paths 树形dp的更多相关文章

  1. Codeforces Beta Round #14 (Div. 2) D. Two Paths 树的直径

    题目链接: http://codeforces.com/contest/14/problem/D D. Two Paths time limit per test2 secondsmemory lim ...

  2. TTTTTTTTTTTTT 树的直径 Codeforces Beta Round #14 (Div. 2) D. Two Paths

    tiyi:给你n个节点和n-1条边(无环),求在这个图中找到 两条路径,两路径不相交,求能找的两条路径的长度的乘积最大值: #include <iostream> #include < ...

  3. Codeforces Beta Round #14 (Div. 2)

    Codeforces Beta Round #14 (Div. 2) http://codeforces.com/contest/14 A 找最大最小的行列值即可 #include<bits/s ...

  4. Codeforces Beta Round #14 (Div. 2) C. Four Segments 水题

    C. Four Segments 题目连接: http://codeforces.com/contest/14/problem/C Description Several months later A ...

  5. Codeforces Beta Round #14 (Div. 2) B. Young Photographer 水题

    B. Young Photographer 题目连接: http://codeforces.com/contest/14/problem/B Description Among other thing ...

  6. Codeforces Beta Round #14 (Div. 2) A. Letter 水题

    A. Letter 题目连接: http://www.codeforces.com/contest/14/problem/A Description A boy Bob likes to draw. ...

  7. Codeforces Beta Round #14 (Div. 2) Two Paths (树形DP)

    Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input outp ...

  8. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  9. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

随机推荐

  1. IntelliJ IDEA 破解 - pycharm

    MAC激活方法 下载破解文件 下载地址: https://files.cnblogs.com/files/resn/JetbrainsCrack-2.7-release-str.jar.zip 或者去 ...

  2. html5 canvas简单的直线路径

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. JavaScript继承详解(一)

    面向对象与基于对象 几乎每个开发人员都有面向对象语言(比如C++.C#.Java)的开发经验. 在传统面向对象的语言中,有两个非常重要的概念 - 类和实例. 类定义了一类事物公共的行为和方法:而实例则 ...

  4. 20155310 2016-2017-2 《Java程序设计》第五周学习总结

    20155310 2016-2017-2 <Java程序设计>第五周学习总结 教材学习内容总结 •收集对象的行为,像是新增对象的add()方法.移除对象的remove()方法等,都是定义在 ...

  5. Android改进版CoverFlow效果控件

    最近研究了一下如何在Android上实现CoverFlow效果的控件,其实早在2010年,就有Neil Davies开发并开源出了这个控件,Neil大神的这篇博客地址http://www.inter- ...

  6. 1、Saltstack简介及安装配置

    1.Saltstack简介 Saltstack是基于Python开发的一套C/S架构,具备Puppet.Ansible功能于一身的配置管理工具,功能十分强大,各模块融合度及复用性极高:使用号称世界上最 ...

  7. 有用的linux命令集锦

    http://blog.csdn.net/hackerain/article/details/7946193 1. 查看内存使用情况 free -m 2. 查看系统版本信息 sudo lsb_rele ...

  8. 如何新建Quartus工程—FPGA入门教程【钛白Logic】

    这一章我们来实现第一个FPGA工程—LED流水灯.我们将通过流水灯例程向大家介绍一次完整的FPGA开发流程,从新建工程,代码设计,综合实现,管脚约束,下载FPGA程序.掌握本章内容,大家就算正式的开始 ...

  9. Workflow规则收藏

    豆瓣电影  查看电影评分等详细信息 查看图片EXIF 图铃机器人 快递查询 翻译 手机号码归属地 音乐视频下载 获取附近的免费WIFI

  10. USB-HID鼠标、键盘通讯格式【转】

    转自:https://blog.csdn.net/techhome803/article/details/9928873 转自:http://www.amobbs.com/forum.php?mod= ...