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 < ...
随机推荐
- 梦想MxWeb3D,三维CAD协同设计平台 2019.05.05更新
SDK开发包下载地址: http://www.mxdraw.com/ndetail_20140.html 在线演示网址: http://www.mxdraw.com:3000/ 1. 增加CAD绘图 ...
- java_线程创建的两种方法
线程创建的方法有两种: 一 继承Thread类: public class ThreadTest { public static void main(String[] args) { //4)在mai ...
- js一般用法
<1> 通过id查找html元素 var x=document.getElementById("intro"); <2> 通过标签名查找html元素 v ...
- 解决docker容器启动时候无法映射端口的问题
当我们停止防火墙后,docker容器启动映射端口可能无法映射端口,这个时候需要重建docker0网桥. 详细的错误是这样的: docker: Error response from daemon: d ...
- JAVA基础——集合类汇总
一.集合与数组 数组(可以存储基本数据类型)是用来存现对象的一种容器,但是数组的长度固定,不适合在对象数量未知的情况下使用. 集合(只能存储对象,对象类型可以不一样)的长度可变,可在多数情况下使用. ...
- 打造完美的ImageLoader——LruCache+DiskLruCache
做android应用少不了要和网络打交道,在我刚开始学android的时候总是处理不好网络图片的加载,尤其是图片乱跳的问题,后来发现了各种缓存图片的方法:本地缓存.软引用.LruCache.... 我 ...
- 11servlet接口
11.servlet接口-2018/07/23 1.servlet 是一个接口,需要导包javax.servlet.Servlet; 第一种编写一个servlet程序的方法 写一个Java类,实现se ...
- 脚本开头,python预编译,控制台输入输出,for,while循环,分支判断,break,continue
3. name = input("name:")与2.x中raw_input一回事, 注意接收的变量全部默认为字符串类型. 从控制台接收用户输入,而密文输入import getpa ...
- lastpass密码管理工具使用教程
现在移动互联网发展异常空气,无论访问哪个平台或者网站必须要注册账号,日子久了就会发现最痛苦的就是记住这些网站的密码.因为我们不可能将所有的网站都是设置同样的的账号密码,因为国内网站用户数据库被泄露的事 ...
- 数据分布vs聚类-数据预处理技巧-对数变换
对于原始数据分布倾斜 利用统计或数学变换来减轻数据分布倾斜的影响.使原本密集的区间的值尽可能的分散, 原本分散的区间的值尽量的聚合. Log变换通常用来创建单调的数据变换.它的主要作用在于帮助稳定方差 ...