D. Two Paths
time limit per test

2 seconds

memory limit per test

64 megabytes

input

standard input

output

standard output

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.

Examples
Input

Copy
4
1 2
2 3
3 4
Output
1
Input

Copy
7
1 2
1 3
1 4
1 5
1 6
1 7
Output
0
Input

Copy
6
1 2
2 3
2 4
5 4
6 4
Output
4

一个很玄的结论:树的直径的长度一定会是某个点的最长距离Hmax与次长距离Hsec之和(似乎不是结论,是dp)
根据这个结论,可以较为简单(dfs对我来说并不简单)的求出树的直径。

附ac代码:
 1 #include<iostream>
2 #include<cstring>
3 using namespace std;
4 const int mm=233;
5 int map[mm][mm];
6 int n,ans;
7 int dep;
8 int dfs(int u,int p)
9 {
10 int max1=0,max2=0;
11 int path_max=0;
12 for(int i=1;i<=n;i++)
13 if(map[u][i]&&i!=p)
14 {
15 int z=dfs(i,u);
16 if(path_max<z)path_max=z;
17 if(max1<dep)///更新当前点的两条最长路径
18 {
19 max2=max1;max1=dep;
20 }
21 else if(max2<dep)max2=dep;
22
23 }
24 if(path_max<max1+max2)path_max=max1+max2;///最长路径等于最长两条分路径和
25 dep=max1+1;///更新上层最大深度为当前层最大深度
26 return path_max;
27 }
28
29 int main()
30 {
31 int a,b;
32 while(cin>>n)
33 { memset(map,0,sizeof(map));
34 for(int i=0;i<n-1;i++)
35 {
36 cin>>a>>b;map[a][b]=map[b][a]=1;
37 }
38 ans=0;
39 for(int i=1;i<=n;i++)
40 for(int j=1;j<=n;j++)
41 if(map[i][j])
42 {
43 int a=dfs(i,j);
44 int b=dfs(j,i);
45 if(ans<a*b)
46 ans=a*b;
47 }
48 cout<<ans<<"\n";
49 }
50 }

学习博客:http://blog.csdn.net/nealgavin/article/details/8505981

codeforces 14D(搜索+求树的直径模板)的更多相关文章

  1. Codeforces 14D Two Paths 树的直径

    题目链接:点击打开链接 题意:给定一棵树 找2条点不反复的路径,使得两路径的长度乘积最大 思路: 1.为了保证点不反复,在图中删去一条边,枚举这条删边 2.这样得到了2个树,在各自的树中找最长链.即树 ...

  2. HDU 4514 - 湫湫系列故事——设计风景线 - [并查集判无向图环][树形DP求树的直径]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...

  3. HDU-4612 Warm up,tarjan求桥缩点再求树的直径!注意重边

    Warm up 虽然网上题解这么多,感觉写下来并不是跟别人竞争访问量的,而是证明自己从前努力过,以后回头复习参考! 题意:n个点由m条无向边连接,求加一条边后桥的最少数量. 思路:如标题,tarjan ...

  4. 题解报告:poj 1985 Cow Marathon(求树的直径)

    Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...

  5. Forethought Future Cup - Elimination Round C 二分 + 交互(求树的直径)

    https://codeforces.com/contest/1146/problem/C 题意 一颗大小为n的树,每次可以询问两个集合,返回两个集合中的点的最大距离,9次询问之内得出树的直径 题解 ...

  6. poj2631 求树的直径裸题

    题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...

  7. poj1985 Cow Marathon (求树的直径)

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 3195   Accepted: 1596 Case ...

  8. hdu 4607 Park Visit 求树的直径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n) ...

  9. [USACO2004][poj1985]Cow Marathon(2次bfs求树的直径)

    http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次bfs: ...

随机推荐

  1. git创建分支并关联远程分支

    1.新建本地分支: 如图,再输入你的分支名字,然后选择从哪个远程分支拉代码,如选择master 至此本地分支创建完成. 2.关联远程分支: (1).先输入git branch -vv,看看分支与远程分 ...

  2. SAP client锁定

    今天发现一个函数可以锁定SAP CLIENT . SCCR_LOCK_CLIENT 参数是client号码. 还可以通过事物SU10批量锁定用户登陆client

  3. let关键字:加强版的var关键字

    本文首发于个人网站:let关键字:加强版的var关键字 你好,今天大叔想和你唠扯唠扯 ES6 新增的关键字 -- let.再说 let 的具体用法之前,大叔想先和你说说大叔自己对 let 的感受 -- ...

  4. Pytorch 中张量的理解

    张量是一棵树 长久以来,张量和其中维度的概念把我搞的晕头转向. 一维的张量是数组,二维的张量是矩阵,这也很有道理. 但是给一个二维张量,让我算出它每一行的和,应该用 sum(dim=0) 还是 sum ...

  5. PW6513高压40V的LDO芯片,SOT89封装

    一般说明 PW6513系列是一款高精度,高输入电压,低静态电流,高速,低具有高纹波抑制的线性稳压器.输入电压高达40V,负载电流为在电压=5V和VIN=7V时高达300mA.该设备采用BCD工艺制造. ...

  6. VGA调试心得

    以前自己调试过视频信号,无非就时钟加行场同步加数据线,如果视频信号出问题,第一看现象,第二测频率,反正出问题不是消隐信号出问题,就是时钟频率出问题.通过这种方式也调试成功过几个显示屏,然后就以为自己对 ...

  7. Jmeter5.1.1 把默认语言调整为中文

    进入安装目录:apache-jmeter-5.1.1\bin\ 找到 jmeter.properties文件 搜索" language=en ",前面带有"#" ...

  8. 控制反转 依赖注入 main函数

    通过依赖注入.服务定位实现控制反转 Go kit - Frequently asked questions https://gokit.io/faq/ Dependency Injection - W ...

  9. Spring Boot引起的“堆外内存泄漏”排查及经验总结 strace

    小结: 检索词:C++内存分配器.jvm内存模型.gdb.内存泄露 https://tech.meituan.com/2019/01/03/spring-boot-native-memory-leak ...

  10. loj1011愤怒的牛

    题目描述 原题来自:USACO 2005 Feb. Gold 农夫约翰建造了一座有 n 间牛舍的小屋,牛舍排在一条直线上,第 i 间牛舍在 x_i 的位置,但是约翰的 m 头牛对小屋很不满意,因此经常 ...