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;}
0 0
原创粉丝点击