#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAX = 205;
int dep, n, map[MAX][MAX];
int dfs(int u, int v){
int max1, max2, path_max;
max1 = max2 = path_max = 0;
for(int i = 1;i <= n;i ++){
if(map[i][u] && i != v){
int z = dfs(i, u);
if(path_max < z) path_max = z;
if(max1 < dep){
max2 = max1;
max1 = dep;
}
else if(max2 < dep) max2 = dep;
}
}
if(path_max < max1 + max2) path_max = max2 + max1;
dep = max1 + 1;
return path_max;
} int main(int argc, char const *argv[]){
int u, v;
//freopen("in.c", "r", stdin);
while(~scanf("%d", &n)){
memset(map, 0, sizeof(map));
for(int i = 0; i < n - 1;i ++){
scanf("%d%d", &u, &v);
map[u][v] = map[v][u] = 1;
}
int ans = -1;
for(int i = 1;i <= n;i ++){
for(int j = 1;j <= n;j ++){
if(map[i][j]){
int a = dfs(i, j);
int b = dfs(j, i);
ans = max(ans, a*b);
}
}
}
printf("%d\n", ans);
}
return 0;
}

Codeforces 14D的更多相关文章

  1. Codeforces 14D Two Paths 树的直径

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

  2. CodeForces 14D 树的直径 Two Paths

    给出一棵树,找出两条不相交即没有公共点的路径,使得两个路径的长度的乘积最大. 思路:枚举树中的边,将该边去掉,分成两棵树,分别求出这两棵树的直径,乘起来维护一个最大值即可. #include < ...

  3. codeforces 14D(搜索+求树的直径模板)

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

  4. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  5. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  6. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  7. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  8. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  9. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

随机推荐

  1. Convert.ToInt32()与int.Parse()的区别 (转载)

    Convert.ToInt32()与int.Parse()的区别(1)这两个方法的最大不同是它们对null值的处理:Convert.ToInt32(null)会返回0而不会产生任何异常,但int.Pa ...

  2. js实现浏览器兼容复制功能

    经常看到这样一种效果:就是单击一个按钮,就将某个区域内的内容,复制到了剪切板中.其实这个功能实现起来也不难,核心就是用到了window子对象clipboardData的一个方法:setData()语法 ...

  3. PHP初学留神(二)

    1.===比较运算符 记得上上篇中说过===与==的问题.当时说,===还要类型相同.但到底是怎样呢?因为我们知道比较运算符是可以把两个值类型转换的.举个栗子,如果一个数字和字符串比较,则字符串会转化 ...

  4. ECSHOP订单一键发货简化订单发货流程

    第一步: 在templates/order_info.htm文件找到: {if $operable_list.confirm}       <input name="confirm&q ...

  5. ubuntu漂亮主题

    桌面看腻了?试试这 4 款漂亮的 Linux 图标主题吧 http://linux.cn/article-4332-1.html Flatabulous https://github.com/anmo ...

  6. Oracle datafile特殊字符处理

    1.发现数据库的数据文件有特殊字符: 2.尝试在sqlplus下用将tablespace offline后修改 SQL> alter tablespace WST_DATA rename dat ...

  7. 通知NSNotificationCenter

    注意:接受通知要写在 viewDidLoad 方法里面 取得系统全局的唯一广播站 NSNotificationCenter *notification = [NSNotificationCenter  ...

  8. portal、portlet、portlet容器三个概念

    什么是portal Portlet规范中是这样定义portal的: A portal is a web based application that –commonly- provides perso ...

  9. 1008: [HNOI2008]越狱

    n个人,m种信仰: 问你相邻的人信仰不同的情况有多少种? 首先第一个人有m种选择,第二个人有m-1种选择,后面所有的人都只有m-1种选择: 所以结果就是m^n-m*(m-1)^(n-1) #inclu ...

  10. Layer 1: Single Objects

    Layer 1: 单一对象 粗略的说, 在javascript中所有对象都是maps的键值对. 键值对的实体在对象中称为属性( property).属性的key经常为 string类型,而他的valu ...