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的更多相关文章

  1. SPOJ TWOPATHS Two Paths

    题目意思:给一棵树,找到俩个不相交的通路,使得这俩个通路的长度和乘机最大: 解法: 小哥一看呵呵 这不就是枚举点 然后求俩边的树的直径在相乘求个最大值的题么! 呵呵 这个N 有100000 当时就不玩 ...

  2. 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 ...

  3. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  4. [LeetCode] Unique Paths II 不同的路径之二

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  5. [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 ...

  6. SPOJ DQUERY D-query(主席树)

    题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...

  7. leetcode : Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  8. 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 ...

  9. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

随机推荐

  1. [辅助软件] 微信小程序开发资源汇总 接入指南

    https://github.com/justjavac/awesome-wechat-weapp https://github.com/justjavac/awesome-wechat-weapp ...

  2. 探索CAS无锁技术

    前言:关于同步,很多人都知道synchronized,Reentrantlock等加锁技术,这种方式也很好理解,是在线程访问的临界区资源上建立一个阻塞机制,需要线程等待 其它线程释放了锁,它才能运行. ...

  3. POSIX.2 正则表达式

    By francis_hao    Oct 1,2017   这里的正则表达式主要是指扩展正则,也就是egrep(grep -e)用到的正则表达式. 字符 含义 类别说明 | 分割分支,正则表达式会去 ...

  4. Codeforces Round #201 (Div. 2)C,E

    数论: C. Alice and Bob time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. C#中excel读取和写入

    1.方法一:采用OleDB读取EXCEL文件: 把EXCEL文件当做一个数据源来进行数据的读取操作,实例如下: public DataSet ExcelToDS(string Path) { stri ...

  6. 使用AAUTO语言开发的云桌面登录客户端

    AAUTO是一个国产小众语言,和lua算是近亲,官方网站 www.aau.cn. 使用aauto的优点我认为对于我来说最主要的有以下两点: 1.无需臃肿的框架类似.NET FRAMEWORK.Adob ...

  7. SpringMVC中 Session的使用情况

    在SpringMVC中,使用Session可以用通过两种方式 1.servlet-api 即HttpSession session.setAttritute(),session.getAttribut ...

  8. 可以随时拿取spring容器中Bean的工具类

    前言 在Spring帮我们管理bean后,编写一些工具类的时候需要从容器中拿到一些对象来做一些操作,比如字典缓存工具类,在没有找到字典缓存时,需要dao对象从数据库load一次,再次存入缓存中.此时需 ...

  9. 2015/8/26 Python基础(1):基本规则及赋值

    Python有如下的基本规则: #后表示注释 \n是行分隔符 \是继续上一行,将过长语句分开 :分号将两个语句连接在一行中 :冒号将代码头和体分开 代码块用缩进块的方式体现 不同缩进深度分隔不同的代码 ...

  10. C11性能之道:转移和转发

    1.move C++11中可以将左值强制转换为右值,从而避免对象的拷贝来提升性能.move将对象的状态或者所有权从一个对象转移到另一个对象,没有内存拷贝.深拷贝和move的区别如图: 从图可以看出,深 ...