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 ...
随机推荐
- tomcat 的log4j配置问题
#log4j.rootLogger=DEBUG,stdout,filelog4j.rootLogger=ERROR,stdout,filelog4j.appender.stdout=org.apach ...
- 2019_京东JAVA实习生招聘机试第一题
题意抽象出来就是,求根节点的所有子节点中,以这些子节点为根的子树的最大节点数. 已有向图的方式来保存无向图,所以叶子结点i的eage[i].size()==1. import java.util.Ar ...
- bitcoin 源码解析 - 交易 Transaction
bitcoin 源码解析 - 交易 Transaction(三) - Script 之前的章节已经比较粗略的解释了在Transaction体系当中的整体运作原理.接下来的章节会对这个体系进行分解,比较 ...
- mysql 增删查改
非关系型数据库关系型数据库Oracle mysql sqlserver db2 Postgresql Sqlite access sqlserver 微软db2 ibm================ ...
- liunx 中安装mysql 图形界面 phpmyadmin
是浏览器图形界面 1. 安装mysql 图形管理工具. 2. 使用phpmyadmin 图像化工具. 3.下载地址 http://www.phpmyadmin.net/ 4. 查看是否安装这两个包 ...
- Report Builder 打开报错
提示:REP-0756:警告:找不到PL/SQL程序库’inv.pll’. 解决办法 处理:1.运行 ---输入REgedit ,在注册表内,查找注册表项Reports_path. 2.将下载的.pl ...
- (待解决)IDEA配置JDBC查询数据库PreparedStatement pstmt = dbconn.prepareStatement(sql)出现空指针错误
package com.demo; import java.io.*; import java.sql.*; import java.util.*; import javax.servlet.*; i ...
- InnoDB INFORMATION_SCHEMA Temporary Table Info Table
InnoDB INFORMATION_SCHEMA Temporary Table Info Table INNODB_TEMP_TABLE_INFO提供有关InnoDB实例中当前活动的用户创建的In ...
- Linux内核学习总览
断断续续学习操作系统已经有大半年时间了,一直想系统地梳理一下. 正好借助<深入Linux内核架构> (Wolfgang Manuere 著,郭旭 译)汇总一下. 首先基础框架篇,Linux ...
- Python开发环境之pyenv环境搭建
首先到Github上下载Pyenv相应的一键安装脚本, 安装完毕,设置环境变量,设置环境变量,可以添加到个人家目录的.bashrc或.bash_profile及系统的全局的/etc/profile, ...