Codeforces--14D--Two Paths(树的直径)
2016-03-12 17:03
387 查看
Two Paths
Submit
Status
Description
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.
Input
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
Output the maximum possible profit.
Sample Input
Input
Output
Input
Output
Input
Output
Sample Output
Hint
Source
Codeforces Beta Round #14 (Div. 2)
n个点,n-1条无向边求这个图中最长的两条不相交的链长度,因为这里只有n-1条边,所以这一定不会存在环,所以我们可以枚举每一条边,用这条边把图分成两部分,求各自部分的树的直径,然后乘积,dfs的时候记录最长和次长的树链长度
Two Paths
Time Limit: 2000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
Status
Description
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.
Input
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
Output the maximum possible profit.
Sample Input
Input
4 1 2 2 3 3 4
Output
1
Input
7 1 2 1 3 1 4 1 5 1 6 1 7
Output
0
Input
6 1 2 2 3 2 4 5 4 6 4
Output
4
Sample Output
Hint
Source
Codeforces Beta Round #14 (Div. 2)
n个点,n-1条无向边求这个图中最长的两条不相交的链长度,因为这里只有n-1条边,所以这一定不会存在环,所以我们可以枚举每一条边,用这条边把图分成两部分,求各自部分的树的直径,然后乘积,dfs的时候记录最长和次长的树链长度
#include<iostream> #include<vector> #include<algorithm> using namespace std; vector<int>G[1010]; int ans,n; int dfs(int v,int x) { int sum=0; int max1=0,max2=0; for(int i=0;i<G[v].size();i++) { if(G[v][i]!=x) { sum=max(sum,dfs(G[v][i],v)); if(ans>max1) { max2=max1; max1=ans; } else max2=ans>max2?ans:max2; } } sum=max(sum,max1+max2); ans=max1+1; return sum; } int main() { while(cin>>n) { for(int i=1;i<=n;i++) G[i].clear(); int x,y; for(int i=0;i<n-1;i++) { ans=0; cin>>x>>y; G[x].push_back(y); G[y].push_back(x); } int maxx=0; for(int i=1;i<=n;i++) { for(int j=0;j<G[i].size();j++) { ans=0; x=dfs(G[i][j],i); y=dfs(i,G[i][j]); maxx=max(maxx,x*y); } } cout<<maxx<<endl; } return 0; }
相关文章推荐
- POJ-2594-Treasure Exploration-(路径相交的最小路径覆盖+闭包传递)
- 4G模块中兴ME3760调试记录
- Qt读取excel
- 第三周项目1:个人所得税计算器
- 字符串连接
- 实战:mysql版本号升级
- 四则运算二
- Codeforces--14D--Two Paths(树的直径)
- c语言:建立简单的静态链表,它由3个学生数据的结点组成,要求输出各结点的数据
- Git学习
- Android中的线程与线程池
- [leetcode 250]Count Univalue Subtrees ------元素都相同的子树个数
- C# 随机数类
- reduce()
- 画图板小结
- 编写Python小程序来统计测试脚本的关键字
- TextView的使用
- android ImageLoader加载本地图片的工具类
- Android源代码之DeskClock (一)
- 我的软件工程课目标