Codeforces Round #14 D. Two Paths(求树上两条不相交的路径的乘积最大值)
题目链接:
http://codeforces.com/problemset/problem/14/D
思路:直接枚举每一天路径的两端,然后求以每一端为树根的树上最长路径,然后相乘就可以了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std; const int MAX_N = (200 + 20);
int N, ed, maxlen, ans, dp[MAX_N], pre[MAX_N];
vector<int > g[MAX_N]; void dfs(int u, int fa, int len, int limit)
{
dp[u] = len;
pre[u] = fa;
REP(i, 0, (int)g[u].size()) {
int v = g[u][i];
if (v != fa && v != limit) dfs(v, u, len + 1, limit);
}
} int gao(int u, int v)
{
memset(dp, 0, sizeof(dp));
dfs(u, -1, 0, v);
maxlen = 0; ed = u;
FOR(i, 1, N) if (dp[i] > maxlen) maxlen = dp[i], ed = i;
dfs(ed, -1, 0, v);
maxlen = 0;
FOR(i, 1, N) if (dp[i] > maxlen) maxlen = dp[i], ed = i;
maxlen = 0;
while (pre[ed] != -1) ed = pre[ed], ++maxlen;
return maxlen;
}
int main()
{
cin >> N;
REP(i, 1, N) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
ans = 0;
FOR(i, 1, N) {
REP(j, 0, (int)g[i].size()) {
int l = gao(i, g[i][j]);
int r = gao(g[i][j], i);
ans = max(ans, l * r);
}
}
cout << ans << endl;
return 0;
}
Codeforces Round #14 D. Two Paths(求树上两条不相交的路径的乘积最大值)的更多相关文章
- c编程:求出4×4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和。
//求出4×4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和 #include <stdio.h> int main() { int sum=0; int max, ...
- Educational Codeforces Round 14
A - Fashion in Berland 水 // #pragma comment(linker, "/STACK:102c000000,102c000000") #inclu ...
- Educational Codeforces Round 12 F. Four Divisors 求小于x的素数个数(待解决)
F. Four Divisors 题目连接: http://www.codeforces.com/contest/665/problem/F Description If an integer a i ...
- Educational Codeforces Round 14 - F (codeforces 691F)
题目链接:http://codeforces.com/problemset/problem/691/F 题目大意:给定n个数,再给m个询问,每个询问给一个p,求n个数中有多少对数的乘积≥p 数据范围: ...
- Educational Codeforces Round 14 D. Swaps in Permutation
题目链接 分析:一些边把各个节点连接成了一颗颗树.因为每棵树上的边可以走任意次,所以不难想出要字典序最大,就是每棵树中数字大的放在树中节点编号比较小的位置. 我用了极为暴力的方法,先dfs每棵树,再用 ...
- Educational Codeforces Round 14 D. Swaps in Permutation (并查集orDFS)
题目链接:http://codeforces.com/problemset/problem/691/D 给你n个数,各不相同,范围是1到n.然后是m行数a和b,表示下标为a的数和下标为b的数可以交换无 ...
- Educational Codeforces Round 14 D. Swaps in Permutation(并查集)
题目链接:http://codeforces.com/contest/691/problem/D 题意: 题目给出一段序列,和m条关系,你可以无限次互相交换这m条关系 ,问这条序列字典序最大可以为多少 ...
- Educational Codeforces Round 14 D. Swaps in Permutation 并查集
D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are gi ...
- Educational Codeforces Round 14 C. Exponential notation 数字转科学计数法
C. Exponential notation 题目连接: http://www.codeforces.com/contest/691/problem/C Description You are gi ...
随机推荐
- 【leetcode】Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- struts2 action 3中书写方式
1.pojo类 简单的javabean 类 需要有 public String execute(){return null;} 2.实现 Action 借口 重写 execute 3.继承 Ac ...
- Python中用format函数格式化字符串的用法
这篇文章主要介绍了Python中用format函数格式化字符串的用法,格式化字符串是Python学习当中的基础知识,本文主要针对Python2.7.x版本,需要的朋友可以参考下 自python2. ...
- UIScrollView 的 delaysContentTouches
UIScrollView 的一段说明: Because a scroll view has no scroll bars, it must know whether a touch signals a ...
- Selenium FF WebDriver运行时开启firebug的2种方式
上一次我实测FF webdriver 加载firefoxhttp://www.cnblogs.com/tobecrazy/p/3997375.html 那么问题就来了,既然能加载firebug能否在运 ...
- ACM/ICPC 之 数论-素数筛选法 与 "打表"思路(POJ 1595)
何为"打表"呢,说得简单点就是: 有时候与其重复运行同样的算法得出答案,还不如直接用算法把这组数据所有可能的答案都枚举出来存到一个足够大的容器中去-例如数组(打表),然后再输入数据 ...
- settings的保存位置
xp:C:\Documents and Settings\Administrator\Local Settings\Application Data\ win8 C:\Users\XXX\AppDat ...
- CentOS7 win7 u盘装双系统 修复系统
环境: ASUS MB VER K45VD 笔记本电脑一台( i5-3230M 处理器. SATA . Nvidia). Windows7 系统 在 win7 下安装 CentOS7 使用 Ultra ...
- xcode运行push通知总是提示输入用户名和密码
xcode运行push通知总是提示输入用户名和密码,目前找到的解决方案是, 打开钥匙串,然后找到push证书下面的私有密钥,双击进入将访问控制设置为允许全部访问,即可.
- 阿里云CentOS配置全过程
1. 安装基本依赖包 yum install gcc gcc-c++ autoconf automake 2. 升级所有 yum update 3.安装mongodb 1. 配置mongodb- ...