codeforces 14D(搜索+求树的直径模板)
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
一个很玄的结论:树的直径的长度一定会是某个点的最长距离Hmax与次长距离Hsec之和(似乎不是结论,是dp)
根据这个结论,可以较为简单(dfs对我来说并不简单)的求出树的直径。
附ac代码:
1 #include<iostream>
2 #include<cstring>
3 using namespace std;
4 const int mm=233;
5 int map[mm][mm];
6 int n,ans;
7 int dep;
8 int dfs(int u,int p)
9 {
10 int max1=0,max2=0;
11 int path_max=0;
12 for(int i=1;i<=n;i++)
13 if(map[u][i]&&i!=p)
14 {
15 int z=dfs(i,u);
16 if(path_max<z)path_max=z;
17 if(max1<dep)///更新当前点的两条最长路径
18 {
19 max2=max1;max1=dep;
20 }
21 else if(max2<dep)max2=dep;
22
23 }
24 if(path_max<max1+max2)path_max=max1+max2;///最长路径等于最长两条分路径和
25 dep=max1+1;///更新上层最大深度为当前层最大深度
26 return path_max;
27 }
28
29 int main()
30 {
31 int a,b;
32 while(cin>>n)
33 { memset(map,0,sizeof(map));
34 for(int i=0;i<n-1;i++)
35 {
36 cin>>a>>b;map[a][b]=map[b][a]=1;
37 }
38 ans=0;
39 for(int i=1;i<=n;i++)
40 for(int j=1;j<=n;j++)
41 if(map[i][j])
42 {
43 int a=dfs(i,j);
44 int b=dfs(j,i);
45 if(ans<a*b)
46 ans=a*b;
47 }
48 cout<<ans<<"\n";
49 }
50 }
学习博客:http://blog.csdn.net/nealgavin/article/details/8505981
codeforces 14D(搜索+求树的直径模板)的更多相关文章
- Codeforces 14D Two Paths 树的直径
题目链接:点击打开链接 题意:给定一棵树 找2条点不反复的路径,使得两路径的长度乘积最大 思路: 1.为了保证点不反复,在图中删去一条边,枚举这条删边 2.这样得到了2个树,在各自的树中找最长链.即树 ...
- HDU 4514 - 湫湫系列故事——设计风景线 - [并查集判无向图环][树形DP求树的直径]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...
- HDU-4612 Warm up,tarjan求桥缩点再求树的直径!注意重边
Warm up 虽然网上题解这么多,感觉写下来并不是跟别人竞争访问量的,而是证明自己从前努力过,以后回头复习参考! 题意:n个点由m条无向边连接,求加一条边后桥的最少数量. 思路:如标题,tarjan ...
- 题解报告:poj 1985 Cow Marathon(求树的直径)
Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...
- Forethought Future Cup - Elimination Round C 二分 + 交互(求树的直径)
https://codeforces.com/contest/1146/problem/C 题意 一颗大小为n的树,每次可以询问两个集合,返回两个集合中的点的最大距离,9次询问之内得出树的直径 题解 ...
- poj2631 求树的直径裸题
题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...
- poj1985 Cow Marathon (求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 3195 Accepted: 1596 Case ...
- hdu 4607 Park Visit 求树的直径
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n) ...
- [USACO2004][poj1985]Cow Marathon(2次bfs求树的直径)
http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次bfs: ...
随机推荐
- SQL Server 日志收缩方法
在日常运维中,有时会遇到"The transaction log for database 'xxxx' is full due to 'ACTIVE_TRANSACTION'." ...
- 网络Devops探索与实践 流程管理分析师
https://mp.weixin.qq.com/s/OKLiDi78uB8ZkPG2kUVxvA 网络Devops探索与实践 王镇 鹅厂网事 2020-09-23 9月16日举办的2020 ODC ...
- Location和Content-Location
div.example { background-color: rgba(229, 236, 243, 1); color: rgba(0, 0, 0, 1); padding: 0.5em; mar ...
- Webpack4.0各个击破(8)tapable篇
目录 一. tapable概述 二. tapable-0.2源码解析 2.1 代码结构 2.2 事件监听方法 2.3 事件触发方法 三. tapable1.0概述 一. tapable概述 tapab ...
- LOJ10128. 花神游历各国
花神喜欢步行游历各国,顺便虐爆各地竞赛.花神有一条游览路线,它是线型的,也就是说,所有游历国家呈一条线的形状排列,花神对每个国家都有一个喜欢程度(当然花神并不一定喜欢所有国家). 每一次旅行中,花神会 ...
- Windows VS Code 配置 Java 开发环境
Windows VS Code 配置 C/C++ 开发环境 准备 Windows [这个相信大家都有 笑: )] VS Code JDK 建议 JDK8以上(不包含JDK8,关于 Windows环境下 ...
- SLAM01
上周末发现了一个巨大的问题,就是我们目前构建的室内定位的方法中,一个基本的假设是错的----这就非常尴尬了. 于是乎赶紧抱一波佛脚,学习一下slam里相关的问题是怎么解决的,找找灵感. 结果看了个开头 ...
- Centos7 安装RabbitMQ 3.6.1
如果你看过前两章对RabbitMQ已经有了一定了解,现在已经摩拳擦掌,来吧动手吧! 用什么系统 本文使用的是Centos7,为了保证对linux不太熟悉的伙伴也能轻松上手(避免折在安装的路上),下面是 ...
- TCP/IP__TCP/IP协议
1.定位:指因特网整个TCP/IP协议族,由四个层次组成:网络接入层.网络层.主机到主机层.应用层.也叫DoD模型.每一层都呼叫它的下一层所提供的网络来完成自己的需求. 2.与OSI模型的对应: 3. ...
- xLua热更新插件
一.xLua插件下载安装 1.从GitHub上搜索并下载插件 2.将文件复制到unity中 3.检查是否有错误 二.在unity中调用lua 1.简单调用 在c#脚本中使用LuaEnv类可以运行lua ...