CF 14 problem D - Two paths

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值