SPOJ Two Paths
Description
给定一个无向图,含有一定的路。从中找出两个最长的路径(每条路径有一些相通路组成)这两个路径不能经过公共的点,求何时二路径的乘积最大。
本题给出的无向图是一棵树,每边权值为1.
原文翻译应为有n个点,n-1条边,两点之间能够相互到达。
Solution
- 直径分成两部分得到的两条路径
- 直径的一部分和另一部分里的最长链
Code
#include <vector>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
const int N = 100005;
namespace { // 此处应该替换为Class Solution
int n, dep[N], fa[N], MxDepP;
int dis[N], cnt, d[N], ind[N];
int L[N], R[N];
std:: vector<int> e[N];
void dfs1(int u, int fath) {
dep[u] = dep[fath] + 1, fa[u] = fath;
for (auto v : e[u])
if (v != fath)
dfs1(v, u);
if (dep[u] > dep[MxDepP])
MxDepP = u;
}
void dfs2(int u, int fath) {
for (int v : e[u])
if (v != fath and not ind[v])
dfs2(v, u), dis[u] = std:: max(dis[u], dis[v] + 1);
}
int GetDiameter() {
dfs1(1, 0);
dfs1(MxDepP, 0);
int end = MxDepP;
while(end)
d[++cnt] = end, ind[end] = true, end = fa[end];
}
long long CalcAnswer() {
long long Res = 0;
for (int i = 1; i <= cnt; i += 1) {
dfs2(d[i], 0);
Res = std:: max(Res, 1ll * (dis[d[i]] - 1) * (cnt - 1));
}
int tmp = 0;
for (int i = 1; i <= cnt; i += 1)
L[i] = tmp = std:: max(tmp, i - 2 + dis[d[i - 1]]);
tmp = 0;
for (int i = cnt; i >= 1; i -= 1)
R[i] = tmp = std:: max(tmp, cnt - i + dis[d[i + 1]] - 1);
for (int i = 1; i <= n; i += 1)
Res = std:: max(Res, 1ll * L[i] * R[i - 1]);
return Res;
}
};
int main () {
scanf("%d", &n);
for (int i = 1; i < n; i += 1) {
int u, v;
scanf("%d%d", &u, &v);
e[u].push_back(v), e[v].push_back(u);
}
GetDiameter();
printf("%lld\n", CalcAnswer());
return 0;
}
SPOJ Two Paths的更多相关文章
- SPOJ TWOPATHS Two Paths
题目意思:给一棵树,找到俩个不相交的通路,使得这俩个通路的长度和乘机最大: 解法: 小哥一看呵呵 这不就是枚举点 然后求俩边的树的直径在相乘求个最大值的题么! 呵呵 这个N 有100000 当时就不玩 ...
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [LeetCode] Unique Paths 不同的路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
- leetcode : Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- SPOJ GSS3 Can you answer these queries III[线段树]
SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
随机推荐
- 【基础】ASP.net MVC 文件下载的几种方法(欢迎讨论)
在ASP.net MVC 中有几种下载文件的方法 前提:要下载的文件必须是在服务器目录中的,至于不在web项目server目录中的文件下载我不知道,但是还挺想了解的. 第一种:最简单的超链接方法,&l ...
- Django CRM系统
本节内容 业务痛点分析 项目需求讨论 使用场景分析 表结构设计 业务痛点分析 我2013年刚加入老男孩教育的时候,学校就一间教室,2个招生老师,招了学生后,招生老师就在自己的excel表里记录一下,每 ...
- JavaScript是如何实现继承的
JavaScript中的function是万能的,除了用于的函数定义,也可以用于类的定义.JavaScript的继承,说起来也是有点怪,没有public,private等访问控制修饰,也没有imple ...
- phalcon安装
参考网站:https://docs.phalconphp.com/zh/latest/reference/tools.html (中文版)cento6.5环境安装:cd ~mkdir phalconc ...
- selenium - webdriver - 设置元素等待
隐式等待:implicitly_wait(value), value默认是0 from selenium import webdriverfrom selenium.common.exceptions ...
- To pack or not to pack – MyISAM Key compression
MyISAM storage engine has key compression which makes its indexes much smaller, allowing better fit ...
- 【链表】在O(1)的时间删除链表的节点
/** * 在O(1)的时间删除链表的节点 * * @author * */ public class Solution { public static void deleteNode(Node he ...
- [技巧篇]04.使用PowerDesigner逆向生成
- Java设计模式の装饰者模式
目录 一.问题引入 二.设计原则 三.用装饰者模式解决问题 四.装饰者模式的特点 五.装饰者模式的定义 六.装饰者模式的实现 七.java.io包内的装饰者模式 一.问题引入 咖啡店的类设计: 一个饮 ...
- JavaMail实现邮件的发送
1,拷贝mail.jar 和activation.jar到项目中 2,开启邮箱的 POP3/SMTP服务,以QQ邮箱为例 进去QQ邮箱-->设置-->账号-->进行设置如下图 注意: ...