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. vuex与redux,我们都一样

    vuex与redux的主要区别: redux:生成的全局数据流是通过每个组件的props逐层传递到各个子组件的,通过@connect装饰器绑定在this.props上面. vuex :生成的全局数据则 ...

  2. 迷宫自动生成以及基于DFS的自动寻路算法

    直接贴代码 #include<ctime> #include<conio.h> #include<iostream> #include<windows.h&g ...

  3. Jmeter使用基础笔记-认识Jmeter

    我在工作过程中接触Jmeter不算特别多,对Jmeter的使用也只是限于基础阶段,不过对付基本的一些需求我想足够使用了.有好几个朋友问我关于Jmeter的问题,在此我将我在工作过程中的使用心得和总结的 ...

  4. 百练4152:最佳加法表达式(dp+高精度)

    描述 给定n个1到9的数字,要求在数字之间摆放m个加号(加号两边必须有数字),使得所得到的加法表达式的值最小,并输出该值.例如,在1234中摆放1个加号,最好的摆法就是12+34,和为36 输入有不超 ...

  5. java之比较两个日期大小----https://blog.csdn.net/dongfangbaiyun/article/details/51225469

    https://blog.csdn.net/dongfangbaiyun/article/details/51225469 java之比较两个日期大小 最近又用到两个日期大小的比较,因此记录在此,方便 ...

  6. BNUOJ 1206 A Plug for UNIX

    A Plug for UNIX Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Origina ...

  7. image url to base64

    image url to base64 https://www.base64-image.de/ https://www.browserling.com/tools/image-to-base64 h ...

  8. [luoguP1586] 四方定理(DP 背包)

    传送门 相当于背包, f[i][j] 表示当前数为 i,能分解成 j 个数的平方的和的数量 那么就是统计背包装物品的数量 ——代码 #include <cmath> #include &l ...

  9. nyoj_79_拦截导弹_201403182040

    拦截导弹 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 某国为了防御敌国的导弹袭击,发展中一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到 ...

  10. Holedox Eating HDU4302 模拟

    Problem Description Holedox is a small animal which can be considered as one point. It lives in a st ...