MOBILE2 - Mobiles

no tags 

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的更多相关文章

  1. 正睿OI DAY3 杂题选讲

    正睿OI DAY3 杂题选讲 CodeChef MSTONES n个点,可以构造7条直线使得每个点都在直线上,找到一条直线使得上面的点最多 随机化算法,check到答案的概率为\(1/49\) \(n ...

  2. dp杂题(根据个人进度选更)

    ----19.7.30 今天又开了一个新专题,dp杂题,我依旧按照之前一样,这一个专题更在一起,根据个人进度选更题目; dp就是动态规划,本人认为,动态规划的核心就是dp状态的设立以及dp转移方程的推 ...

  3. wangkoala杂题总集(根据个人进度选更)

    CQOI2014 数三角形 首先一看题,先容斥一波,求出网格内选三个点所有的情况,也就是C(n*m,3);然后抛出行里三点共线的方案数:C(n,3)*m; 同理就有列中三点共线的方案数:n*C(m,3 ...

  4. 2019暑期金华集训 Day6 杂题选讲

    自闭集训 Day6 杂题选讲 CF round 469 E 发现一个数不可能取两次,因为1,1不如1,2. 发现不可能选一个数的正负,因为1,-1不如1,-2. hihoCoder挑战赛29 D 设\ ...

  5. Atcoder&CodeForces杂题11.7

    Preface 又自己开了场CF/Atcoder杂题,比昨天的稍难,题目也更有趣了 昨晚炉石检验血统果然是非洲人... 希望这是给NOIP2018续点rp吧 A.CF1068C-Colored Roo ...

  6. Codeforces 杂题集 2.0

      记录一些没有写在其他随笔中的 Codeforces 杂题, 以 Problemset 题号排序   1326D2 - Prefix-Suffix Palindrome (Hard version) ...

  7. 【Java面试】-- 杂题

    杂题 2019-11-03  21:09:37  by冲冲 1.类加载器的双亲委派机制 类加载器:把类通过类加载器加载到JVM中,然后转换成class对象(通过类的全路径来找到这个类). 双亲委派机制 ...

  8. 贪心/构造/DP 杂题选做Ⅱ

    由于换了台电脑,而我的贪心 & 构造能力依然很拉跨,所以决定再开一个坑( 前传: 贪心/构造/DP 杂题选做 u1s1 我预感还有Ⅲ(欸,这不是我在多项式Ⅱ中说过的原话吗) 24. P5912 ...

  9. 贪心/构造/DP 杂题选做Ⅲ

    颓!颓!颓!(bushi 前传: 贪心/构造/DP 杂题选做 贪心/构造/DP 杂题选做Ⅱ 51. CF758E Broken Tree 讲个笑话,这道题是 11.3 模拟赛的 T2,模拟赛里那道题的 ...

随机推荐

  1. 关于移动端的Click事件

    在移动端执行Click事件,通常情况出现有300毫秒的延迟,为防止这种不必要的延迟效果,我们可以换种方式来实现,同样达到快速执行Click事件的效果. 先了解一下移动端Click的执行顺序: touc ...

  2. 全国OA系统下载地址(全)

    思道OAhttp://www.anyoffice.net微软.NET平台,支持64位 金和OAhttp://www.jinher.com 红帆OAhttp://www.ioffice.cn 致远OAh ...

  3. KAFKA分布式消息系统[转]

    KAFKA分布式消息系统  转自:http://blog.chinaunix.net/uid-20196318-id-2420884.html Kafka[1]是linkedin用于日志处理的分布式消 ...

  4. linux指令备份

    ls -a 显示隐藏文件 cd 回到当前用户的目录 /home/ubuntu touch 创建文件 cat Hello.javamore/less Hello.java分页显示 grep root / ...

  5. 寻找链表中倒数第K个结点的位置

    输入一个链表,输出该链表中倒数第K个结点. struct ListNode { int m_nValue; ListNode* m_pNext; }; ListNode* FindKthToTail( ...

  6. WHU 1572 Cyy and Fzz (AC自动机 dp )

    题意: 给出n个串,求任意长度为m的字符串包含串的个数的期望.(n<=8,m<=14,给定串的长度不超过12). Solution: 首先可以想到应该用概率DP,我们需要至少3维,dp[i ...

  7. YII 验证功能

    YII 表单的验证可以在module目录下的xxx.php里面定义验证方法,设定需要验证的字段就行 //用户表单验证,在模型里面设置一个方法,具体表单验证规则        public functi ...

  8. RabbitMQ笔记

    RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. MQ全称为Message Queue, 消息队列(MQ)是一种应用程序 ...

  9. EJB

    Enterprise JavaBean,企业级javabean,是J2EE的一部分,定义了一个用于   开发基于组件的企业多重应用程序的标准.其特点包括网络服务支持和核心开发工具(SDK). 是Jav ...

  10. thinkphp3.2.2前后台公用类架构问题

    3.13之前好多项目都使用前后台公用类,在lib/action下创建Baseaction做为公共继承类,发现3.2.2里面很多人都用A调用,这样每用一次要用A调用,好麻烦,小编特意偷懒.亲测使用以下方 ...