Codeforces Beta Round #14 (Div. 2) D. Two Paths 树的直径
题目链接:
http://codeforces.com/contest/14/problem/D
D. Two Paths
time limit per test2 secondsmemory limit per test64 megabytes
#### 问题描述
> 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.
####样例输入
> 6
> 1 2
> 2 3
> 2 4
> 5 4
> 6 4
####样例输出
> 4
题意
给你一颗树,找两条不相交不重叠的路径,使得它们的乘积最大。
题解
枚举删哪条边,然后分别跑树的直径
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=222;
struct Edge{
int u,v,ne,flag;
Edge(int u,int v,int ne):u(u),v(v),ne(ne),flag(0){}
Edge(){}
}egs[maxn*2];
int n;
int head[maxn],tot;
void addEdge(int u,int v){
egs[tot]=Edge(u,v,head[u]);
head[u]=tot++;
}
void dfs(int u,int fa,int dep,int& ret,int& ma){
if(dep>ma){ ret=u,ma=dep; }
int p=head[u];
while(p!=-1){
Edge& e=egs[p];
if(!e.flag&&e.v!=fa){
dfs(e.v,u,dep+1,ret,ma);
}
p=e.ne;
}
}
int solve(int u){
int ret=0;
int v=-1,ma=-1;
dfs(u,-1,0,v,ma);
u=v;
v=-1,ma=-1;
dfs(u,-1,0,v,ma);
return ma;
}
void init(){
clr(head,-1);
tot=0;
}
int main() {
scf("%d",&n);
init();
rep(i,0,n-1){
int u,v;
scf("%d%d",&u,&v);
addEdge(u,v);
addEdge(v,u);
}
int ans=0;
for(int i=0;i<tot;i+=2){
egs[i].flag=egs[i^1].flag=1;
int x=solve(egs[i].u);
int y=solve(egs[i].v);
ans=max(ans,x*y);
egs[i].flag=egs[i^1].flag=0;
}
prf("%d\n",ans);
return 0;
}
//end-----------------------------------------------------------------------
Codeforces Beta Round #14 (Div. 2) D. Two Paths 树的直径的更多相关文章
- 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 ...
- TTTTTTTTTTTTT 树的直径 Codeforces Beta Round #14 (Div. 2) D. Two Paths
tiyi:给你n个节点和n-1条边(无环),求在这个图中找到 两条路径,两路径不相交,求能找的两条路径的长度的乘积最大值: #include <iostream> #include < ...
- 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) 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. ...
- Codeforces Beta Round #14 (Div. 2) Two Paths (树形DP)
Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input outp ...
- Codeforces Beta Round #79 (Div. 1 Only) B. Buses 树状数组
http://codeforces.com/contest/101/problem/B 给定一个数n,起点是0 终点是n,有m两车,每辆车是从s开去t的,我们只能从[s,s+1,s+2....t-1 ...
- Codeforces Beta Round #12 (Div 2 Only) D. Ball 树状数组查询后缀、最值
http://codeforces.com/problemset/problem/12/D 这里的BIT查询,指的是查询[1, R]或者[R, maxn]之间的最值,这样就够用了. 设三个权值分别是b ...
随机推荐
- 在AI人工智能中如何巧妙学习大数据编程,成为五十万年薪的佼佼者
编辑 ai狗年 大数据和人工智能的关系,首先要说什么是大数据.这些年来,大数据先是被神化,继而又被妖魔化,到了今天,其实谁也不知道别人所谓的大数据指的是什么.我大数据从业者,建了一个大数据资源共享群1 ...
- 《Nginx高性能Web服务器》系列分享专栏
<Nginx高性能Web服务器>系列分享专栏 [作者:Poechant] Nginx是目前最流行的基于BSD-like协议.轻量级.高性能的HTTP服务器.反向代理服务器和电子邮件(SMT ...
- PyQt5 signal and slot
PyQt5 的 signal 与 slot 有所改变,例如,先定义一个 ZeroSignal 类: class ZeroSignal(QObject): atzero = pyqtSignal(int ...
- (数据科学学习手札52)pandas中的ExcelWriter和ExcelFile
一.简介 pandas中的ExcelFile()和ExcelWriter(),是pandas中对excel表格文件进行读写相关操作非常方便快捷的类,尤其是在对含有多个sheet的excel文件进行操控 ...
- C语言实现可复用栈
一.思考 最开始写的栈,通过宏来改变元素数据类型,在同一程序中只能用于一种数据类型,想要用于多种数据类型则要复制代码并改名.那么,有没有方法不复制代码就可以用于多种数据类型? 二.基本思路 在我的经验 ...
- 【课堂实践】Myod
实验内容 编写MyOD.java 用java MyOD XXX实现Linux下od -tx -tc XXX的功能 实验代码 od.java 截图 遇到的问题及解决办法 一开始想的方向是将得出的功能结果 ...
- 20155321 2016-2017-2 《Java程序设计》第十周学习总结
20155321 2016-2017-2 <Java程序设计>第十周学习总结 教材学习内容总结 网络概览 局域网和广域网:局域网通常限定在一个有效的地理区域之内,广域网由许多局域网组成.最 ...
- 20155333 2016-2017-2 《Java程序设计》第一周学习总结
<java程序设计>第一周学习总结 学习目标 •了解java基础知识 •了解JVM.JRE与JDK,并下载.安装.测试JDK •了解PATH.CLASSPATH.SOURCEPATH的作用 ...
- OpenStack入门篇(五)之KVM性能优化及IO缓存介绍
1.KVM的性能优化,介绍CPU,内存,IO性能优化 KVM CPU-->qemu进行模拟ring 3-->用户应用 (用户态,用户空间)ring 0-->操作系统 (内核态,内核空 ...
- HBase第一章 安装 HMaster 主备
1.集群环境 Hadoop HA 集群规划 hadoop1 cluster1 nameNode HMaster hadoop2 cluster1 nameNodeStandby ZooKeeper ...