#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. selenium自动化测试(1):环境搭建

    Selenium是一款优秀的WEB自动化测试工具,它功能强大,易于使用,支持多种平台.多种浏览器和多种开发语言.这里介绍使用python+selenium进行自动化测试的一些基础知识. 在Window ...

  2. 关闭一个winform窗体刷新另外一个

    例如Form1是你的主窗体,然后Form2是你的要关闭那个窗体,在Form1中SHOW FORM2的窗体那里加上一句f2.FormClosed += new FormClosedEventHandle ...

  3. 判断浏览器是否支持某个css3属性的javascript方法

    判断浏览器是否支持css3某个属性的方法: /** * 判断浏览器是否支持某一个CSS3属性 * @param {String} 属性名称 * @return {Boolean} true/false ...

  4. Mac OS X下GnuPlot的安装和配置(无法set term png等图片输出)

    今天使用gitstats分析git repo的活动信息,发现其内部使用gnuplot,结果发现无法生成png图片,进入gnuplot的shell发现无法设置png格式输出.如下 gnuplot> ...

  5. 要成为开发中最牛逼的测试,测试中最牛逼的开发。从今天起学python,写博客。--python基础学习(一)

    Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32Type & ...

  6. Python获取两个ip之间的所有ip

    int_ip = lambda x: '.'.join([str(x/(256**i)%256) for i in range(3,-1,-1)]) ip_int = lambda x:sum([25 ...

  7. Python数据库连接池实例——PooledDB

    不用连接池的MySQL连接方法 import MySQLdbconn= MySQLdb.connect(host='localhost',user='root',passwd='pwd',db='my ...

  8. TypeScript学习指南第一章--基础数据类型(Basic Types)

    基础数据类型(Basic Types) 为了搭建应用程序,我们需要使用一些基础数据类型比如:numbers,strings,structures,boolean等等. 在TypeScript中除了Ja ...

  9. Hadoop集群(第5期副刊)_JDK和SSH无密码配置

    1.Linux配置java环境变量 1.1 原文出处 地址:http://blog.csdn.net/jiedushi/article/details/6672894 1.2 解压安装jdk 在she ...

  10. DOM in Angular2

    <elementRef> import {ElementRef} from "@angular/core"; constructor(private element:  ...