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】Word Break II
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- codeforces 518B. Tanya and Postcard 解题报告
题目链接:http://codeforces.com/problemset/problem/518/B 题目意思:给出字符串 s 和 t,如果 t 中有跟 s 完全相同的字母,数量等于或者多过 s,就 ...
- JavaScript高级程序设计学习笔记--函数表达式
关于函数声明,它的一个重要特征就是函数声明提升,意思是在执行代码之间会读取函数声明,意思是在执行代码之前会先读取函数声明.这就意味着可以把函数声明放在调用它的语句 后面. sayHi(); funct ...
- codeforces gym 100286 H - Hell on the Markets (贪心算法)
题目链接 题意:n个数分别为a[i],问是否存在一组对应的b[i],b[i]=1 || b[i]=-1,使得ai*bi的n项和为0. 题解: 先证明一个结论吧,对于1≤ai≤i+1,前面ai个数一定可 ...
- xcode报错,svn : is not a workingCopy
解决方案: 1.点击对应的targets 2.选择build phases 3.在红框处有一个run script选项(截图中已经删除了.),点击下拉按钮,看看是否是与svn有关的东西, 如果是,删除 ...
- rsync实现同步
一.备份客户端: 1.创建/etc/rsyncd.secrets 权限配置600 (写服务器端的账户密码) 2.客户端配置文件: port=873log file=/var/log/rsync.log ...
- iOS键盘监听的通知
#pragma mark - 键盘通知回调方法 // 监听键盘的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector: ...
- 理解KMP算法
母串:S[i] 模式串:T[i] 标记数组:Next[i](Next[i]表示T[0~i]最长前缀/后缀数) 先来讲一下最长前缀/后缀的概念 例如有字符串T[6]=abcabd接下来讨论的全部是真前缀 ...
- JQ JSON数据类型
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Powershell实例小结(服务管理)
有关服务管理的具体实例脚本如下: #$lists="1.1.1.1","2.2.2.2" #远程ip列表 foreach ($list in $lists){ ...