Codeforces Round #14 (Div. 2) D. Two Paths ( 树的直径
来源:互联网 发布:php集成环境包 编辑:程序博客网 时间:2024/06/05 21:12
Two Paths
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
41 22 33 4
71 21 31 41 51 61 7
61 22 32 45 46 4
Sample Output
1
0
4
题意
n个点,n-1条无向边求这个图中最长的两条不相交的最长长度
题解:
我们可以枚举每一条边,用这条边把图分成两部分,求各自部分的树的直径
AC代码
#include <bits/stdc++.h>using namespace std;#define LL long long#define CLR(a,b) memset(a,(b),sizeof(a))const int N = 1e3;vector<int> v[N];int dis[N];bool vis[N];int res, sum;struct node { int s, t;} edge[N]; void bfs(int s,int t){ CLR(vis,false); CLR(dis,0); vis[s] = vis[t] = true; res = 0; queue<int> q; q.push(s); while(!q.empty()) { int p = q.front(); q.pop(); if(dis[p] > sum) { sum = dis[p]; res = p; } for(int i = 0;i < v[p].size(); i++) { int u = v[p][i]; if(!vis[u]) { vis[u] = true; dis[u] = max(dis[u],dis[p]+1); q.push(u); } } } }int main(){ int n; while(cin>>n) { for(int i = 0;i <= n; i++) v[i].clear(); for(int i = 0;i < n-1; i++) { cin>>edge[i].s>>edge[i].t; v[edge[i].s].push_back(edge[i].t); v[edge[i].t].push_back(edge[i].s); } int ans = 0; for(int i = 0;i < n-1; i++) { sum = 0; bfs(edge[i].s,edge[i].t); bfs(res,edge[i].t); int xx = sum; sum = 0; bfs(edge[i].t,edge[i].s); bfs(res,edge[i].s); int yy = sum; ans = max(ans,xx*yy); } cout<<ans<<endl; }return 0;}
- Codeforces Round #14 (Div. 2) D. Two Paths ( 树的直径
- Codeforces 14D Two Paths 树的直径
- Codeforces--14D--Two Paths(树的直径)
- Codeforces 14D Two Paths 【树的直径】
- 【CodeForces】14D - Two Paths(树的直径)
- Codeforces Beta Round #14 (Div. 2) D. Two Paths
- Codeforces Round #328 (Div. 2) D. Super M (树的直径,虚树的直径)
- 【DFS——Codeforces Beta Round #14 (Div. 2)】D. Two Paths
- Codeforces Beta Round #14 (Div. 2) D. Two Paths 树形dp
- Codeforces Beta Round #14 D - Two Paths
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D. Bear and Two Paths
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D. Bear and Two Paths
- Codeforces Round #328 (Div. 2) 592 D. Super M 树的直径
- 树的直径 Codeforces Round #260 (Div. 2)E
- [树的直径] Codeforces 804D Round #411 (Div. 1) D. Expected diameter of a tree
- Codeforces Round #411 (Div. 1) D. Expected diameter of a tree(树的直径)
- Codeforces Round #14 D. Two Paths(求树上两条不相交的路径的乘积最大值)
- Codeforces 14D Two Paths
- 验证一组数字是否magic square
- 1112. Stucked Keyboard (20)
- 数据内存存储
- 【bzoj2789】 [Poi2012]Letters
- HDUoj 5723 Abandoned country(最小生成树+dfs
- Codeforces Round #14 (Div. 2) D. Two Paths ( 树的直径
- 网络性能测试工具qperf使用
- 51 node 基础题
- RecyclerView系列(3)—ItemDirection打造粘性悬浮分组title
- UVA 1639——Candy(期望,精度处理)
- Android自实现百分比布局
- 关于word2016保存失败
- hibernate的实体类编写规则与主键生成策略
- 剖析整数,浮点数在内存中的存储