Codeforces Beta Round #14 (Div. 2) Two Paths (树形DP)
2 seconds
64 megabytes
standard input
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.
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 the maximum possible profit.
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
【题意】给你一棵树,要求找到两条链,使得乘积最大。
【分析】树形DP,对于每个节点维护两条最长链即可。
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 1e5+;;
const int M = ;
const int mod = 1e9+;
const int mo=;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
int n,s;
int x[N],y[N];
vector<int>edg[N];
int dfs(int u,int fa){
int mx1=,mx2=,ret=;
for(int v : edg[u]){
if(v==fa)continue;
ret=max(ret,dfs(v,u));
if(s>mx1)mx2=mx1,mx1=s;
else mx2=max(mx2,s);
}
ret=max(ret,mx1+mx2);
s=mx1+;
return ret;
}
int main(){
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d%d",&x[i],&y[i]);
edg[x[i]].pb(y[i]);
edg[y[i]].pb(x[i]);
}
int ans=;
rep(i,,n-){
int a=dfs(x[i],y[i]);
int b=dfs(y[i],x[i]);
ans=max(ans,a*b);
}
printf("%d\n",ans);
return ;
}
Codeforces Beta Round #14 (Div. 2) Two Paths (树形DP)的更多相关文章
- Codeforces Beta Round #14 (Div. 2)
Codeforces Beta Round #14 (Div. 2) http://codeforces.com/contest/14 A 找最大最小的行列值即可 #include<bits/s ...
- Codeforces Beta Round #14 (Div. 2) D. Two Paths 树形dp
D. Two Paths 题目连接: http://codeforces.com/contest/14/problem/D Description As you know, Bob's brother ...
- 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 Beta Round #14 (Div. 2) C. Four Segments 水题
C. Four Segments 题目连接: http://codeforces.com/contest/14/problem/C Description Several months later A ...
- Codeforces Beta Round #14 (Div. 2) B. Young Photographer 水题
B. Young Photographer 题目连接: http://codeforces.com/contest/14/problem/B Description Among other thing ...
- Codeforces Beta Round #14 (Div. 2) A. Letter 水题
A. Letter 题目连接: http://www.codeforces.com/contest/14/problem/A Description A boy Bob likes to draw. ...
- TTTTTTTTTTTTT 树的直径 Codeforces Beta Round #14 (Div. 2) D. Two Paths
tiyi:给你n个节点和n-1条边(无环),求在这个图中找到 两条路径,两路径不相交,求能找的两条路径的长度的乘积最大值: #include <iostream> #include < ...
- Codeforces Beta Round #96 (Div. 2) E. Logo Turtle dp
http://codeforces.com/contest/133/problem/E 题目就是给定一段序列,要求那个乌龟要走完整段序列,其中T就是掉头,F就是向前一步,然后开始在原点,起始方向随意, ...
- Codeforces Beta Round #96 (Div. 1) C. Logo Turtle —— DP
题目链接:http://codeforces.com/contest/132/problem/C C. Logo Turtle time limit per test 2 seconds memory ...
随机推荐
- java将文件转为UTF8工具类
package hiveTest; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File ...
- Centos7下关于memcached的安装和简单使用
在这里,由于用编译安装memcached服务端过于复杂,因此我选用依赖管理工具 yum 来实现 memcached 的服务端安装: [root@localhost /]# yum install -y ...
- 【LibreOJ】#541. 「LibreOJ NOIP Round #1」七曜圣贤
[题意]一开始车上有编号为0~a的红茶,过程中出现的红茶编号仅有[0,b),有三种操作: 1.买进编号未在车上出现过的红茶. 2.丢掉车上指定编号的红茶. 3.将最早丢出去的红茶捡回来. 每次操作后求 ...
- POJ 2456 Aggressive cows ( 二分搜索)
题目链接 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The ...
- Linux命令--hostname和uname
hostname命令 hostname命令用于显示和设置系统的主机名称.环境变量HOSTNAME也保存了当前的主机名.在使用hostname命令设置主机名后,系统并不会永久保存新的主机名,重新启动机器 ...
- win10安装提示“我们无法创建新的分区”
今日于笔记本安装win10时突然出现提示:我们无法创建新的分区.网上搜了不少建议,尝试了都无果. 由于我的笔记本是固态硬盘与机械硬盘混合,所以情况可能更加特殊. 最后成功的方法是: 1. 先将Win1 ...
- 实验室项目.md
1 嵌入式操作系统 为什么要用嵌入式操作系统 普通的单片机编程:程序(软件)--单片机硬件: 嵌入式操作系统开发:程序(软件)--操作系统--嵌入式硬件(包括单片机等); 我们平时普通所学的单片机编程 ...
- 谈谈mybatis逆向工程中的Example类
参考博客: http://openwares.net/database/mybatis_generator_example.html 一.Example类的作用:一个用于筛选复杂条件的类 二.Exam ...
- 尽量用const,enum,inline代替define
在读<Effective C++>之前,我确实不知道const,enum,inline会和define扯上什么关系,看完感觉收获很大,记录之. define: 宏定义. 在编译预处理时,对 ...
- FineReport——自定义控件实现填报提交事件和校验
在报表内部或者在引用报表的HTML页面,定义一个按钮标签,通过FR提供的方法实现提交功能. <button onclick="_g('${sessionID}').writeRepor ...