TopCoder SRM 591 DIV1 275

Problem Statement

    Manao is working in the Tree Research Center. It may come as a surprise that the trees they research are not the ones you can see in a park. Instead, they are researching special graphs. (See Notes for definitions of terms related to these trees.)

Manao's daily job is reconstructing trees, given some partial information about them. Today Manao faced a difficult task. He needed to find the maximum possible diameter of a tree, given the following information:
  • Some vertex in the tree is called V.
  • The distance between V and the farthest vertex from V is D.
  • For each x between 1 and D, inclusive, Manao knows the number of vertices such that their distance from V is x.
You are given a vector <int> cnt containing D strictly positive integers. For each i, the i-th element of cnt is equal to the number of vertices which have distance i+1 from V. Please help Manao with his task. Return the maximum possible diameter of a tree that matches the given information.

Definition

    
Class:TheTree
Method:maximumDiameter
Parameters:vector <int>
Returns:int
Method signature:int maximumDiameter(vector <int> cnt)
(be sure your method is public)

Limits

    
Time limit (s):2.000
Memory limit (MB):64

Notes

-A tree is a connected graph with no cycles. Note that each tree with N vertices has precisely N-1 edges.
-The distance between two vertices of a tree is the smallest number of edges one has to traverse in order to get from one of the vertices to the other one.
-The diameter of a tree is the maximum of all pairwise distances. In other words, the diameter is the distance between the two vertices that are the farthest away from each other.

Constraints

-cnt will contain between 1 and 50 elements, inclusive.
-Each element of cnt will be between 1 and 1000, inclusive.

Examples

0) 
    
{3}
Returns: 2
The only tree that matches the given information is shown below. The vertex V is red.

1) 
    
{2, 2}
Returns: 4
There are two trees which correspond to the given partial information:



The tree on the left has diameter 3 and the tree on the right has diameter 4.
2) 
    
{4, 1, 2, 4}
Returns: 5
This is one example of a tree that corresponds to the given constraints and has the largest possible diameter.

3) 
    
{4, 2, 1, 3, 2, 5, 7, 2, 4, 5, 2, 3, 1, 13, 6}
Returns: 21
 

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

题目描述:给你一组数cnt[ ],cnt[i]表示第i+1层有几个节点。问你树的直径最大为多少?

思路:我们考虑一下,当第i层只有1个节点时,接下来的最大直径只能是 deep(层数)-i+ans(已计算过的直径)。否则直径的长度可以加2。

下面是代码:

#include <bits/stdc++.h>
using namespace std;
int ans;
class TheTree {
public:
    int maximumDiameter( vector <int> cnt );
};
int TheTree::maximumDiameter(vector <int> cnt) {
    ans=0;
    for(int i=0;i<=cnt.size()-1;i++)
    {
    	int ret=0;
    	for(int j=i;j<=cnt.size()-1;j++)
		{
			if(cnt[j]==1){ret+=cnt.size()-j;break;}
			ret+=2;
		}
		ans=max(ans,ret);
	}
	return ans;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值