Two Paths--cf14D(树的直径)
2 seconds
64 megabytes
standard input
standard output
As you know, Bob's brother lives in Flatland. In Flatland there are n cities, connected by n - 1 two-way roads. The cities are numbered from 1 to n. You can get from one city to another moving along the roads.
The «Two Paths» company, where Bob's brother works, has won a tender to repair two paths in Flatland. A path is a sequence of different cities, connected sequentially by roads. The company is allowed to choose by itself the paths to repair. The only condition they have to meet is that the two paths shouldn't cross (i.e. shouldn't have common cities).
It is known that the profit, the «Two Paths» company will get, equals the product of the lengths of the two paths. Let's consider the length of each road equals 1, and the length of a path equals the amount of roads in it. Find the maximum possible profit for the company.
The first line contains an integer n (2 ≤ n ≤ 200), where n is the amount of cities in the country. The following n - 1 lines contain the information about the roads. Each line contains a pair of numbers of the cities, connected by the road ai, bi (1 ≤ ai, bi ≤ n).
Output the maximum possible profit.
4
1 2
2 3
3 4
1
7
1 2
1 3
1 4
1 5
1 6
1 7
0
6
1 2
2 3
2 4
5 4
6 4
4 直接暴力
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include <vector>
#include <queue> using namespace std;
#define memset(a,b) memset(a,b,sizeof(a))
#define N 250
int G[N][N],vis[N],dis[N];
int Index,Max,n; void bfs(int u)
{
Max=;
memset(vis,);
memset(dis,);
queue<int>Q;
Q.push(u);
vis[u]=;
while(!Q.empty())
{
int p;
p=Q.front();
Q.pop();
for(int i=; i<=n; i++)
{
if(G[p][i]!=)
{
if(!vis[i])
{
vis[i]=;
Q.push(i);
dis[i]=dis[p]+;
if(Max<dis[i])
{
Max=dis[i];
Index=i;
}
}
}
}
}
} int main()
{
int a[N],b[N];
while(scanf("%d",&n)!=EOF)
{
memset(G,);
memset(a,);
memset(b,);
for(int i=; i<n; i++)
{
scanf("%d %d",&a[i],&b[i]);
G[a[i]][b[i]]=G[b[i]][a[i]]=;
}
int MM=;
for(int i=; i<n; i++)
{
int x,y;
G[a[i]][b[i]]=G[b[i]][a[i]]=;
Index=;
bfs(a[i]);
bfs(Index);
x=Max; Index=;
bfs(b[i]);
bfs(Index);
y=Max;
MM=max(MM,x*y);
G[a[i]][b[i]]=G[b[i]][a[i]]=;
}
printf("%d\n",MM);
}
return ;
}
Two Paths--cf14D(树的直径)的更多相关文章
- Codeforces--14D--Two Paths(树的直径)
Two Paths Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit ...
- cf14D Two Paths(树的直径)
题意: N个点构成一棵树.树枝的长度都是1. 在当中找两条不相交[没有公共点]的路,使得二者长度之积最大. (2 ≤ n ≤ 200) 思路: 一开始思路好麻烦,好麻烦,好麻烦,,,,,,,而且WA, ...
- cf14d 树的直径,枚举删边
#include<bits/stdc++.h> using namespace std; #define maxn 300 ]; int n,head[maxn],tot,a,b,dis[ ...
- 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 14D Two Paths 树的直径
题目链接:点击打开链接 题意:给定一棵树 找2条点不反复的路径,使得两路径的长度乘积最大 思路: 1.为了保证点不反复,在图中删去一条边,枚举这条删边 2.这样得到了2个树,在各自的树中找最长链.即树 ...
- CodeForces 14D 树的直径 Two Paths
给出一棵树,找出两条不相交即没有公共点的路径,使得两个路径的长度的乘积最大. 思路:枚举树中的边,将该边去掉,分成两棵树,分别求出这两棵树的直径,乘起来维护一个最大值即可. #include < ...
- TTTTTTTTTTTTT 树的直径 Codeforces Beta Round #14 (Div. 2) D. Two Paths
tiyi:给你n个节点和n-1条边(无环),求在这个图中找到 两条路径,两路径不相交,求能找的两条路径的长度的乘积最大值: #include <iostream> #include < ...
- Gym - 100676H Capital City(边强连通分量 + 树的直径)
H. Capital City[ Color: Black ]Bahosain has become the president of Byteland, he is doing his best t ...
- Park Visit(树的直径)
传送门 Park Visit Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- codeforces 14D(搜索+求树的直径模板)
D. Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input o ...
随机推荐
- mac homebrew安装
http://book.51cto.com/art/201107/278761.htm 3.2.3 使用 Homebrew 安装 Git Mac OS X 有好几个包管理器,用于管理一些开源软件在 M ...
- 华为S3700交换机DHCP 配置
1.设置交换机名称 system-view [Huawei]sysname dhcp01 [dhcp01] 2.配置管理IP [dhcp01]interface Vlanif 1 [dhcp01-Vl ...
- CATransaction 知识
CATransaction 事务类,可以对多个layer的属性同时进行修改.它分隐式事务,和显式事务. 区分隐式动画和隐式事务:隐式动画通过隐式事务实现动画 . 区分显式动画和显式事务:显式动画有多种 ...
- 介绍三款大前端UI框架
一.蚂蚁金服团队推出的基于React antd (全名:ant.design) 友情跳链:https://ant.design/index-cn:使用antd模板:https://pro.ant.de ...
- render_to_response()
render_to_response('模板名称',字典) 字典:第二个参数必须是为该模板创建context时所使用的字典,如果不提供第二个参数,render_response()使用一个空字典
- eclipse生成spring boot jar包
1.右击项目,选择Run As - Maven clean 2.右击项目,选择Run As - Maven install 3.成功后 会在项目的target文件夹下生成jar包 4.将打包好的jar ...
- vc++创建多线程应用
构建线程参数结构体: typedef struct { int nIndex; HANDLE hThread; int param1; ... }ThreadParam; 创建线程数组: Thread ...
- 开源代码生成器,基于mybatis-generator扩展,结合freemarker
git源码地址:https://github.com/JonSnow592622272/free-generator-code 码云gitee源码地址:https://gitee.com/a59262 ...
- [BZOJ2120]:数颜色(分块?)
题目传送门 我感觉这种题没必要扯淡题目大意了,没啥用. 暴力过掉,擦了个边. 主要是讲一下这道题我用到的卡常. 首先,0,1标记我用的位运算,位运算符跑的要比正常的+,-,×,÷,true,false ...
- SQL比较两表字段和字段类型
一.问题 业务需要把TB_Delete_KYSubProject表数据恢复到TB_KYSubProject,但提示错误,错误原因是两表字段类型存在不一致 insert into [TB_KYSubPr ...