Two Paths

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

Submit
Status

Description

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.

Sample Input

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

Sample Output

Hint

Source


n个点,n-1条无向边求这个图中最长的两条不相交的链长度,因为这里只有n-1条边,所以这一定不会存在环,所以我们可以枚举每一条边,用这条边把图分成两部分,求各自部分的树的直径,然后乘积,dfs的时候记录最长和次长的树链长度
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int>G[1010];
int ans,n;
int dfs(int v,int x)
{
int sum=0;
int max1=0,max2=0;
for(int i=0;i<G[v].size();i++)
{
if(G[v][i]!=x)
{
sum=max(sum,dfs(G[v][i],v));
if(ans>max1)
{
max2=max1;
max1=ans;
}
else
max2=ans>max2?ans:max2;
}
}
sum=max(sum,max1+max2);
ans=max1+1;
return sum;
}
int main()
{
while(cin>>n)
{
for(int i=1;i<=n;i++)
G[i].clear();
int x,y;
for(int i=0;i<n-1;i++)
{
ans=0;
cin>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
int maxx=0;
for(int i=1;i<=n;i++)
{
for(int j=0;j<G[i].size();j++)
{
ans=0;
x=dfs(G[i][j],i);
y=dfs(i,G[i][j]);
maxx=max(maxx,x*y);
}
}
cout<<maxx<<endl;
}
return 0;
}

Codeforces--14D--Two Paths(树的直径)的更多相关文章

  1. Codeforces 14D Two Paths 树的直径

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

  2. 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 ...

  3. Codeforces 592D - Super M - [树的直径][DFS]

    Time limit 2000 ms Memory limit 262144 kB Source Codeforces Round #328 (Div. 2) Ari the monster is n ...

  4. Codeforces 455C Civilization:树的直径 + 并查集【合并树后直径最小】

    题目链接:http://codeforces.com/problemset/problem/455/C 题意: 给你一个森林,n个点,m条边. 然后有t个操作.共有两种操作: (1)1 x: 输出节点 ...

  5. codeforces GYM 100781A【树的直径】

    先求出每棵树的直径,排个序,要想图的直径最小的话需要每棵树的直径中点像直径最大的树的直径中点连边,这样直径有三种情况:是直径最大的树的直径:a[tot]:是直径最大的树和直径第二大的树的半径拼起来+1 ...

  6. codeforces 734E(DFS,树的直径(最长路))

    题目链接:http://codeforces.com/contest/734/problem/E 题意:有一棵黑白树,每次操作可以使一个同色连通块变色,问最少几次操作能使树变成全黑或全白. 思路:先进 ...

  7. CodeForces 14D 树的直径 Two Paths

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

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

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

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

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

随机推荐

  1. JDBC配置MSSQL

    使用JDBC连接SQL SERVER 这可能是个很老套的话题,但不管怎么说还是有用的.姑且把配置方法贴出来吧.1. 确认Sql Server的的运行状态打开Sql Server配置管理器,确认Sql ...

  2. jdbcTemplate传参使用Map或List

    List传参方式 举个例子 sql = "select * from table where id=? and param=?": sql中的参数要用?形式,然后使用list.ad ...

  3. 洛谷——P1404 平均数

    P1404 平均数 题目描述 给一个长度为n的数列,我们需要找出该数列的一个子串,使得子串平均数最大化,并且子串长度>=m. 前缀和+二分答案 #include<iostream> ...

  4. Gym - 101670B Pond Cascade(CTU Open Contest 2017 贪心,二分)

    题目: The cascade of water slides has been installed in the park recently and it has to be tested. The ...

  5. 洛谷 P1280 尼克的任务 (线性DP)

    题意概括 线性资源分配的问题,因为空闲的时间大小看后面的时间(反正感觉这个就是个套路)所以从后往前DP. 转移方程 如果当前时刻没有工作 f[i]=f[i+1]+1 如果当前时刻有工作 f[i]=ma ...

  6. C++关键字:explicit

    #include "pch.h" #include <iostream> using namespace std; class BaseClass { public: ...

  7. 洛谷 2173 BZOJ 2816 [ZJOI2012]网络

    [题解] 明显的LCT模板题,c种颜色就开c棵LCT好了.. #include<cstdio> #include<algorithm> #define N 100010 #de ...

  8. 【OpenCV, MFC, DIP】向图像中加入各种噪声

    1.椒盐噪声 Mat dstImage = srcImage.clone(); ; k < n; k++) { //随机取值行列 int i = rand() % dstImage.rows; ...

  9. 50. Spring Boot日志升级篇—log4j【从零开始学Spring Boot】

    如果你使用的是spring boot 1.4.0版本的话,那么你可能需要配合以下文章进行学习 90.Spring Boot 1.4 使用log4j错误[从零开始学Spring Boot] Log4j是 ...

  10. nyoj_600_花儿朵朵_201404162000

    花儿朵朵 时间限制:1000 ms  |  内存限制:65535 KB 难度:5   描述 春天到了,花儿朵朵盛开,hrdv是一座大花园的主人,在他的花园里种着许多种鲜花,每当这个时候,就会有一大群游 ...