SRM 591 1A 2014.5.26

SRM 591 1A 2014.5.26

DIV1

250

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

Code:

#include<iostream>

#include<string>

#include<vector>

 

using namespace std;

 

class TheTree

{

public: int maximumDiameter(vector<int> cnt)

         {

                            intans=0;

                            for(inti=0;i<cnt.size();i++)

                            {

                                     if(cnt[i]>=1)

                                     {

                                               ans++;

                                               cnt[i]--;

                                     }

                                     elsebreak;

                            }

                            for(inti=0;i<cnt.size();i++)

                            {

                                     if(cnt[i]>=1)

                                     {

                                               ans++;

                                               cnt[i]--;

                                     }

                                     elsebreak;

                            }

                            returnans;

         }

};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值