Codeforces--14D--Two Paths(树的直径)
Time Limit: 2000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
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
4
1 2
2 3
3 4
1
7
1 2
1 3
1 4
1 5
1 6
1 7
0
6
1 2
2 3
2 4
5 4
6 4
4
Sample Output
Hint
Source
#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(树的直径)的更多相关文章
- Codeforces 14D Two Paths 树的直径
题目链接:点击打开链接 题意:给定一棵树 找2条点不反复的路径,使得两路径的长度乘积最大 思路: 1.为了保证点不反复,在图中删去一条边,枚举这条删边 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 ...
- Codeforces 592D - Super M - [树的直径][DFS]
Time limit 2000 ms Memory limit 262144 kB Source Codeforces Round #328 (Div. 2) Ari the monster is n ...
- Codeforces 455C Civilization:树的直径 + 并查集【合并树后直径最小】
题目链接:http://codeforces.com/problemset/problem/455/C 题意: 给你一个森林,n个点,m条边. 然后有t个操作.共有两种操作: (1)1 x: 输出节点 ...
- codeforces GYM 100781A【树的直径】
先求出每棵树的直径,排个序,要想图的直径最小的话需要每棵树的直径中点像直径最大的树的直径中点连边,这样直径有三种情况:是直径最大的树的直径:a[tot]:是直径最大的树和直径第二大的树的半径拼起来+1 ...
- codeforces 734E(DFS,树的直径(最长路))
题目链接:http://codeforces.com/contest/734/problem/E 题意:有一棵黑白树,每次操作可以使一个同色连通块变色,问最少几次操作能使树变成全黑或全白. 思路:先进 ...
- CodeForces 14D 树的直径 Two Paths
给出一棵树,找出两条不相交即没有公共点的路径,使得两个路径的长度的乘积最大. 思路:枚举树中的边,将该边去掉,分成两棵树,分别求出这两棵树的直径,乘起来维护一个最大值即可. #include < ...
- codeforces 14D(搜索+求树的直径模板)
D. Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input o ...
- TTTTTTTTTTTTT 树的直径 Codeforces Beta Round #14 (Div. 2) D. Two Paths
tiyi:给你n个节点和n-1条边(无环),求在这个图中找到 两条路径,两路径不相交,求能找的两条路径的长度的乘积最大值: #include <iostream> #include < ...
随机推荐
- Redis系列(九)--几道面试题
这里只是一点面试题,想了解更多,可以查看本人的Redis系列:https://www.cnblogs.com/huigelaile/category/1461895.html 1.Redis和Memc ...
- Apache添加到windows服务和移除Apache的windows服务
Apache添加到windows服务和移除Apache的windows服务 Apache免安装版将其添加到Windows服务中: 打开cmd控制台,在上面输入"你的Apache安装目录\bi ...
- 微服务网关从零搭建——(三)Ocelot网关 + identity4
增加验证服务 1.创建名为AuthService 的core 空项目 2.修改startup文件 using System; using System.Collections.Generic; usi ...
- java基础学习日志--String、StringBuffer方法案例
package StringDemo; import java.util.Arrays; /* * 常用String.StringBufer类的方法 */ public class Demo1 { p ...
- java基础学习日志--异常案例
package test7; public class InvalidScroreException extends Exception { public InvalidScroreException ...
- Ztree加载完成默认选中根节点右侧生成表格
需求:页面加载完成之后,默认选中ztree的根节点,并执行其点击方法,右侧生成表格: 效果:如下图所示: 思路:在节点点击事件clickNode方法中根据节点的部门code查询这个部门下的所有员工,并 ...
- 对于 前端请求Django 后端服务出现403 Forbidden (CSRF token missing or incorrect.) 问题的解析
Django中使用ajax post向後臺傳送資料時403 Forbidden (CSRF token missing or incorrect.):的解決辦法 在Django中使用ajax post ...
- MySQL Connector/Python 接口 (一)
这里仅介绍 MySQL 官方开发的 Python 接口,参见这里: https://dev.mysql.com/doc/connector-python/en/ Chapter 1 Introduct ...
- Prüfer序列和cayley定理
参考资料: 1.matrix67 <经典证明:Prüfer编码与Cayley公式> 2.百度百科 3.Forget_forever prufer序列总结 4.维基百科 5.dirge的学习 ...
- saltstack(三) grains、pillar的使用
一,grains grains: 这个跟puppet的facter功能一样.主要负责采集客户端一些基本信息, 这个也完全可以自定义,可以在客户端自定义,然后自动汇报上来:也可以从服务器端定义然后推下去 ...