TTTTTTTTTTTTT 树的直径 Codeforces Beta Round #14 (Div. 2) D. Two Paths
tiyi:给你n个节点和n-1条边(无环),求在这个图中找到 两条路径,两路径不相交,求能找的两条路径的长度的乘积最大值;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const double pi=acos(-1);
const int mod=100000000;
ll max(ll a,ll b)
{return a>b?a:b;};
int min(int a,int b)
{return a<b?a:b;}; struct Edge{
int u,v;
}e[205]; vector<int> G[205];
int vis[205],step[205],s,len; int bfs(int a,int pre)
{
int s=a;
len=0;
MM(vis,0);
MM(step,0);
queue<int> q;
q.push(a);
vis[pre]=vis[a]=1;
while(q.size())
{
int u=q.front();q.pop();
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(vis[v]) continue;
q.push(v);
vis[v]=1;
step[v]=max(step[v],step[u]+1);
if(step[v]>len)
{
len=step[v];
s=v;
}
}
}
return s;
}//求树的直径len模板
int main()
{
int n,u,v;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++) G[i].clear();
for(int i=1;i<n;i++)
{
scanf("%d %d",&e[i].u,&e[i].v);
G[e[i].u].push_back(e[i].v);
G[e[i].v].push_back(e[i].u);
} int ans=0;
for(int i=1;i<n;i++)
{
int u=e[i].u,v=e[i].v;
bfs(bfs(u,v),v);
int x=len;
bfs(bfs(v,u),u);
x*=len;
ans=max(ans,x);
}
printf("%d\n",ans);
}
return 0;
}
分析:枚举每条边,将图拆成两棵树(保证没有交点),分别求得两棵树的直径,乘积就是此时的最大值
树的直径资料:链接
TTTTTTTTTTTTT 树的直径 Codeforces Beta Round #14 (Div. 2) D. Two Paths的更多相关文章
- Codeforces Beta Round #14 (Div. 2) D. Two Paths 树的直径
题目链接: http://codeforces.com/contest/14/problem/D D. Two Paths time limit per test2 secondsmemory lim ...
- Codeforces Beta Round #14 (Div. 2) D. Two Paths 树形dp
D. Two Paths 题目连接: http://codeforces.com/contest/14/problem/D Description As you know, Bob's brother ...
- Codeforces Beta Round #14 (Div. 2)
Codeforces Beta Round #14 (Div. 2) http://codeforces.com/contest/14 A 找最大最小的行列值即可 #include<bits/s ...
- Codeforces Beta Round #14 (Div. 2) B. Young Photographer 水题
B. Young Photographer 题目连接: http://codeforces.com/contest/14/problem/B Description Among other thing ...
- Codeforces Beta Round #14 (Div. 2) C. Four Segments 水题
C. Four Segments 题目连接: http://codeforces.com/contest/14/problem/C Description Several months later A ...
- Codeforces Beta Round #14 (Div. 2) A. Letter 水题
A. Letter 题目连接: http://www.codeforces.com/contest/14/problem/A Description A boy Bob likes to draw. ...
- Codeforces Beta Round #14 (Div. 2) Two Paths (树形DP)
Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input outp ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
随机推荐
- leetcode中等题
# Title Solution Acceptance Difficulty Frequency 1 Two Sum 44.5% Easy 2 Add Two Number ...
- 刚接触SkyLine的一点小收获与感触
因为刚接触Skyline不到一个星期,也怕把学习到的忘记掉,所以写一点学习到的一些皮毛的东西,赶紧记录一下,怕回头忘记 1.网上关于web端的开发非常多,也有很多牛人分享自己的经验,所以学习起来也相对 ...
- 不基于比较的排序算法:Counting-sort和Radix-sort
- Halide安装指南release版本
Halide安装指南 本版本针对Halide release版本 by jourluohua 使用的是Ubuntu 16.04 64位系统默认Gcc 4.6 由于halide中需要C++ 11中的特性 ...
- 无线传输模块HC-12
无线传输模块HC-12使用 因为实验室的无人机需要使用一款无线传输模块进行遥控控制,我们讨论的中测试了HC-12,并对HC-12传输距离进行了简单测试.在此做下使用记录. 模块概述 HC-12 无线串 ...
- centos 7 SVN安装脚本搭建主从同步灵活切换
svn 脚本下载 http://opensource.wandisco.com/subversion_installer_1.9.sh 2019-Aug-20 12:20:4810.1Kapplica ...
- 【有钱的大佬看过来】Java开发学习大纲
Java开发学习大纲文档V7.0 有钱的大佬可以买下这个版权,全网最完整最详细了,没钱的大佬可以按照自己的方式去整理.有需要的私聊作者QQ:253173641 来源于-幸福的沉淀:https://ww ...
- ERA-interim
数据介绍整理中... https://software.ecmwf.int/wiki/display/UER/Time-integrated+surface+solar+radiation+downw ...
- 能ping通某网页,但无法访问网页的处理
cmd----->netsh winsock reset 用以重置Winsock目录,是网络环境初始化,解决一些奇怪的问题
- shell 实用脚本
功能 将当前目录下文件拷贝至另一目录下,且拷贝前先备份 #!/bin/sh #脚本功能 #覆盖文件前先备份 cfsuffix=$(date +%Y%m%d); #备份文件后缀 ]; then #输入参 ...