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 }
  • 相关阅读:
    Struts2-Spring和Hibernate整合
    windows下使用Eclipse编译执行MapReduce程序 Hadoop2.6.0/Ubuntu
    Android 基于Netty的消息推送方案之对象的传递(四)
    基于JQuery实现表单元素值的回写
    iOS 从UITableViewController中分离数据源
    navicat premium 的使用——navicat 连接MySQL数据库
    【甘道夫】Ubuntu14 server + Hadoop2.2.0环境下Sqoop1.99.3部署记录
    罗永浩和锤子手机:对不起,我被你打动了
    用C语言解决迷宫问题
    Android利用reative_layout生成梅花界面
  • 原文地址:https://www.cnblogs.com/anhuizhiye/p/3606087.html
Copyright © 2011-2022 走看看