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? 如果有的话,输出字典序最小的路径 ...
随机推荐
- BZOJ5343 & 洛谷4602 & LOJ2555:[CTSC2018]混合果汁——题解
https://www.luogu.org/problemnew/show/P4602 https://loj.ac/problem/2555 https://www.lydsy.com/JudgeO ...
- cf 460 E. Congruence Equation 数学题
cf 460 E. Congruence Equation 数学题 题意: 给出一个x 计算<=x的满足下列的条件正整数n的个数 \(p是素数,2 ≤ p ≤ 10^{6} + 3, 1 ≤ a ...
- Change the IPTables log file
http://www.networkinghowtos.com/howto/change-the-iptables-log-file/ An important aspect of any f ...
- caffe中的Accuracy+softmaxWithLoss
转:http://blog.csdn.net/tina_ttl/article/details/51556984 今天才偶然发现,caffe在计算Accuravy时,利用的是最后一个全链接层的输出(不 ...
- mysql的select的五子句
转: http://www.cnblogs.com/billyu/p/5033167.html http://www.cnblogs.com/xiadong90-2015/p/4222965.html ...
- ASP.NET基础学习(暴力破解密码)
首先写出一段登陆程序: //ashx端 <%@ WebHandler Language="C#" Class="AddCalation" %> us ...
- HDU 4529 状压dp
郑厂长系列故事——N骑士问题 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)To ...
- HDU1814 2-sat 模板
Peaceful Commission Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 【设计模式】 模式PK:代理模式VS装饰模式
1.概述 对于两个模式,首先要说的是,装饰模式就是代理模式的一个特殊应用,两者的共同点是都具有相同的接口,不同点则是代理模式着重对代理过程的控制,而装饰模式则是对类的功能进行加强或减弱,它着重类的功能 ...
- mysql 并发测试
针对上一节做一些针对公司业务的测试. 我们来做一些压力测试. 服务器配置: 操作系统: centos 5.6-64 CPU: 8核 内存: 8G 硬盘:sas 文件系统:linux MySQL:5.6 ...