zoukankan      html  css  js  c++  java
  • Two Paths--cf14D(树的直径)

    题目链接:http://codeforces.com/problemset/problem/14/D
    D. Two Paths
    time limit per test
    2 seconds
    memory limit per test
    64 megabytes
    input
    standard input
    output
    standard 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 <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <math.h>
    #include <stdlib.h>
    #include <vector>
    #include <queue>
    
    using namespace std;
    #define memset(a,b) memset(a,b,sizeof(a))
    #define N 250
    int G[N][N],vis[N],dis[N];
    int Index,Max,n;
    
    void bfs(int u)
    {
        Max=0;
        memset(vis,0);
        memset(dis,0);
        queue<int>Q;
        Q.push(u);
        vis[u]=1;
        while(!Q.empty())
        {
            int p;
            p=Q.front();
            Q.pop();
            for(int i=1; i<=n; i++)
            {
                if(G[p][i]!=0)
                {
                    if(!vis[i])
                    {
                        vis[i]=1;
                        Q.push(i);
                        dis[i]=dis[p]+1;
                        if(Max<dis[i])
                        {
                            Max=dis[i];
                            Index=i;
                        }
                    }
                }
            }
        }
    }
    
    int main()
    {
        int a[N],b[N];
        while(scanf("%d",&n)!=EOF)
        {
            memset(G,0);
            memset(a,0);
            memset(b,0);
            for(int i=1; i<n; i++)
            {
                scanf("%d %d",&a[i],&b[i]);
                G[a[i]][b[i]]=G[b[i]][a[i]]=1;
            }
            int MM=0;
            for(int i=1; i<n; i++)
            {
                int x,y;
                G[a[i]][b[i]]=G[b[i]][a[i]]=0;
                Index=0;
                bfs(a[i]);
                bfs(Index);
                x=Max;
    
                Index=0;
                bfs(b[i]);
                bfs(Index);
                y=Max;
                MM=max(MM,x*y);
                G[a[i]][b[i]]=G[b[i]][a[i]]=1;
            }
            printf("%d
    ",MM);
        }
        return 0;
    }
  • 相关阅读:
    char*”类型的值不能用于初始化“LPTSTR , Const char*”类型的值不能用于初始化“LPCTSTR
    LPCTSTR和LPTSTR和char *
    C++ char*,const char*,string的相互转换(转)
    vs2017 开发 C++ 操作mysql的动态库
    VS2017 创建C++Dll动态库(二)
    VS2017中托管C++程序调用托管C++生成的动态库,程序无法调试的问题(转)
    win10 MySQLroot 远程连接
    c++中c_str()的用法详解(转)
    【605】Python的开发环境相关 (不同版本python、pip)
    【604】Python class __dict__.update的使用
  • 原文地址:https://www.cnblogs.com/linliu/p/5279594.html
Copyright © 2011-2022 走看看