zoukankan      html  css  js  c++  java
  • Codeforces Beta Round #14 (Div. 2) D. Two Paths 树的直径

    题目链接:

    http://codeforces.com/contest/14/problem/D

    D. Two Paths

    time limit per test2 seconds
    memory limit per test64 megabytes
    #### 问题描述 > 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. #### 输入 > 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 the maximum possible profit. ####样例输入 > 6 > 1 2 > 2 3 > 2 4 > 5 4 > 6 4 ####样例输出 > 4

    题意

    给你一颗树,找两条不相交不重叠的路径,使得它们的乘积最大。

    题解

    枚举删哪条边,然后分别跑树的直径

    代码

    #include<map>
    #include<set>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<ctime>
    #include<vector>
    #include<cstdio>
    #include<string>
    #include<bitset>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<functional>
    using namespace std;
    #define X first
    #define Y second
    #define mkp make_pair
    #define lson (o<<1)
    #define rson ((o<<1)|1)
    #define mid (l+(r-l)/2)
    #define sz() size()
    #define pb(v) push_back(v)
    #define all(o) (o).begin(),(o).end()
    #define clr(a,v) memset(a,v,sizeof(a))
    #define bug(a) cout<<#a<<" = "<<a<<endl
    #define rep(i,a,b) for(int i=a;i<(b);i++)
    #define scf scanf
    #define prf printf
    
    typedef long long LL;
    typedef vector<int> VI;
    typedef pair<int,int> PII;
    typedef vector<pair<int,int> > VPII;
    
    const int INF=0x3f3f3f3f;
    const LL INFL=0x3f3f3f3f3f3f3f3fLL;
    const double eps=1e-8;
    const double PI = acos(-1.0);
    
    //start----------------------------------------------------------------------
    
    const int maxn=222;
    
    struct Edge{
        int u,v,ne,flag;
        Edge(int u,int v,int ne):u(u),v(v),ne(ne),flag(0){}
        Edge(){}
    }egs[maxn*2];
    
    int n;
    int head[maxn],tot;
    
    void addEdge(int u,int v){
        egs[tot]=Edge(u,v,head[u]);
        head[u]=tot++;
    }
    
    void dfs(int u,int fa,int dep,int& ret,int& ma){
        if(dep>ma){ ret=u,ma=dep; }
        int p=head[u];
        while(p!=-1){
            Edge& e=egs[p];
            if(!e.flag&&e.v!=fa){
                dfs(e.v,u,dep+1,ret,ma);
            }
            p=e.ne;
        }
    }
    
    int solve(int u){
        int ret=0;
        int v=-1,ma=-1;
        dfs(u,-1,0,v,ma);
        u=v;
        v=-1,ma=-1;
        dfs(u,-1,0,v,ma);
        return ma;
    }
    
    void init(){
        clr(head,-1);
        tot=0;
    }
    
    int main() {
        scf("%d",&n);
        init();
        rep(i,0,n-1){
            int u,v;
            scf("%d%d",&u,&v);
            addEdge(u,v);
            addEdge(v,u);
        }
        int ans=0;
        for(int i=0;i<tot;i+=2){
            egs[i].flag=egs[i^1].flag=1;
            int x=solve(egs[i].u);
            int y=solve(egs[i].v);
            ans=max(ans,x*y);
            egs[i].flag=egs[i^1].flag=0;
        }
        prf("%d
    ",ans);
        return 0;
    }
    
    //end-----------------------------------------------------------------------
  • 相关阅读:
    [Linux]Ubuntu下正确挂载NTFS磁盘的方法
    Google搜索指令与自定义引擎
    【Linux】Android手机在Ubuntu上无法被adb识别解决办法(权限相关)
    [ Linux ] Remove PPA source from your pc
    一键去除 UC浏览器 论坛模式 内置的广告
    Huawei U8825d 对4G手机内存重新分区过程[把2Gb内置SD卡容量划分给DATA分区使用]
    【Android】把Linux GCC安插在Android手机上
    [Windows]隐藏文件及目录的命令
    Linux压缩包简体中文乱解决方案[全]
    修改su密码 macbook
  • 原文地址:https://www.cnblogs.com/fenice/p/5845396.html
Copyright © 2011-2022 走看看