[codeforces]14D

D. Two Paths
time limit per test : 2 seconds
memory limit per test : 64 megabytes

As you know, Bob’s brother lives in Flatland. In Flatland there are n cities, connected by n   −   1 n - 1 n1 two-way roads. The cities are numbered from 1 1 1 to n n 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 ) n (2 ≤ n ≤ 200) n(2n200), where n is the amount of cities in the country. The following n   −   1 n - 1 n1 lines contain the information about the roads. Each line contains a pair of numbers of the cities, connected by the road a i ,   b i ( 1   ≤   a i ,   b i   ≤   n ) ai, bi (1 ≤ ai, bi ≤ n) ai,bi(1ai,bin).
###Output
Output the maximum possible profit.
###Examples
Input1

4
1 2
2 3
3 4

Output1

1

Input2

7
1 2
1 3
1 4
1 5
1 6
1 7

Output2

0

Input3

6
1 2
2 3
2 4
5 4
6 4

Output3

4

题意:
给你一棵树,问树上任意两条不相交的路径(没有公共点即不相交)的长度乘积最大为多少。

题解:
因为n<=200。枚举边将其删去,那么这个树就变成两棵树了,然后将两棵树上最长路径长度相乘的最大值求出来就好了。

#include<bits/stdc++.h>
#define INF 2e9
using namespace std;
int n,dis[204],ans,ne,h[204];
struct edge{
    int to,nt;
}e[40004];
bool vis[204];
queue<int>q;
int add(int u,int v){
    e[++ne].to=v;e[ne].nt=h[u];h[u]=ne;
    return 0;
}
int bfs(int st){
    int pos=st,res;
    for(int i=0;i<=201;i++)dis[i]=INF;
    dis[st]=0;
    q.push(st);
    while(!q.empty()){
        int x=q.front();q.pop();
        for(int i=h[x];i;i=e[i].nt){
            if( vis[x]&&vis[e[i].to])continue;
            if( dis[e[i].to]>dis[x]+1){
                dis[e[i].to]=dis[x]+1;
                q.push(e[i].to);
            }
        }
    }
    for(int i=0;i<=201;i++)if(dis[i]!=INF&&dis[i]>dis[pos])pos=i;
    for(int i=0;i<=201;i++)dis[i]=INF;
    dis[pos]=0;res=pos;
    q.push(pos);
    while(!q.empty()){
        int x=q.front();q.pop();
        for(int i=h[x];i;i=e[i].nt){
            if( vis[x]&&vis[e[i].to])continue;
            if( dis[e[i].to]>dis[x]+1){
                dis[e[i].to]=dis[x]+1;
                q.push(e[i].to);
            }
        }
    }
    for(int i=0;i<=201;i++)if(dis[i]!=INF&&dis[i]>dis[res])res=i;
    return dis[res];
}
int main(){
    ans=0;
    scanf("%d",&n);
    memset(vis,0,sizeof(vis));
    for(int i=1;i<n;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        add(u,v);add(v,u);
    }
    for(int i=1;i<=n;i++){
        for(int j=h[i];j;j=e[j].nt){
            vis[i]=1;vis[e[j].to]=1;
            ans=max(ans,bfs(i)*bfs(e[j].to));
            vis[i]=0;vis[e[j].to]=0;
        }
    }
    printf("%d\n",ans);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值