POJ 2631 Roads in the North POJ 1985 Cow Marathon
来源:互联网 发布:人工智能ai电影主题 编辑:程序博客网 时间:2024/05/29 18:13
都是求树上(无向)任意两点的最长距离
两题都没有告诉是树,但2631可以根据任意两点之间只存在一条路径和每条路径至多经过一个点一次推出来(否则有环),1985是看了discuss才知道的
做法是先从任意一点开始dfs,找到距离最大的那个点,从这个点开始再进行一次dfs,这时的最大距离则是答案
因为第一次做dfs时,找到的距离最大的那个点,一定是最长路径上的一个端点
如果这个起始点在最长路径上,显然,否则找到的那个点的路径成为新的最长路径的一部分
如果它不在最长路径上,则从起始点开始,沿着最大距离往四周扩散,总有一个点会在最长路径上,这就回到了上面那种情况
还有一种做法是,这两个点肯定是叶子,只要从根开始,找每个节点的子树中到该节点距离最远的两个距离(到某两片叶子的距离),取较大的一对
代码:
2631
1985
- POJ 2631 Roads in the North POJ 1985 Cow Marathon
- 树的直径学习小记 Poj 1985 Cow Marathon+Poj 2631 Roads in the North
- POJ - 2631 Roads in the North
- poj 2631 Roads in the North
- POJ 2631 Roads in the North
- B - Roads in the North POJ-2631
- POJ 2631Roads in the North【裸题】
- 【POJ 2631 Roads in the North】
- 【POJ】[2631]Roads in the North
- POJ 2631 Roads in the North 笔记
- Roads in the North POJ
- POJ 2613 Roads in the North
- POJ 2631 Roads in the North(树形dp)
- POJ 2631 Roads in the North 树的直径
- 【树的直径】 POJ 2631 Roads in the North
- POJ 2631 Roads in the North(树的直径)
- poj 2631 Roads in the North(裸树的直径)
- POJ 2631 -- Roads in the North【树的直径 && 裸题】
- java操作ftp 上传下载创建文件夹(转+扩展)
- c# winform app.config xml 配置文件 读写操作
- VC虚函数的应用(关键字Virtual)
- BWY一般数据源的提取
- 排序:查找数组中逆序对的个数
- POJ 2631 Roads in the North POJ 1985 Cow Marathon
- openSUSE11.3安装ghc
- 查找:二分查找(binary search)
- skyeye + ulibc + busybox + linux kernel
- [转]submit()和onsubmit()的区别
- 管理员必备:五大Linux性能命令
- 怎么设置easyui 的 combobox的默认值
- 排序:选择排序(selection sort)
- Git分支管理