杂题 SPOJ MOBILE2 - Mobiles
MOBILE2 - Mobiles
You have been asked to buy a gift for your baby brother, Ike. However, you have noticed that Ike has a very particular taste in gifts. He only likes gifts that are configured in his particular style.
You have found a shop that sells mobiles. A mobile is a multi-layered decoration that is typically hung from the roof. Each mobile consists of a series of horizontal rods connected by vertical wires. Each rod has a wire hanging from both ends, which holds either another horizontal rod or a toy.
A sample mobile is shown below:
To satisfy your brother, you need to find a mobile that can be reconfigured so that:
(i) any two toys are either at the same level (that is, joined to the roof by the same number of rods), or di.er by only one level;
(ii) for any two toys that differ by one level, the toy to the left is further down than the toy to the right.
Mobiles can be reconfigured by performing swaps. A swap involves taking some rod, unhooking whatever is hanging beneath the left and right ends, and reattaching them at opposite ends (that is, the right and left ends respectively). This process does not modify the ordering of any rods or toys further down.
Since you are training for the Informatics Olympiad, you decide to write a program to test whether a given mobile can be reconfigured into a gift that Ike will like!
As an example, consider the mobile illustrated earlier. Ike will not like this mobile. Although it satisfies condition (i), it breaks condition (ii) — the toy at the leftmost end is at a higher level than the toys to its right.
However, the mobile can be reconfigured into a mobile that Ike will like. The following swaps are required:
1. First, the left and right ends of rod 1 are swapped. This exchanges the positions of rods 2 and 3, resulting in the following configuration:
2. Second, and finally, the left and right ends of rod 2 are swapped. This moves rod 4 to the left end of rod 2, and the toy to the right end of rod 2.
It can be seen that this final mobile satisfies Ike's requirements. All toys are at most one level apart, and the toys at a lower level are further to the left than the toys at a higher level.
Your task is, given a description of a mobile, to determine the smallest number of swaps required to reconfigure it so that Ike will like it (if this is possible). You may assume that the toys can never get in each other's way.
Input
Multiple test cases, the number of them will be given at the very first line.
For each test case:
The first line of input will contain the single integer n (1 <= n <= 100 000) representing the number of rods in the mobile. The rods are numbered 1, 2 , ..., n.
The following n lines will describe the connections for each rod. Specifically, the ith of these lines will describe rod i. Each of these lines will contain two integers l r separated by a single space, indicating what is hung beneath the left and right ends of the rod respectively. If a toy is hung beneath this rod, the corresponding integer l or r will be -1. Otherwise the integer l or r will be the number of a rod that is hung beneath this rod.
If there are any rods hanging beneath rod i, these rods will have numbers strictly greater than i. Rod 1 is the single rod at the top of the mobile.
Output
Output should consist of a single line containing a single integer, giving the smallest number of swaps required to reconfigure the mobile according to Ike's constraints. If this is not possible, you should output the integer -1.
Example
Input:
1
6
2 3
-1 4
5 6
-1 -1
-1 -1
-1 -1 Output:
2
Warning: large input/output data,be careful with certain languages
今天考试就考了这道题目。
写了很久,最后还是写对了。
Windows下要注意递归会爆栈!!!!!(结果扣两分)
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn=;
int ls[maxn],rs[maxn];
int Max[maxn],Min[maxn],dep[maxn];
int n;
bool DFS(int node,int d)
{
if(d>)return false;
if(node==-)return true;
bool OK=true;
dep[node]=d;
OK&=DFS(ls[node],d+);
OK&=DFS(rs[node],d+);
Max[node]=max(Max[ls[node]],Max[rs[node]])+;
Min[node]=min(Min[ls[node]],Min[rs[node]])+;
}
int ans;
int DFS2(int node)
{
if(dep[node]==Min[])
{
if(ls[node]==-&&rs[node]==-)
return ;
else if(ls[node]!=-&&rs[node]!=-)
return ;
else {
if(ls[node]==-)ans++;
return -;
}
}
int d1=DFS2(ls[node]);
int d2=DFS2(rs[node]);
if(d1==-&&d2==||d1==&&d2==||d1==&&d2==-)ans++;
if(d1==-||d2==-||d1==-&&d2==-)return -;
if(d1==d2)return d1;
else return -;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d%d",&ls[i],&rs[i]);
int tot=;
if(!DFS(,)||Max[]-Min[]>||DFS2()==-){
printf("-1\n");
return ;
} printf("%d\n",ans);
return ;
}
杂题 SPOJ MOBILE2 - Mobiles的更多相关文章
- 正睿OI DAY3 杂题选讲
正睿OI DAY3 杂题选讲 CodeChef MSTONES n个点,可以构造7条直线使得每个点都在直线上,找到一条直线使得上面的点最多 随机化算法,check到答案的概率为\(1/49\) \(n ...
- dp杂题(根据个人进度选更)
----19.7.30 今天又开了一个新专题,dp杂题,我依旧按照之前一样,这一个专题更在一起,根据个人进度选更题目; dp就是动态规划,本人认为,动态规划的核心就是dp状态的设立以及dp转移方程的推 ...
- wangkoala杂题总集(根据个人进度选更)
CQOI2014 数三角形 首先一看题,先容斥一波,求出网格内选三个点所有的情况,也就是C(n*m,3);然后抛出行里三点共线的方案数:C(n,3)*m; 同理就有列中三点共线的方案数:n*C(m,3 ...
- 2019暑期金华集训 Day6 杂题选讲
自闭集训 Day6 杂题选讲 CF round 469 E 发现一个数不可能取两次,因为1,1不如1,2. 发现不可能选一个数的正负,因为1,-1不如1,-2. hihoCoder挑战赛29 D 设\ ...
- Atcoder&CodeForces杂题11.7
Preface 又自己开了场CF/Atcoder杂题,比昨天的稍难,题目也更有趣了 昨晚炉石检验血统果然是非洲人... 希望这是给NOIP2018续点rp吧 A.CF1068C-Colored Roo ...
- Codeforces 杂题集 2.0
记录一些没有写在其他随笔中的 Codeforces 杂题, 以 Problemset 题号排序 1326D2 - Prefix-Suffix Palindrome (Hard version) ...
- 【Java面试】-- 杂题
杂题 2019-11-03 21:09:37 by冲冲 1.类加载器的双亲委派机制 类加载器:把类通过类加载器加载到JVM中,然后转换成class对象(通过类的全路径来找到这个类). 双亲委派机制 ...
- 贪心/构造/DP 杂题选做Ⅱ
由于换了台电脑,而我的贪心 & 构造能力依然很拉跨,所以决定再开一个坑( 前传: 贪心/构造/DP 杂题选做 u1s1 我预感还有Ⅲ(欸,这不是我在多项式Ⅱ中说过的原话吗) 24. P5912 ...
- 贪心/构造/DP 杂题选做Ⅲ
颓!颓!颓!(bushi 前传: 贪心/构造/DP 杂题选做 贪心/构造/DP 杂题选做Ⅱ 51. CF758E Broken Tree 讲个笑话,这道题是 11.3 模拟赛的 T2,模拟赛里那道题的 ...
随机推荐
- 怎样写好一份IT技术岗位的简历
10月是校园招聘的旺季,很多应届毕业生都忙碌起来了,从CSDN笔试-面试文章的火热程度,从我收到的简历就看得出来. 我很久没有参与笔试和面试了,所以只能从“简历”来阐述下我的看法. 截至目前,已经帮8 ...
- JavaScript正则验证数字、英文、电话号、身份证号、邮箱地址、链接地址等
验证是否为数字:/^[0-9]*$/验证是否为汉字:/^[\u4e00-\u9fa5],{0,}$/验证x-y位的数字:/^\d{x,y}$/验证由26个英文字母组成的字符串:/^[A-Za-z]+$ ...
- OpenGL ES 3.0 点,线,三角形绘制形式总结
OpenGL ES 3.0 顶点 -1, 1, 0, -0.5f, 0, 0, 0, -1, 0, -1, 0, 0, 0.5f, 0, 0, 1, -1, ...
- Android开发需要注意的地方
1.理解运用商场概略 开发者对商场状况的理解与APP的胜利紧密相连,往常,AppStore和GooglePlay能够说是挪动运用最为丰厚的运用生态,像苹果的下载计算表单会记载抢手运用的下载 ...
- oracle数据表误删恢复
1.查看回收站中的表: select object_name,original_name,partition_name,type,ts_name,createtime,droptime from re ...
- 在万网虚拟主机上部署MVC5
参考 要想部署mvc,需要把一些mvc用到的全局程序集改为本地部署,通过N次试验,终于搞定. 特写个备忘录,免得以后忘了. 首先更改web.config,在里面加上 <system.web> ...
- mahout的安装、配置及运行java程序
一.下载安装包: http://mahout.apache.org/general/downloads.html 二.解压: 将下载的安装包解压到需要的目录下 三.配置环境变量: export MAH ...
- 第三篇、调优之路 Apache调优
1. 简介 在第一篇中整合了apache + tomcat ,利用了apache解析静态文件为tomcat解压.但是在测试机上发现两者性能不足,不能充分利用服务器的性能,该篇中将对apache进行性 ...
- php之文件上传类代码
/* 单个文件上传 功能 上传文件 配置允许的后缀 配置允许的大小 获取文件后缀 判断文件的后缀 报错 */ class UpTool{ protected $allowExt = 'jpg,jpeg ...
- [Winfrom] 捕获窗体最大化、最小化和关闭按钮的事件
const int WM_SYSCOMMAND = 0x112;const int SC_CLOSE = 0xF060;const int SC_MINIMIZE = 0xF020;const int ...