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
今天考试就考了这道题目。
写了很久,最后还是写对了。
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 void DFS(int node,int d) 9 { 10 if(node==-1)return; 11 dep[node]=d; 12 DFS(ls[node],d+1); 13 DFS(rs[node],d+1); 14 Max[node]=max(Max[ls[node]],Max[rs[node]])+1; 15 Min[node]=min(Min[ls[node]],Min[rs[node]])+1; 16 } 17 int ans; 18 int DFS2(int node) 19 { 20 if(dep[node]==Min[1]) 21 { 22 if(ls[node]==-1&&rs[node]==-1) 23 return 0; 24 else if(ls[node]!=-1&&rs[node]!=-1) 25 return 1; 26 else { 27 if(ls[node]==-1)ans++; 28 return -1; 29 } 30 } 31 int d1=DFS2(ls[node]); 32 int d2=DFS2(rs[node]); 33 if(d1==-1&&d2==1||d1==0&&d2==1||d1==0&&d2==-1)ans++; 34 if(d1==-2||d2==-2||d1==-1&&d2==-1)return -2; 35 if(d1==d2)return d1; 36 else return -1; 37 } 38 int main() 39 { 40 scanf("%d",&n); 41 for(int i=1;i<=n;i++) 42 scanf("%d%d",&ls[i],&rs[i]); 43 DFS(1,1); 44 45 int tot=0; 46 if(Max[1]-Min[1]>1||DFS2(1)==-2){ 47 printf("-1\n"); 48 return 0; 49 } 50 51 printf("%d\n",ans); 52 return 0; 53 }