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. axios请求,拦截器的使用

    1. axios 创建请求 import axios from 'axios' import {Message} from 'element-ui' import router from " ...

  2. 用camke编译python程序

    project(test) cmake_minimum_required(VERSION 3.0) find_package(OpenCV REQUIRED) find_package (Python ...

  3. 【题解】互不侵犯 SCOI 2005 BZOJ 1087 插头dp

    以前没学插头dp的时候觉得这题贼难,根本不会做,学了才发现原来是一裸题. 用二进制表示以前的格子的状态,0表示没放国王,1表示放了国王. 假设当前位置为(x,y),需要记录的是(x-1,y-1)至(x ...

  4. HDU 3507斜率优化dp

    Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)To ...

  5. linux内核挂载文件系统的两种方式

    1.nfs方式(挂载的为文件夹) bootargs=root=/dev/nfs nfsroot=192.168.1.105:/mnt/rootfs/rootfs ip=192.168.1.88:192 ...

  6. 图论:最短路-Dijkstra

    Dijkstra+堆优化具有稳定的时间复杂度,在一些数据范围要求比较严格(准确来说是图比较苛刻)的时候能够保证稳定的时间复杂度 但是Dijkstra不能够解决负边权的问题,所以在使用的时候一定要仔细读 ...

  7. 【设计模式】 模式PK:策略模式VS桥梁模式

    1.概述 我们先来看两种模式的通用类图. 两者之间确实很相似.如果把策略模式的环境角色变更为一个抽象类加一个实现类,或者桥梁模式的抽象角色未实现,只有修正抽象化角色,想想看,这两个类图有什么地方不一样 ...

  8. Freemarker的配置与使用

    1.在pro.xml配置文件中引入架包 <!--freemarker-->  <dependency>   <groupId>freemarker</grou ...

  9. 原生js实现单屏滚动

    类似于fullpage的单屏滚动,使用原生JS实现,不依赖任何js库: css: html,body {height:100%;} body {margin:0px;} div {height:100 ...

  10. bzoj3043 IncDec Sequence

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3043 [题解] 比较神奇的一道题,开始没往差分的角度上想,所以没想出来. 考虑查分数组,有$ ...