杂题 SPOJ MOBILE2

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下要注意递归会爆栈!!!!!(结果扣两分)

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 const int maxn=100010;
 5 int ls[maxn],rs[maxn];
 6 int Max[maxn],Min[maxn],dep[maxn];
 7 int n;
 8 bool DFS(int node,int d)
 9 {
10     if(d>100)return false;
11     if(node==-1)return true;
12     bool OK=true;
13     dep[node]=d;
14     OK&=DFS(ls[node],d+1);
15     OK&=DFS(rs[node],d+1);
16     Max[node]=max(Max[ls[node]],Max[rs[node]])+1;
17     Min[node]=min(Min[ls[node]],Min[rs[node]])+1;
18 }
19 int ans;
20 int DFS2(int node)
21 {
22     if(dep[node]==Min[1])
23     {
24         if(ls[node]==-1&&rs[node]==-1)
25             return 0;
26         else if(ls[node]!=-1&&rs[node]!=-1)
27             return 1;
28         else {
29             if(ls[node]==-1)ans++;
30             return -1;
31         }
32     }
33     int d1=DFS2(ls[node]);
34     int d2=DFS2(rs[node]);
35     if(d1==-1&&d2==1||d1==0&&d2==1||d1==0&&d2==-1)ans++;
36     if(d1==-2||d2==-2||d1==-1&&d2==-1)return -2;
37     if(d1==d2)return d1;
38     else return -1;
39 }
40 int main()
41 {
42     scanf("%d",&n);
43     for(int i=1;i<=n;i++)
44         scanf("%d%d",&ls[i],&rs[i]);
45     int tot=0;
46     if(!DFS(1,1)||Max[1]-Min[1]>1||DFS2(1)==-2){
47         printf("-1
");
48         return 0;
49     }
50         
51     printf("%d
",ans);
52     return 0;
53 }
尽最大的努力,做最好的自己!
原文地址:https://www.cnblogs.com/TenderRun/p/5251171.html