zoukankan      html  css  js  c++  java
  • Codeforces Beta Round #14 (Div. 2) Two Paths (树形DP)

    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
    【题意】给你一棵树,要求找到两条链,使得乘积最大。
    【分析】树形DP,对于每个节点维护两条最长链即可。
    #include <bits/stdc++.h>
    #define inf 0x3f3f3f3f
    #define met(a,b) memset(a,b,sizeof a)
    #define pb push_back
    #define mp make_pair
    #define rep(i,l,r) for(int i=(l);i<=(r);++i)
    #define inf 0x3f3f3f3f
    using namespace std;
    typedef long long ll;
    const int N = 1e5+5;;
    const int M = 17;
    const int mod = 1e9+7;
    const int mo=123;
    const double pi= acos(-1.0);
    typedef pair<int,int>pii;
    int n,s;
    int x[N],y[N];
    vector<int>edg[N];
    int dfs(int u,int fa){
        int mx1=0,mx2=0,ret=0;
        for(int v : edg[u]){
            if(v==fa)continue;
            ret=max(ret,dfs(v,u));
            if(s>mx1)mx2=mx1,mx1=s;
            else mx2=max(mx2,s);
        }
        ret=max(ret,mx1+mx2);
        s=mx1+1;
        return ret;
    }
    int main(){
        scanf("%d",&n);
        for(int i=1;i<n;i++){
            scanf("%d%d",&x[i],&y[i]);
            edg[x[i]].pb(y[i]);
            edg[y[i]].pb(x[i]);
        }
        int ans=0;
        rep(i,1,n-1){
            int a=dfs(x[i],y[i]);
            int b=dfs(y[i],x[i]);
            ans=max(ans,a*b);
        }
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    几种垃圾回收算法
    Web前端研发工程师编程能力飞升之路
    Visual Studio使用小技巧6 – 为代码加上Using(Resolve using)和管理Using(Organize using) (转载)
    ASP.NET Session的七点认识
    经典SQL语句大全(转载)
    3种类型的程序员:“虫族”,“人族”,“神族”(转载)
    数据库设计中的14个技巧(转载)
    导致Asp.net站点重启的10个原因(转载)
    jQuery前端数据通用验证库,解放你的双手
    关于团队执行力的思考
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/7349064.html
Copyright © 2011-2022 走看看