题目链接:点击打开链接

题意:给定一棵树

找2条点不反复的路径,使得两路径的长度乘积最大

思路:

1、为了保证点不反复,在图中删去一条边,枚举这条删边

2、这样得到了2个树,在各自的树中找最长链。即树的直径,然后相乘就可以

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<set>
#include<vector>
#include<map>
#include<math.h>
#include<queue>
#include<string>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define N 220
struct Edge {
int from, to, nex;
bool hehe;
}edge[N<<1];
int head[N], edgenum;
void add(int u, int v){
Edge E={u,v,head[u],true};
edge[edgenum] = E;
head[u] = edgenum ++;
}
int n;
int dis[N];
void init(){ memset(head, -1, sizeof head); edgenum = 0; }
int bfs(int x){
memset(dis, 0, sizeof dis);
dis[x] = 1;
queue<int>q; q.push(x);
int hehe = x;
while(!q.empty()) {
int u = q.front(); q.pop();
for(int i = head[u]; ~i; i = edge[i].nex) {
if(!edge[i].hehe)continue;
int v = edge[i].to;
if(dis[v])continue;
dis[v] = dis[u]+1, q.push(v);
if(dis[hehe]<dis[v])hehe = v;
}
}
return hehe;
}
int main(){
int i, u, v;
while(~scanf("%d",&n)){
init();
for(i=1;i<n;i++){
scanf("%d %d",&u,&v);
add(u,v); add(v,u);
}
int ans = 0;
for(i = 0; i < edgenum; i+=2)
{
edge[i].hehe = edge[i^1].hehe = false;
u = edge[i].from; v = edge[i].to;
int L1 = bfs(u); int R1 = bfs(L1);
int mul1 = dis[R1] -1;
int L2 = bfs(v); int R2 = bfs(L2);
int mul2 = dis[R2] -1;
ans = max(ans, mul1 * mul2);
edge[i].hehe = edge[i^1].hehe = true;
}
printf("%d\n",ans);
}
return 0;
}

Codeforces 14D 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 592D - Super M - [树的直径][DFS]

    Time limit 2000 ms Memory limit 262144 kB Source Codeforces Round #328 (Div. 2) Ari the monster is n ...

  3. Codeforces 455C Civilization:树的直径 + 并查集【合并树后直径最小】

    题目链接:http://codeforces.com/problemset/problem/455/C 题意: 给你一个森林,n个点,m条边. 然后有t个操作.共有两种操作: (1)1 x: 输出节点 ...

  4. codeforces GYM 100781A【树的直径】

    先求出每棵树的直径,排个序,要想图的直径最小的话需要每棵树的直径中点像直径最大的树的直径中点连边,这样直径有三种情况:是直径最大的树的直径:a[tot]:是直径最大的树和直径第二大的树的半径拼起来+1 ...

  5. codeforces 734E(DFS,树的直径(最长路))

    题目链接:http://codeforces.com/contest/734/problem/E 题意:有一棵黑白树,每次操作可以使一个同色连通块变色,问最少几次操作能使树变成全黑或全白. 思路:先进 ...

  6. CodeForces 14D 树的直径 Two Paths

    给出一棵树,找出两条不相交即没有公共点的路径,使得两个路径的长度的乘积最大. 思路:枚举树中的边,将该边去掉,分成两棵树,分别求出这两棵树的直径,乘起来维护一个最大值即可. #include < ...

  7. codeforces 14D(搜索+求树的直径模板)

    D. Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input o ...

  8. TTTTTTTTTTTTT 树的直径 Codeforces Beta Round #14 (Div. 2) D. Two Paths

    tiyi:给你n个节点和n-1条边(无环),求在这个图中找到 两条路径,两路径不相交,求能找的两条路径的长度的乘积最大值: #include <iostream> #include < ...

  9. Codeforces--14D--Two Paths(树的直径)

     Two Paths Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit ...

随机推荐

  1. poj3264(Sparse-Table 算法模板)

    poj3264 题意 询问区间最大值最小值之差. 分析 dp_max[i][j] 表示以 i 为起点,长度为 \(2^j\) 的区间最大值. 利用递推预处理出区间最大值最小值. code #inclu ...

  2. 编译原理学习笔记·语法分析(LL(1)分析法/算符优先分析法OPG)及例子详解

    语法分析(自顶向下/自底向上) 自顶向下 递归下降分析法 这种带回溯的自顶向下的分析方法实际上是一种穷举的不断试探的过程,分析效率极低,在实际的编译程序中极少使用. LL(1)分析法 又称预测分析法, ...

  3. 每天一个liunx命令3之awk实现文本文件的抓取

    =============================================================================grep -h -s -E 'HUAWEI_9 ...

  4. 随机取若干条记录的SQL语句

    原文:随机取若干条记录的SQL语句 MySql中随机提取数据库N条记录 select * from TableName order by rand() limit N   SQLServer中随机提取 ...

  5. linux命令详解:basename命令

    转:http://www.cnblogs.com/lwgdream/archive/2013/11/05/3407768.html 前言 bashname命令用于获取路径中的文件名或路径名(获取的时候 ...

  6. Android可伸缩列表ExpandableListView

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  7. 永久关闭WPS热点

    可以通过设置WPS,关闭广告推送和热点即可,方法如下 准备:打开已经安装wps的设备 1.单击电脑左下角,找到wps 2.选中WPS Office中的配置工具 3.在弹窗中选择“高级” 4.打开之后选 ...

  8. setSystemUiVisibility() 与 getSystemUiVisibility() 显示隐藏状态栏

    Android  4.4  Camera 源码里面有一个操作界面的方法: /** * If {@param visible} is false, this hides the action bar a ...

  9. 阿里蚂蚁的前端ant-design

    蚂蚁国内镜像: http://ant-design.gitee.io/components/date-picker-cn/ 阿里的设计平台:https://design.alipay.com/deve ...

  10. 本地启动tomcat的时候报内存溢出错误:java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: PermGen space

    问题分析: PermGen space的全称是Permanent Generation space,是指内存的永久保存区域,这块内存主要是被JVM存放Class和Meta信息的,Class在被Load ...