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

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

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

  3. Codeforces Beta Round #14 (Div. 2)

    Codeforces Beta Round #14 (Div. 2) http://codeforces.com/contest/14 A 找最大最小的行列值即可 #include<bits/s ...

  4. Codeforces Beta Round #14 (Div. 2) B. Young Photographer 水题

    B. Young Photographer 题目连接: http://codeforces.com/contest/14/problem/B Description Among other thing ...

  5. Codeforces Beta Round #14 (Div. 2) C. Four Segments 水题

    C. Four Segments 题目连接: http://codeforces.com/contest/14/problem/C Description Several months later A ...

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

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

  8. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  9. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

随机推荐

  1. 分层最短路(牛客第四场)-- free

    题意: 给你边权,起点和终点,有k次机会把某条路变为0,问你最短路是多长. 思路: 分层最短路模板题.题目有点坑(卡掉了SPFA,只能用dijkstra跑的算法). #include<iostr ...

  2. file_put_contents实现内容追加

    file_put_contents("test.txt", "This is another something.", FILE_APPEND); FILE_A ...

  3. Java小程序—录屏小程序(上半场)

    做软件的三个步骤: (1)做什么? (2)怎么做? (3)动手做! ok,我们今天要做的是一个录屏软件,那怎么做呢?首先,我们小时候都玩过一种小人书,就是当你快速翻动书页时,书中的人物就会活灵活现的动 ...

  4. Transparency Tutorial with C# - Part 3

    Download image fade demo - 4 Kb Download image fade source project- 7 Kb Download image fade images ...

  5. 百度地图 echarts tooltip属性 经纬度坐标不显示

    中国地图.散点图结合点击后显示当前城市数量 不显示经纬度坐标 echarts.appMap = function (id, opt) { // 实例 var chart = this.init(doc ...

  6. 公司SQL考核及小结(Oracle)

    一.数据库初始化脚本: Create TABLE HAND_CUSTOMERS ( CUSTOMERS_NO ), CUSTOMERS_NAME ), CUSTOMERS_GENDER ), CUST ...

  7. 【原创】大叔经验分享(54)flume kudu sink运行一段时间kudu client报错

    flume kudu sink运行一段时间报错: 19/05/05 10:15:56 WARN client.ConnectToCluster: Error receiving a response ...

  8. luogu P5366 [SNOI2017]遗失的答案

    luogu 首先gcd为\(G\),lcm为\(L\),有可能出现的数(指同时是\(G\)的因数以及是\(L\)的倍数)可以发现只有几百个.如果选出的数要能取到gcd,那么对于每种质因子,都要有一个数 ...

  9. wex5打包详解

    1.模式选择 模式一:主要针对是简单的运用,进行智能更新,也就是说即使服务器更新了,客户端也不会立即更新,不适合产品类型的APP. 模式二:服务器资源更新了,客户端也会立即更新. 模式三:调试模式. ...

  10. js动态添加行和列(table)

    function AddTableRow() { var Table = document.getElementById("NewTable"); //取得自定义的表对象 NewR ...