图论 SRM 674 Div1 VampireTree 250
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 |
|||||||||||||
|
|||||||||||||
Limits |
|||||||||||||
|
|||||||||||||
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) | |||||||||||||
|
|||||||||||||
3) | |||||||||||||
|
题意:告诉每个点有几个儿子,如果是根节点,有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的更多相关文章
- 状态压缩DP SRM 667 Div1 OrderOfOperations 250
Problem Statement Cat Noku has just finished writing his first computer program. Noku's compute ...
- Topcoder SRM 643 Div1 250<peter_pan>
Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...
- Topcoder Srm 726 Div1 Hard
Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...
- SRM 595 DIV1 250
挺简单的组合把. #include <cstdio> #include <cstring> #include <iostream> #include <vec ...
- SRM 594 DIV1 250
可能开始宿舍比较乱,思绪静不下来...想了大半个小时,终于确定了应该暴力+DP,然后写了枚举除数,和被除的版本..这样,还敲错了个字母,第一次提交还100多,修改提交还有75.多,最后想到,貌似不打对 ...
- TC SRM 593 DIV1 250
我只能说的亏没做,要不就挂0了.. 本来想四色定理,肯定4就可以的...然后准备爆,发现3的时候不好爆,又想了老一会,嗯,数据范围不小,应该不是暴力,直接找规律,貌似最大就是3,有一个3连块,输出3, ...
- TC SRM 593 DIV1 250(dfs)
这图最多3色就可以 搜2就行了 #include <iostream> #include<cstdio> #include<cstring> #include< ...
- 最小公倍数 SRM 661 Div1 250: MissingLCM
Problem Statement The least common multiple (denoted "lcm") of a non-empty sequence of pos ...
- Topcoder SRM 698 Div1 250 RepeatString(dp)
题意 [题目链接]这怎么发链接啊..... Sol 枚举一个断点,然后类似于LIS一样dp一波 这个边界条件有点迷啊..fst了两遍... #include<bits/stdc++.h> ...
随机推荐
- Java常用工具类题库
一. 填空题 在Java中每个Java基本类型在java.lang包中都在一个相应的包装类,把基本类型数据转换为对象,其中包装类Integer是___Number__的直接子类. 包装类Inte ...
- Linq查询
//Linq查询 List<A1> a1 = new List<A1>(); a1.Add(, Name = , Gender = true }); a1.Add(, Name ...
- PHP面向对象——异常处理
Error_reporting(0); //在网站正式上线的时候不准他报任何错误. 错误级别为不允许报错 Exception 是所有异常的基类. 测试并捕捉一个错误的例子: class mysq ...
- 微信支付 - V3支付问题
参考资料:http://www.2cto.com/weixin/201506/407690.html 1.微信公众号支付出错: 当前页面的URL未注册: get_brand_wcpay_reque ...
- MySQL 主从同步
http://blog.csdn.net/z69183787/article/details/53897894
- Shell编程基础教程5--文本过滤、正则表达式、相关命令
5.文本过滤.正则表达式.相关命令 5.1.正则表达式(什么是正则表达式?正则表达式怎么进行匹配?常用命令) 简介: 一种用来描述文本模式的特殊语法 ...
- python多线程之Condition(条件变量)
#!/usr/bin/env python # -*- coding: utf-8 -*- from threading import Thread, Condition import time it ...
- .NET NLog 详解(四) - filter
我们将版本向前切换到20051025,这期的关注点是filter.我们在使用日志的时候可能希望加上一些过滤器,在满足某些特定条件的时候才输出.举个简单的使用方式如下: <nlog> < ...
- Java后端WebSocket的Tomcat实现
转自:http://blog.chenzuhuang.com/archive/28.html 文章摘要随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随着HTML5 ...
- WPF初学(一)——布局【良好界面的基础】
由Winform转到WPF的一部分人,很可能忽略掉布局,习惯性的使用固定定位.然而,没有良好的布局,后面界面控件画的再好看,花哨,都不过是鲜花插在牛粪上,很可能始终都是一坨??(呵呵). 闲话少说,首 ...