SRM 591 1A 2014.5.26
2016-02-13 20:11
399 查看
SRM 591 1A 2014.5.26
DIV1
250
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;
}
};
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 | ||||||||||||
| ||||||||||||
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) | ||||||||||||
|
#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;
}
};
相关文章推荐
- hdu1035Robot Motion
- SRM 593 1A 2013.12.10
- 安卓开发——指定Button的样式
- SRM 592 1A 2013.12.10
- 数据结构:哈夫曼树,哈夫曼编码与译码系统
- 吕鑫MFC学习系列八
- SRM 595 1A 2013.12.9
- SRM 594 1A 2013.12.10
- 【VB】On Error略解
- 于是就开始第二课了_(:зゝ∠)_。类的继承等
- SRM 596 1A 2013.12.9
- sublime
- 封装arp数据包并发送出去所遇到的问题
- 使用xib时要注意的细节
- SRM 597 1A 2013.12.6
- 婚礼现场布置应该注意什么
- 自定义Dialog 让Activity 不变暗 正在加载动画
- SRM 599 1A 2013.12.6
- [UVA455]Periodic Strings
- SRM 153 1A 2013.12.5