Problem Statement

    

You are a genealogist specializing in family trees of vampires. Vampire family trees differ from human family trees. In particular, vampires are "born" in a different way. The only way to create a new vampire is that an existing vampire turns a living human into a new vampire. Whenever this happens, we say that the older vampire is the master and the newly created vampire is the servant of that master.

Given a particular family of vampires, the distance between two vampires is the smallest number of steps along the family tree we need to make in order to get from one vampire to the other. Formally, in each step you can move from the current vampire to any of its servants, or to its master (if it has one). Note that for each vampire V the distance between V and V is zero.

You are now studying one particular family of vampires. These vampires have all been created from a single vampire: the True Ancestor. This special vampire has no master. You know that there are n vampires in the family, and you have numbered them 0 through n-1 (in no particular order).

You do not know the master/servant relationships between the vampires in the family. The only information you have is a vector <int> num with n elements. For each valid i, the following statement is true: "If vampire i is the True Ancestor, he has exactly num[i] servants. Otherwise, he has exactly (num[i] - 1) servants."

Consider all valid family trees that are consistent with this information. If there are no such trees, return -1. Otherwise, find and return the maximum distance between any two vampires in any of those family trees. (In other words, for each of the corresponding trees determine the maximum distance, and return the maximum of those maximums.)

Definition

    
Class: VampireTree
Method: maxDistance
Parameters: vector <int>
Returns: int
Method signature: int maxDistance(vector <int> num)
(be sure your method is public)

Limits

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

Constraints

- num will contain between 2 and 20 elements, inclusive.
- Each element of num will be between 1 and n-1, inclusive.

Examples

0)  
    
{1, 2, 1}
Returns: 2
One possible solution is that vampire 1 is the True Ancestor, and vampires 0 and 2 are its servants.
1)  
    
{2, 2, 2}
Returns: -1
At least two of the vampires must have two servants, but there needs to be at least 5 vampires for such a situation to happen (excluding the True Ancestor).
2)  
    
{1, 1, 1, 1, 4}
Returns: 2
 
3)  
    
{1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
Returns: -1
 

题意:告诉每个点有几个儿子,如果是根节点,有num[i]个,否则有num[i-1]个.问这样的树是否存在,若存在,问树上两点的最大距离

分析:这里的num[i]就相当于每个结点的度数,有个性质:deg = 2 * n - 2可以判断树是否存在.然后最大距离一定是在两个叶子结点产生,因为树可以变化,总可以找到除了其他子节点外,其他所有点都在最大距离的路径里,答案就是 1 + cnt (num[i] > 1)的个数.我还傻傻的直接构造一棵树求LCA.....,但是好像还留在div1:)

官方题解

class VampireTree {
public:
int maxDistance( vector <int> num ) {
int n = num.size ();
int deg = 0, ans = 1;
for (int i=0; i<n; ++i) {
deg += num[i];
if (num[i] > 1) ans++;
}
if (deg != 2 * n - 2) return -1;
else return ans;
}
};

  

图论 SRM 674 Div1 VampireTree 250的更多相关文章

  1. 状态压缩DP SRM 667 Div1 OrderOfOperations 250

    Problem Statement      Cat Noku has just finished writing his first computer program. Noku's compute ...

  2. Topcoder SRM 643 Div1 250<peter_pan>

    Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...

  3. Topcoder Srm 726 Div1 Hard

    Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...

  4. SRM 595 DIV1 250

    挺简单的组合把. #include <cstdio> #include <cstring> #include <iostream> #include <vec ...

  5. SRM 594 DIV1 250

    可能开始宿舍比较乱,思绪静不下来...想了大半个小时,终于确定了应该暴力+DP,然后写了枚举除数,和被除的版本..这样,还敲错了个字母,第一次提交还100多,修改提交还有75.多,最后想到,貌似不打对 ...

  6. TC SRM 593 DIV1 250

    我只能说的亏没做,要不就挂0了.. 本来想四色定理,肯定4就可以的...然后准备爆,发现3的时候不好爆,又想了老一会,嗯,数据范围不小,应该不是暴力,直接找规律,貌似最大就是3,有一个3连块,输出3, ...

  7. TC SRM 593 DIV1 250(dfs)

    这图最多3色就可以 搜2就行了 #include <iostream> #include<cstdio> #include<cstring> #include< ...

  8. 最小公倍数 SRM 661 Div1 250: MissingLCM

    Problem Statement The least common multiple (denoted "lcm") of a non-empty sequence of pos ...

  9. Topcoder SRM 698 Div1 250 RepeatString(dp)

    题意 [题目链接]这怎么发链接啊..... Sol 枚举一个断点,然后类似于LIS一样dp一波 这个边界条件有点迷啊..fst了两遍... #include<bits/stdc++.h> ...

随机推荐

  1. linux service命令常见使用方法

    service命令,顾名思义,就是用于管理Linux操作系统中服务的命令. 1. 声明:这个命令不是在所有的linux发行版本中都有.主要是在redhat.fedora.mandriva和centos ...

  2. mongoDB 3.0以前版本 - 入门指南、示例

    一.准备工作 1. 下载mongoDB 下载地址:http://www.mongodb.org/downloads 选择合适你的版本 相关文档:http://www.mongodb.org/displ ...

  3. CABasicAnimation 按home键后台之后,再切回来动画就停止了

    解决方法: 1. CABasicAnimation *thisAnimation = [CABasicAnimtaion animationWithKeyPath:@"transform.r ...

  4. php 投票系统练习

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. JVM的内存分配与垃圾回收策略

    自动内存管理机制主要解决了两个问题:给对象分配内存以及回收分配给对象的内存. >>垃圾回收的区域 前面的笔记中整理过虚拟机运行数据区,再看一下这个区域: 注意在这个Runtime Data ...

  6. Android消息推送怎么实现?

    在开发Android和iPhone应用程序时,我们往往需要从服务器不定的向手机客户端即时推送各种通知消息,iPhone上已经有了比较简单的和完美的推送通知解决方案,可是Android平台上实现起来却相 ...

  7. git branch用法总结

    git branch      git branch 不带参数:列出本地已经存在的分支,并且在当前分支的前面加“*”号标记,例如:   #git branch* master   newbranch ...

  8. js或jquery实现页面打印可局部打印

    方法一:直接用js的打印方法 <input id="btnPrint" type="button" value="打印" onclic ...

  9. 虚拟机通过NAT方式与主机、互联网通信

    1.首先配置物理主中机VMnet8的IP信息 主机物理IP为192.168.3.9

  10. FP-Growth算法及演示程序

    FP-Growth算法 FP-Growth(频繁模式增长)算法是韩家炜老师在2000年提出的关联分析算法,它采取如下分治策略:将提供频繁项集的数据库压缩到一棵频繁模式树(FP-Tree),但仍保留项集 ...