题目链接:http://codeforces.com/problemset/problem/14/D

题意:有n个city ; n-1条路:求断开一条路之后分成的两部分所构成的树的直径的积最大是多少;

n的取值范围不大,所以可以采用暴力枚举的方法’;

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#include <queue>
#include <map> using namespace std; typedef long long LL; #define met(a, b) memset(a, b, sizeof(a))
#define INF 0x3f3f3f3f
#define N 210 int G[N][N], n, vis[N], dist[N], Max, Index; void bfs(int s)
{
met(vis, );
vis[s] = ;
dist[s] = ; queue<int> Q; Q.push(s); while( Q.size() )
{
int p = Q.front();
Q.pop(); for(int i=; i<=n; i++)
{
if( !G[p][i] )continue; if( !vis[i] )
{
vis[i] = ; dist[i] = dist[p] + ; Q.push(i); if(dist[i] > Max)
{
Max = dist[i]; Index = i;
}
}
}
}
} int main()
{
int a[N], b[N]; while(scanf("%d", &n)!=EOF)
{
met(G, ); for(int i=; i<n; i++)
{
scanf("%d %d", &a[i], &b[i]); G[a[i]][b[i]] = G[b[i]][a[i]] = ;
} int Ans = ; for(int i=; i<n; i++)
{
G[a[i]][b[i]] = G[b[i]][a[i]] = ; met(dist, );
Max = Index = ;
bfs(a[i]);
bfs(Index); int ans = Max; met(dist, );
Max = Index = ;
bfs(b[i]);
bfs(Index); Ans = max(Ans, ans * Max); G[a[i]][b[i]] = G[b[i]][a[i]] = ;
}
printf("%d\n", Ans);
}
return ;
}

D. Two Paths---cf14D(树的直径)的更多相关文章

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

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

  2. cf14D Two Paths(树的直径)

    题意: N个点构成一棵树.树枝的长度都是1. 在当中找两条不相交[没有公共点]的路,使得二者长度之积最大. (2 ≤ n ≤ 200) 思路: 一开始思路好麻烦,好麻烦,好麻烦,,,,,,,而且WA, ...

  3. cf14d 树的直径,枚举删边

    #include<bits/stdc++.h> using namespace std; #define maxn 300 ]; int n,head[maxn],tot,a,b,dis[ ...

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

  5. Codeforces 14D Two Paths 树的直径

    题目链接:点击打开链接 题意:给定一棵树 找2条点不反复的路径,使得两路径的长度乘积最大 思路: 1.为了保证点不反复,在图中删去一条边,枚举这条删边 2.这样得到了2个树,在各自的树中找最长链.即树 ...

  6. CodeForces 14D 树的直径 Two Paths

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

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

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

  8. Gym - 100676H Capital City(边强连通分量 + 树的直径)

    H. Capital City[ Color: Black ]Bahosain has become the president of Byteland, he is doing his best t ...

  9. Park Visit(树的直径)

    传送门 Park Visit Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

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

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

随机推荐

  1. 前端实现table表格导出excel

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. KMP + 求最小循环节 --- POJ 2406 Power Strings

    Power Strings Problem's Link: http://poj.org/problem?id=2406 Mean: 给你一个字符串,让你求这个字符串最多能够被表示成最小循环节重复多少 ...

  3. 梯度下降算法到logistic回归

    http://sbp810050504.blog.51cto.com/2799422/1608064/ http://blog.csdn.net/dongtingzhizi/article/detai ...

  4. 出错的方法有可能是JDK,也可能是程序员写的程序,无论谁写的,抛出一定用throw

    应对未检查异常就是养成良好的检查习惯. 已检查异常是不可避免的,对于已检查异常必须实现定义好应对的方法. 已检查异常肯定跨越出了虚拟机的范围.(比如“未找到文件”) 如何处理已检查异常(对于所有的已检 ...

  5. mac 安装软件提示权限不足的解决的方法

    假设直接输入命令提示没权限的时候 1.sudo 安装命令 2.sudo su -  这时候切换到root用户下了 ,能够随心所欲了

  6. Wamp2.5 64bit,无法改动MySQL datadir位置

    今天偶然想到去更新一下机子里面PHP的版本号,然后又一次去wamp官网下载了WAMP(wamp 64  Apache : 2.4.9 MySQL : 5.6.17 PHP : 5.5.12 PHPMy ...

  7. 神经网络Batch Normalization——学习笔记

    训练神经网络的过程,就是在求未知参数(权重).让网络搭建起来,得到理想的结果. 分类-监督学习. 反向传播求权重:每一层在算偏导数.局部梯度,链式法则. 激活函数: sigmoid仅中间段趋势良好 对 ...

  8. 织梦dedecms整合discuz论坛的操作方法

    织梦dedecms和discuz论坛整合主要用途,是让两个系统共享用户数据,同一个用户可以在两个网站都可以登录.在我们制作织梦cms模板的时候,有时需要整合discuz里的东细.本文主要讲解一下ded ...

  9. 使用typescript开发react应用

    初始化 mkdir project-dir cd project-dir yarn init -y 安装依赖 yarn add react react-dom yarn add -D typescri ...

  10. shell脚本学习总结05--变量与环境变量

    bash中一切变量都是以字符串的形式存储,env命令可以查看与此终端进程相关的环境变量. man bash 查看一个进程的环境变量 1.获得程序的PID,例如Java# pgrep java23492 ...