题目链接:http://codeforces.com/problemset/problem/14/D
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
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

直接暴力
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include <vector>
#include <queue> using namespace std;
#define memset(a,b) memset(a,b,sizeof(a))
#define N 250
int G[N][N],vis[N],dis[N];
int Index,Max,n; void bfs(int u)
{
Max=;
memset(vis,);
memset(dis,);
queue<int>Q;
Q.push(u);
vis[u]=;
while(!Q.empty())
{
int p;
p=Q.front();
Q.pop();
for(int i=; i<=n; i++)
{
if(G[p][i]!=)
{
if(!vis[i])
{
vis[i]=;
Q.push(i);
dis[i]=dis[p]+;
if(Max<dis[i])
{
Max=dis[i];
Index=i;
}
}
}
}
}
} int main()
{
int a[N],b[N];
while(scanf("%d",&n)!=EOF)
{
memset(G,);
memset(a,);
memset(b,);
for(int i=; i<n; i++)
{
scanf("%d %d",&a[i],&b[i]);
G[a[i]][b[i]]=G[b[i]][a[i]]=;
}
int MM=;
for(int i=; i<n; i++)
{
int x,y;
G[a[i]][b[i]]=G[b[i]][a[i]]=;
Index=;
bfs(a[i]);
bfs(Index);
x=Max; Index=;
bfs(b[i]);
bfs(Index);
y=Max;
MM=max(MM,x*y);
G[a[i]][b[i]]=G[b[i]][a[i]]=;
}
printf("%d\n",MM);
}
return ;
}

Two Paths--cf14D(树的直径)的更多相关文章

  1. Codeforces--14D--Two Paths(树的直径)

     Two Paths Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit ...

  2. cf14D Two Paths(树的直径)

    题意: N个点构成一棵树.树枝的长度都是1. 在当中找两条不相交[没有公共点]的路,使得二者长度之积最大. (2 ≤ n ≤ 200) 思路: 一开始思路好麻烦,好麻烦,好麻烦,,,,,,,而且WA, ...

  3. cf14d 树的直径,枚举删边

    #include<bits/stdc++.h> using namespace std; #define maxn 300 ]; int n,head[maxn],tot,a,b,dis[ ...

  4. Codeforces Beta Round #14 (Div. 2) D. Two Paths 树的直径

    题目链接: http://codeforces.com/contest/14/problem/D D. Two Paths time limit per test2 secondsmemory lim ...

  5. Codeforces 14D Two Paths 树的直径

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

  6. CodeForces 14D 树的直径 Two Paths

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

  7. TTTTTTTTTTTTT 树的直径 Codeforces Beta Round #14 (Div. 2) D. Two Paths

    tiyi:给你n个节点和n-1条边(无环),求在这个图中找到 两条路径,两路径不相交,求能找的两条路径的长度的乘积最大值: #include <iostream> #include < ...

  8. Gym - 100676H Capital City(边强连通分量 + 树的直径)

    H. Capital City[ Color: Black ]Bahosain has become the president of Byteland, he is doing his best t ...

  9. Park Visit(树的直径)

    传送门 Park Visit Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

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

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

随机推荐

  1. Linux服务器用iotop命令分析服务器磁盘IO情况

    Linux下的IO统计工具如iostat, nmon等大多数是只能统计到per设备的读写情况, 如果你想知道每个进程是如何使用IO的就比较麻烦.如果会systemtap, 或者blktrace这些事情 ...

  2. js Math 对象

    Math 对象方法 方法 描述 abs(x) 返回数的绝对值. acos(x) 返回数的反余弦值. asin(x) 返回数的反正弦值. atan(x) 以介于 -PI/2 与 PI/2 弧度之间的数值 ...

  3. (三)docker 的启动,重启,关闭命令

    docker启动命令,docker重启命令,docker关闭命令  启动    systemctl start docker 守护进程重启 sudo systemctl daemon-reload 重 ...

  4. dig - 发送域名查询信息包到域名服务器

    SYNOPSIS(总览) dig [@ server ] domain [Aq query-type ] [Aq query-class ] [+ Aq query-option ] [-Aq dig ...

  5. 基于VueJS的render渲染函数结合自定义组件打造一款非常强大的IView 的Table

    基于VueJS的render渲染函数结合自定义组件打造一款非常强大的IView 的Table https://segmentfault.com/a/1190000015970367

  6. iview分析

  7. gocode安装不上的解决办法

    mkdir -p $GOPATH/src/golang.org/x  //路径下创建此文件 cd $GOPATH/src/golang.org/x      //切换到此目录 git clone ht ...

  8. 手工修改注册表激活windows xp法

    手工修改注册表激活windows xp法: 1.安装原版windows xp 2.打开注册表regedit 3.找到主键 Hkey_Local_Machine\Software\Microsoft\W ...

  9. 光猫&路由器网络配置

    前期准备:电脑(工业电脑).网线.光猫.路由器 1.检查连接光猫后能否正常上网:把网线两头的水晶头,一头插在光猫上的千兆口,一头插在电脑(工业电脑)的网口上,看电脑能否正常上网: 可以正常上网:说明光 ...

  10. 线程的start和run方法的区别

    回到这个问题,可以用源码的角度去回答,这样会让面试官对有更好的印象 ------>如果直接调用run方法的话,所执行的线程是main线程.调用start方法的话,会新建一个子线程,去执行run方 ...