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
4
1 2
2 3
3 4
Output
1
Input
7
1 2
1 3
1 4
1 5
1 6
1 7
Output
0
Input
6
1 2
2 3
2 4
5 4
6 4
Output
4
我承认一开始被这个分数和她的标签吓着了。。
后来发现n最大才二百的时候,直接枚举每条边,删掉这条边之后形成两棵树。求这两棵树的直径,更新答案。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=100+100;
vector<int> p[maxx];
int dp[maxx][2];
int ans;
int n;inline int dfs(int u,int f)
{int sum=0;int max1=0,max2=0;for(int i=0;i<p[u].size();i++){if(p[u][i]!=f){sum=max(sum,dfs(p[u][i],u));if(ans>max1){max2=max1;max1=ans;}else max2=max(max2,ans);}}sum=max(sum,max1+max2);ans=max1+1;return sum;
}
int main()
{int x,y;scanf("%d",&n);for(int i=1;i<n;i++){scanf("%d%d",&x,&y);p[x].push_back(y);p[y].push_back(x);}int ans=0;int _max=0;for(int i=1;i<=n;i++){for(int j=0;j<p[i].size();j++){ans=0;int _max1=dfs(i,p[i][j]);int _max2=dfs(p[i][j],i);_max=max(_max,_max1*_max2);}}cout<<_max<<endl;
}

努力加油a啊,(o)/~

Two Paths CodeForces - 14D(暴力+树的直径)相关推荐

  1. Codeforces 337D Book of Evil:树的直径【结论】

    题目链接:http://codeforces.com/problemset/problem/337/D 题意: 给你一棵树,n个节点. 如果一个节点处放着"罪恶之书",那么它会影响 ...

  2. Book of Evil(树的直径+思维)

    Book of Evil Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area ...

  3. 0x63.图论 - 树的直径与最近公共祖先

    目录 一.树的直径(Diameter) 1.树形DP求树的直径 2.两次BFS/DFS求树的直径 1.POJ 1985.Cow Marathon(DFS求树的直径模板题) 2.AcWing 350. ...

  4. hdu 4607 Park Visit 求树的直径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 Claire and her little friend, ykwd, are travelli ...

  5. 51 nod 1427 文明 (并查集 + 树的直径)

    1427 文明 题目来源: CodeForces 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 安德鲁在玩一个叫"文明"的游戏.大妈正在 ...

  6. 历届试题 大臣的旅费(深搜 树的直径)

    问题描述 很久以前,T王国空前繁荣.为了更好地管理国家,王国修建了大量的快速路,用于连接首都和王国内的各大城市. 为节省经费,T国的大臣们经过思考,制定了一套优秀的修建方案,使得任何一个大城市都能从首 ...

  7. nssl1438-战略威慑【枚举,树的直径】

    正题 题目大意 nnn个点的无根树,求两条不相交的路径使它们长度之积最大. 解题思路 我们暴力枚举第一条,然后求树的直径即可. codecodecode #include<cstdio> ...

  8. 【洛谷 P3304】[SDOI2013]直径(树的直径)

    题目链接 题意,求一棵树被所有直径经过的边的条数. 这题是我们8.25KS图论的最后一题,当时我果断打了暴力求所有直径然后树上差分统计的方法,好像有点小问题,boom0了. 考完改这题,改了好久,各种 ...

  9. 树的直径/重心 学习笔记

    树的直径 POJ2631 Roads in the North 题意:裸的直径 题解:套模板,但是开始的时候,我的代码在第一次dfs循环的时候,没有考虑到,路长全为0的情况,而用来记录最远点的maxp ...

最新文章

  1. python盘点订单_django解决订单并发问题【推荐】
  2. 王者荣耀装备测试软件,王者荣耀首度公开5v5 PVP自动化测试方案
  3. 柱状图、堆叠柱状图、瀑布图有什么区别?怎样用Python绘制?(附代码)
  4. sharepoint 2010 beta Workflow
  5. python怎么安装打开文件不存在怎么办呢_如果文件不存在,Python中的open()不会创建文件...
  6. dedecms后台左侧菜单空白不显示怎么处理
  7. Kafka学习笔记(3)----Kafka的数据复制(Replica)与Failover
  8. 从GAN到WGAN到LipschitzGA
  9. c json保存整型数组,您如何存储“ int”? NSMutableArray *或NSMutableDictionary *中的值?整数形式的JSON数据的长期问题。...
  10. python四大高阶函数_四大高阶函数
  11. ffmpeg解码H264文件并实时播放
  12. 新闻:西部数据拟200亿美元收购原东芝存储 《创新者的窘境》硬盘发展史
  13. 测试苹果电脑性能软件xbench在哪,Mac OS操作系统性能对比测试
  14. 小学计算机无生试讲教案,小学英语无生试讲
  15. 【音频处理】Melodyne 简介 ( Melodyne 音频处理注意事项 | 在音乐宿主软件中加载 Melodyne 插件 )
  16. edge里bing必应搜索不了解决办法
  17. IOS 七种手势详解(动图+Demo下载)
  18. ccks2020中文短文本实体链接任务测评论文--小米团队--第一名
  19. Java 内部类 面试“变态题”
  20. Mac安装mtr与brow安装

热门文章

  1. uni-app/微信小程序:验证手机号 身份证 邮箱(正则表达式)
  2. 程序员基本功04JAVA的内存回收
  3. 笔记本电脑清洁小记(附螺丝滑丝处理,常见意外情况)
  4. 川大计算机高考分数,2020年四川大学录取分数是多少 2020年高考多少分可以报考四川大学...
  5. 如何改善mysql数据装载操作效率的方法_详述如何提高MySQL中数据装载效率
  6. Android开发之Android性能优化记录
  7. php mysql 绑定变量,在MYSQL中,怎么用PDO绑定变量的方式插入数据?
  8. iOS7下隐藏status bar的详细研究
  9. linux系统原理是什么意思,Linux系统原理之整体概述
  10. linux - python