CF-13D - Two Paths(搜索)
2013-01-15 15:44
399 查看
D - Two Paths
Crawling in process...Crawling failedTime
Limit:2000MS Memory Limit:65536KB
64bit IO Format:%I64d & %I64u
SubmitStatusPracticeCodeForces
14D
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
思路:因为题目给的是一棵树求两条无公共点的最长路径的积。所以可以随便在树里选条边删掉,再剩下的两颗子树中找最长的相乘即可。
如图选删去AB边,那就从A开始搜两条最长分支的路径之和就是A所在部分的最长路径。同样搜B。
所以A所在的只有一条最长分支为3,B有两条,一个是3一个是2所以B所在最长为4
Crawling in process...Crawling failedTime
Limit:2000MS Memory Limit:65536KB
64bit IO Format:%I64d & %I64u
SubmitStatusPracticeCodeForces
14D
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
思路:因为题目给的是一棵树求两条无公共点的最长路径的积。所以可以随便在树里选条边删掉,再剩下的两颗子树中找最长的相乘即可。
如图选删去AB边,那就从A开始搜两条最长分支的路径之和就是A所在部分的最长路径。同样搜B。
所以A所在的只有一条最长分支为3,B有两条,一个是3一个是2所以B所在最长为4
#include<iostream> #include<cstring> using namespace std; const int mm=233; int map[mm][mm]; int n,ans; int dep; int dfs(int u,int p) { int max1=0,max2=0; int path_max=0; for(int i=1;i<=n;i++) if(map[u][i]&&i!=p) { int z=dfs(i,u); if(path_max<z)path_max=z; if(max1<dep)///更新当前点的两条最长路径 { max2=max1;max1=dep; } else if(max2<dep)max2=dep; } if(path_max<max1+max2)path_max=max1+max2;///最长路径等于最长两条分路径和 dep=max1+1;///更新上层最大深度为当前层最大深度 return path_max; } int main() { int a,b; while(cin>>n) { memset(map,0,sizeof(map)); for(int i=0;i<n-1;i++) { cin>>a>>b;map[a][b]=map[b][a]=1; } ans=0; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(map[i][j]) { int a=dfs(i,j); int b=dfs(j,i); if(ans<a*b) ans=a*b; } cout<<ans<<"\n"; } }
相关文章推荐
- CF-13D - Two Paths(搜索)
- SCOM 2012知识分享-13:搜索功能
- CF 678C 暑假集训13(gcd,lcm)
- Codeforces Beta Round #14 (Div. 2) D. Two Paths 树形dp
- CF_Round274_Div1_B_Long Jump_二分搜索
- 【Henu ACM Round#16 D】Bear and Two Paths
- HDU 3131 One…Two…Five! (暴力搜索)
- 13 款开源的全文搜索引擎
- <cf>Two Problems
- HDU Sequence two 2611 DFS搜索判重
- 搜索系统13:一个查询从solr到lucene都干了些什么事?
- 【搜索】[AtCoder Regular Contest 092 F]Two Faced Edges
- codeforces 673D D. Bear and Two Paths(构造)
- Codeforces Round #295 (Div. 2)---B. Two Buttons( bfs步数搜索记忆 )
- hdu Two Paths 次短路模板 (可往回走)
- leetcode 257. Binary Tree Paths-树路径|深度搜索
- CF Comparing Two Long Integers 616A
- Codeforces edu 6. D Professor GukiZ and Two Arrays 二分搜索 枚举
- Codeforces--14D--Two Paths(树的直径)
- CodeForces 673D - Bear and Two Paths(构造)