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 ...
随机推荐
- How exception works ?
这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=28 February 18, 2013 How exception work ...
- 备忘录:python 3在class中使用yield
之前代码都是直接在函数级别使用yield,但封装class后如何使用yield很少遇到. 经过半天的学习,总算完成示例.代码没有什么特殊地方,仅仅作为一个工作项. 与生成器合作: ########## ...
- 使用ABAP正则表达式解析HTML标签
需求就是我用ABAP的某个函数从数据库读取一个字符串出来,该字符串的内容是一个网页. 网页的form里包含了很多隐藏的input field.我的任务是解析出name为svyValueGuid的inp ...
- raid 0 1 5 10 总结的知识点
raid 0 1 5 10 raid 发的别名条带 raid 0 读取性能最高需要磁盘2*N个(N>0)代表所有raid级别中的最高存储性能,其实原理就是把连续的数据分散到多个磁盘上存取,这样, ...
- Zotero文献管理神器使用
为什么使用Zotero管理论文? 1.可以从网上剪藏 2.可以查询 3.有作者 标题 期刊 索引 4.word自动生成论文索引 把pdf文件导入Zotero 按住ctrl+shift拖动pdf文件,就 ...
- 在proe模型文件里面存储用户数据
存储外部数据 author:visualsan 2014.2 上海 1.简介 利用外部数据存储外部接口,可以在模型文件里面尺寸用户自定义数据.在模型保存时数据自动存储,在模型载入时数据自动载入.外部数 ...
- 【整理】解决vue不相关组件之间的数据传递----vuex的学习笔记,解决报错this.$store.commit is not a function
解决vue不相关组件之间的数据传递----vuex的学习笔记,解决报错this.$store.commit is not a function https://www.cnblogs.com/jaso ...
- 输入3个数a,b,c,按大小顺序输出。
题目:输入3个数a,b,c,按大小顺序输出. 思路: 根据最简单的, 经典的C语言算法, 两两相互交换得到他们的顺序 public class 第三十四题abc三个数大小排序 { public sta ...
- 717. 1-bit and 2-bit Characters@python
We have two special characters. The first character can be represented by one bit 0. The second char ...
- python UDP-数据报协议
基于udp协议通信的套接字 服务端 from socket import * server = socket(AF_INET, SOCK_DGRAM) # SOCK_DGRAM=>数据报协议 s ...