Codeforces Beta Round #14 (Div. 2) D. Two Paths 树的直径

题目链接:

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

D. Two Paths


time limit per test2 secondsmemory 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\n",ans);
    return 0;
}

//end-----------------------------------------------------------------------

转载于:https://www.cnblogs.com/fenice/p/5845396.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值