CF 14 problem D - Two paths
来源:互联网 发布:windows xp的外观设置 编辑:程序博客网 时间:2024/05/16 14:33
D. Two Paths
time limit per test2 seconds
memory limit per test64 megabytes
inputstandard input
outputstandard output
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.
Examples
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
题意:
本题目就是要求出一棵树上面两条路径长度的乘积,输出最大的乘积即可。
思路:
枚举每条边,对于每一条边,我都可以求出断开这条边以后得到的两条最长的路径的长度。其中的最大值就是答案。
代码
#include<cstdio>#include<vector>#include<cstring>#include<algorithm>using namespace std;vector<int >ve[300];int n;int vis[300];int dis[300];int tar[300];int zux,zuy;bool ok(int x,int y){ if(x==zux&&y==zuy) return 1; else if(x==zuy&&y==zux) return 1; return 0;}void dfs(int num){ tar[num]=1; vis[num]=1; for(int i=0;i<ve[num].size();i++) { int x=ve[num][i]; if(vis[x]||ok(x,num)) continue; dis[x]=dis[num]+1; dfs(x); }}int pac(int x){ memset(dis,0,sizeof dis); memset(vis,0,sizeof vis); dfs(x); int ans=0; for(int i=1;i<=n;i++) { if(dis[ans]<dis[i]) ans=i; } memset(dis,0,sizeof dis); memset(vis,0,sizeof vis); dfs(ans); ans=0; for(int i=1;i<=n;i++) { if(ans<dis[i]) ans=dis[i]; } return ans;}int solve(){ memset(tar,0,sizeof tar); int t1=0,t2=0; if(ve[zux].size()==1) t1=0; else t1=pac(zux); if(ve[zuy].size()==1) t2=0; else t2=pac(zuy); return t1*t2;}int main(){ while(scanf("%d",&n)!=EOF) { for(int i=1;i<=n;i++) ve[i].clear(); int x,y; for(int i=0;i<n-1;i++) { scanf("%d%d",&x,&y); ve[x].push_back(y); ve[y].push_back(x); } int ans=0; for(int i=1;i<=n;i++) { for(int j=0;j<ve[i].size();j++) { zux=i; zuy=ve[i][j]; ans=max(ans,solve()); } // printf("ans:%d\n",ans); } printf("%d\n",ans); } return 0;}
- CF 14 problem D - Two paths
- CF-13D - Two Paths(搜索)
- Codeforces 14D Two Paths
- CodeForces 14D Two Paths
- Codeforces Beta Round #14 D - Two Paths
- CodeForces 14 E - Camels && D - Two Paths
- codeforces 14D Two Paths dfs
- CF:Problem 383D
- Codeforces 14D Two Paths 树的直径
- Codeforces--14D--Two Paths(树的直径)
- Codeforces 14D Two Paths 【树的直径】
- Codeforces Beta Round #14 (Div. 2) D. Two Paths
- 【CodeForces】14D - Two Paths(树的直径)
- CF 251D Two Sets
- codeforcs 351 D. Bear and Two Paths
- CF 8D Two Friends (三分+二分)
- CF 8D Two Friends 【二分+三分】
- 【CF 125D】 Two progressions 划分等差数列
- Android-百度地图之方向感应和模式更改
- 面向对象(方法的形式参数是类名的时候如何调用)
- js防刷新的倒计时代码(未测试)
- 即使是没有结果的爱恋,我依然会选择你
- 二分查找——B:Can you solve this equation? 解题报告
- CF 14 problem D - Two paths
- 离散数学中输出律的证明:(P∧Q→R)恒等于(P→(Q→R))
- POJ 1325 Machine Schedule 二分图 最小覆盖
- LeetCode 292. Nim Game
- CORS(跨域资源共享)简介。。。ie67不支持
- ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
- mysql导入导出 (未测试)
- iOS 使用AFN 进行单图和多图上传
- xcode 中不小心删除 stroryboard 的恢复办法