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. 洛谷 P2906 [USACO08OPEN]牛的街区Cow Neighborhoods | Set+并查集

    题目: https://www.luogu.org/problemnew/show/P2906 题解: 垃圾水题 #include<cstdio> #include<algorith ...

  2. [bzoj 1594]猜数游戏

    主要是怎么处理矛盾 矛盾的条件有$2$种: 第一种是当把所有相等的$a$都全部找到后,他们并没有全联通,所以矛盾,因为没有两个是相同的 第二种是在2组$(l,r,a)$,$(l1,r1,a1)$中,$ ...

  3. MyEclipse安装FreeMarker插件

      MyEclipce8.6中安装FreeMarker插件,这绝对是最简单的方法.   步骤如下:   (一)打开http://sourceforge.net/projects/freemarker- ...

  4. HDU1531 差分约束

    King Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  5. [LeetCode] 7. Reverse Integer ☆

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321   Have y ...

  6. Beagleboneblack的MLO文件干了些啥

    Beagleboneblack在启动linux之前还有三个启动阶段: ROM code  -->  MLO  -->  u-boot --> kernel 先看看ROM code干了 ...

  7. HDU 5696 区间的价值 暴力DFS

    Problem Description 我们定义"区间的价值"为一段区间的最大值*最小值. 一个区间左端点在L,右端点在R,那么该区间的长度为(R−L+1). 现在聪明的杰西想要知 ...

  8. mysql创建用户,并授权

    1.创建用户 CREATE USER 'username'@'host' IDENTIFIED BY 'password'; host分下列3种情况:'%' - 所有情况都能访问‘localhost’ ...

  9. windows下用时间戳创建文件名

    英文环境下: echo Archive_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.zip 中文: ...

  10. 【BZOJ1560】【JSOI2009】火星藏宝图 [DP]

    火星藏宝图 Time Limit: 10 Sec  Memory Limit: 64 MB[Submit][Status][Discuss] Description Input Output Samp ...