D. Two Paths---cf14D(树的直径)
题目链接: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(树的直径)的更多相关文章
- 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 ...
随机推荐
- 对redis深入理解
1.Redis有哪些数据结构? 字符串String.字典Hash.列表List.集合Set.有序集合SortedSet.如果你是Redis中高级用户,还需要加上下面几种数据结构HyperLogLog. ...
- 禁止sethc.exe运行 防止3389的sethc后门
废话:在土司看到的一篇文章,发私信给那个哥们儿说让不让转载,结果还没回复我就在百度看到相同的文章.他自己也是转载的.这哥们儿ID迟早被ban 文章转载自:http://www.jb51.net/hac ...
- [Eth]网络查看命令:route
最近在调试网络,出现问题,两个网口分别接外网内网,结果不同 http://www.cnblogs.com/peida/archive/2013/03/05/2943698.html
- 如何让每个 WordPress 页面有不同的风格或者样式
如果使用 page.php 来处理所有页面的外观的话,答案肯定是不行的,但是如果使用不同的 WordPress 页面模板,就可以自定义每个页面的外观了. 比如你博客的所有的页面除了“关于”这个页面之外 ...
- 关于SQL高量问题
一工作今天在用DataTable.Table.Select("字段 like")查询时候老是碰到格式不正确 dtrFoundRow = dtvOOView.Table.Select ...
- C++ 引用本质的详解
//引用本质的理解① #include<iostream> using namespace std; int GetA(){ ; return a; } int & GetB(){ ...
- EF报LINQ to Entities 不识别方法“Web_JZRepository.Models.copy_materials_details get_Item(Int32) ”,因此该方法无法转换为存储表达式。
说明用了如 List<T> list=new List<T>(); je.copy_materials_details.SingleOrDefault(x => x.ID ...
- log4j配置文件
log4j.rootLogger=INFO,CONSOLElog4j.addivity.org.apache=truelog4j.appender.stdout=org.apache.log4j.Co ...
- 运动目标检测ViBe算法
一.运动目标检测简介 视频中的运动目标检测这一块现在的方法实在是太多了.运动目标检测的算法依照目标与摄像机之间的关系可以分为静态背景下运动检测和动态背景下运动检测.先简单从视频中的背景类型来讨论. ...
- JNI 各类数据类型处理
JNI和java数据类型转换: 1.基本数据类型下面一张表是描述了 Java 基本数据类型和JNI中基本数据类型的相对应关系已经占用空间大小. 随便观察就能发现,其实就基本数据类型而已,JNI基本数据 ...