题目链接:点击打开链接

题意:给定一棵树

找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. Python的网络编程[1] -> FTP 协议[2] -> 使用 ftplib 建立 FTP 客户端

    使用 ftplib 建立 FTP 客户端 用于建立FTP Client,与 pyftplib 建立的 Server 进行通信. 快速导航 1. 模块信息 2. 建立 FTP 客户端 1. 模块信息 1 ...

  2. [POI2014]Around the world

    题目大意: 一个环上有$n(n\le10^6)$个点,每个点之间的距离为$l_i(l_i\le10^9)$.有$m(m\le100)$架飞机,每架飞机单次最大航行距离为$d_i$.飞机只能在点上起飞. ...

  3. http网页性能最佳实践

    你愿意为打开一个网页等待多长时间?我一秒也不愿意等.但是事实上大多数网站在响应速度方面都让人失望.现在越来越多的人开始建立自己的网站,博客,你的网页响应速度如何呢?在这篇文章中我们来介绍一下提高网页性 ...

  4. CentOS 6.9设置IP、网关、DNS

    在/etc/sysconfig/network-scripts目录下,用vi命令编辑eth0: DEVICE=eth0 #设备名称 HWADDR=C8:1F:66:B8:EB:9F #物理地址 ONB ...

  5. tiny4412 串口驱动分析八 --- log打印的几个阶段之内核启动阶段(printk tiny4412串口驱动的注册)

    作者:彭东林 邮箱:pengdonglin137@163.com 开发板:tiny4412ADK+S700 4GB Flash 主机:Wind7 64位 虚拟机:Vmware+Ubuntu12_04 ...

  6. BFS求最短路

    假设有一个n行m列的迷宫,每个单位要么是空地(用1表示)要么是障碍物(用0表示).如和找到从起点到终点的最短路径?利用BFS搜索,逐步计算出每个节点到起点的最短距离,以及最短路径每个节点的前一个节点. ...

  7. git回退到某个commit

    git log查看提交历史及提交的commit_id 回退命令: $ git reset --hard HEAD^ 回退到上个版本$ git reset --hard HEAD~3 回退到前3次提交之 ...

  8. 为什么输入shutdown -h -t会报错:command not fount

    如果是直接用普通用户($)的身份进行输入[user@localhost ~]$ shutdown -h -t 是不能执行,因为普通用户没有关闭机器的权限. 然而直接使用[user@localhost ...

  9. 2017.5.9 java多线程总结

    参考来自:http://www.cnblogs.com/lwbqqyumidi/p/3804883.html http://blog.csdn.net/gf771115/article/details ...

  10. PS 如何制作球面化文字效果

    球面化文字效果图....   00newopen-a   00newopen-b     01mew+channel   02ctrl+L   03ctrl+I   04new+wenzi   05R ...