#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. 多站点FTP同步

    需求描述: 由于工作的原因,发布程序时,经常需要将一站点的脚本.程序同步到其它的终端站点中,以保证所有站点的程序是同步的,用过FlashFtp,CuteFtp,LeapFtp等客户端工具,均于自己的需 ...

  2. html5绘制折线图

    html5绘制折线图详细代码 <html> <canvas id="a_canvas" width="1000" height="7 ...

  3. CSS3的position:sticky介绍

    用户的屏幕越来越大,而页面太宽的话会不宜阅读,所以绝大部分网站的主体宽度和之前相比没有太大的变化,于是浏览器中就有越来越多的空白区域,所以你可能注意到很多网站开始在滚动的时候让一部分内容保持可见,比如 ...

  4. 大数据时代的技术hive:hive的数据类型和数据模型

    在上篇文章里,我列举了一个简单的hive操作实例,创建了一张表test,并且向这张表加载了数据,这些操作和关系数据库操作类似,我们常把hive和关系数据库进行比较,也正是因为hive很多知识点和关系数 ...

  5. 【BZOJ】1002: [FJOI2007]轮状病毒 递推+高精度

    1002: [FJOI2007]轮状病毒 Description 给定n(N<=100),编程计算有多少个不同的n轮状病毒. Input 第一行有1个正整数n. Output 将编程计算出的不同 ...

  6. 使用Memcached、Spring AOP构建数据库前端缓存框架

    数据库访问可能是很多网站的瓶颈.动不动就连接池耗尽.内存溢出等.前面已经讲到如果我们的网站是一个分布式的大型站点,那么使用 memcached实现数据库的前端缓存是个很不错的选择:但如果网站本身足够小 ...

  7. Python 知识点

    1. generator #g is a generator and g is iterable g = (x*x for x in range(5)) for n in g: print(n) # ...

  8. ubuntu中文版切换为英文后字体变化问题解决

    为了熟悉更多Linux 的英语词汇,希望把Ubuntu 的界面换成英文的.使用System->Administration->Language Suport( 系统-> 管理-> ...

  9. 将CMD内的显示内容输出到txt文件

    将CMD内的显示内容输出到txt文件 xxxx -t >c:\test.txt        //xxxx为命令  如ping www.baidu.com //-t >c:\test.tx ...

  10. UVA 11737 Extreme Primitive Society

    非常容易的一个题: 只要判断两种基因相差的最小值就行: #include<cstdio> #include<cstring> #include<algorithm> ...