zoukankan      html  css  js  c++  java
  • codeforces --- 14D

    C - Two Paths
    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    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

    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

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 const int MAX = 205;
     6 int dep, n, map[MAX][MAX];
     7 int dfs(int u, int v){
     8     int max1, max2, path_max;
     9     max1 = max2 = path_max = 0;
    10     for(int i = 1;i <= n;i ++){
    11         if(map[i][u] && i != v){
    12             int z = dfs(i, u);
    13             if(path_max < z) path_max = z;
    14             if(max1 < dep){
    15                 max2 = max1;
    16                 max1 = dep;
    17             }
    18             else if(max2 < dep) max2 = dep;
    19         }
    20     }
    21     if(path_max < max1 + max2) path_max = max2 + max1;
    22     dep = max1 + 1;
    23     return path_max;
    24 }
    25 
    26 int main(int argc, char const *argv[]){
    27     int u, v;
    28     //freopen("in.c", "r", stdin);
    29     while(~scanf("%d", &n)){
    30         memset(map, 0, sizeof(map));
    31         for(int i = 0; i < n - 1;i ++){
    32             scanf("%d%d", &u, &v);
    33             map[u][v] = map[v][u] = 1;
    34         }
    35         int ans = -1;
    36         for(int i = 1;i <= n;i ++){
    37             for(int j = 1;j <= n;j ++){
    38                 if(map[i][j]){
    39                     int a = dfs(i, j);
    40                     int b = dfs(j, i);
    41                     ans = max(ans, a*b);
    42                 }
    43             }
    44         }
    45         printf("%d
    ", ans);
    46     }
    47     return 0;
    48 }
  • 相关阅读:
    主席树学习记录
    P1072 Hanson 的趣味题 题解
    好文章收集
    计算几何专题
    小问题
    CSP-S2020题解
    上下界网络流
    想到的无法解决的点子
    省选联考2020组合数问题
    省选数学复习
  • 原文地址:https://www.cnblogs.com/anhuizhiye/p/3606087.html
Copyright © 2011-2022 走看看