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.)
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 | |||||||||||||
|
| ||||||||||||
Limits | |||||||||||||
|
| ||||||||||||
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) | |||||||||||||
|
| ||||||||||||
1) | |||||||||||||
|
| ||||||||||||
2) | |||||||||||||
|
| ||||||||||||
3) | |||||||||||||
|
|
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;
}
};