首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【CodeForces】14D - Two Paths(树的直径)

【CodeForces】14D - Two Paths(树的直径)

作者头像
FishWang
发布2025-08-26 20:38:46
发布2025-08-26 20:38:46
13800
代码可运行
举报
运行总次数:0
代码可运行

点击打开题目

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

代码语言:javascript
代码运行次数:0
运行
复制
4
1 2
2 3
3 4

output

代码语言:javascript
代码运行次数:0
运行
复制
1

input

代码语言:javascript
代码运行次数:0
运行
复制
7
1 2
1 3
1 4
1 5
1 6
1 7

output

代码语言:javascript
代码运行次数:0
运行
复制
0

input

代码语言:javascript
代码运行次数:0
运行
复制
6
1 2
2 3
2 4
5 4
6 4

output

代码语言:javascript
代码运行次数:0
运行
复制
4

题意:去掉一条边把图分成两部分,分别求两部分树的直径,相乘的值取最大。

题解:枚举每一条边即可。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define MAX 200
int n;
bool mapp[MAX+11][MAX+11];
int f[MAX+11];
bool vis[MAX+11];
int dfs(int x,int y)
{
	queue<int> q;
	CLR(f,0);
	CLR(vis,false);
	f[x] = 0;
	q.push(x);
	vis[x] = true;
	int maxx = 0;
	int pos = x;
	while (!q.empty())
	{
		int t = q.front();
		q.pop();
		for (int i = 1 ; i <= n ; i++)
		{
			if (i != y && !vis[i] && mapp[t][i])
			{
				q.push(i);
				f[i] = f[t] + 1;
				vis[i] = true;
				if (f[i] > maxx)
				{
					maxx = f[i];
					pos = i;
				}
			}
		}
	}
	return pos;
}
int main()
{
	CLR(mapp,false);
	scanf ("%d",&n);
	for (int i = 1 ; i < n ; i++)
	{
		int t1,t2;
		scanf ("%d %d",&t1,&t2);
		mapp[t1][t2] = mapp[t2][t1] = true;
	}
	int ans = 0;
	for (int i = 1 ; i <= n ; i++)
	{
		for (int j = i + 1 ; j <= n ; j++)
		{
			if (mapp[i][j])
			{
				int a = dfs(i,j);
				a = dfs(a,j);
				a = f[a];
				int b = dfs(j,i);
				b = dfs(b,i);
				b = f[b];
				ans = max(ans , a * b);
			}
		}
	}
	printf ("%d\n",ans);
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档