CSES - APIO 2007 - Results
Submission details
Task:Mobiles
Sender:henrikaalto
Submission time:2019-03-07 15:09:33 +0200
Language:C++
Status:READY
Result:0
Feedback
groupverdictscore
#10
Test results
testverdicttime
#1ACCEPTED0.02 sdetails
#2ACCEPTED0.03 sdetails
#3ACCEPTED0.01 sdetails
#4ACCEPTED0.01 sdetails
#5ACCEPTED0.01 sdetails
#60.02 sdetails
#70.01 sdetails
#8ACCEPTED0.02 sdetails
#90.02 sdetails
#100.03 sdetails
#110.03 sdetails
#120.01 sdetails
#13ACCEPTED0.01 sdetails
#14ACCEPTED0.01 sdetails
#15ACCEPTED0.02 sdetails
#16ACCEPTED0.02 sdetails
#17ACCEPTED0.02 sdetails
#18ACCEPTED0.02 sdetails
#19ACCEPTED0.02 sdetails
#20ACCEPTED0.02 sdetails
#21ACCEPTED0.01 sdetails
#220.02 sdetails
#230.01 sdetails
#240.02 sdetails
#250.05 sdetails
#26ACCEPTED0.02 sdetails
#270.01 sdetails
#280.02 sdetails
#290.02 sdetails
#300.01 sdetails
#310.02 sdetails
#32ACCEPTED0.03 sdetails
#330.05 sdetails
#340.05 sdetails
#350.04 sdetails
#360.05 sdetails
#370.07 sdetails
#38ACCEPTED0.05 sdetails
#390.05 sdetails
#400.06 sdetails
#410.05 sdetails
#420.06 sdetails

Compiler report

input/code.cpp: In function 'int h1(int, int)':
input/code.cpp:41:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^

Code

#include<bits/stdc++.h>
using namespace std;

int d[101010][4];
int p[101010];
void fail() {
  cout <<-1;
  exit(0);
}
void h(int s,int asd) {
  if(d[s][0] != -1) h(d[s][0],asd+1);
  else p[asd+1]++;
  if(d[s][1] != -1) h(d[s][1],asd+1);
  else p[asd+1]++;
}

int h1(int s,int asd) {
  int mil = 1e9;
  int mix = 0;
  d[s][2] = 1e9;
  if(d[s][0] != -1) {
    h1(d[s][0],asd+1);
    mil = min(mil,d[d[s][0]][2]);
    mix = max(mix,d[d[s][0]][3]);
  }
  else {
    d[s][2] = min(d[s][2],asd+1);
    d[s][3] = max(d[s][3],asd+1);
  }
  if(d[s][1] != -1) {
    h1(d[s][1],asd+1); 
    mil = min(mil,d[d[s][1]][2]);
    mix = max(mix,d[d[s][1]][3]);
  }
  else {
    d[s][2] = min(d[s][2],asd+1);
    d[s][3] = max(d[s][3],asd+1);
  }
  d[s][2] = min(d[s][2],mil);
  d[s][3] = max(d[s][3],mix);
}

int steps = 0;

void h2(int s,int asd) {
  int leftmin = d[s][0]!=-1?d[d[s][0]][2]:asd+1;
  int leftmax = d[s][0]!=-1?d[d[s][0]][3]:asd+1;
  
  int rmin = d[s][1]!=-1?d[d[s][1]][2]:asd+1;
  int rmax = d[s][1]!=-1?d[d[s][1]][3]:asd+1;
//  cout << s << " " << rmin << " " << rmax << " | " << leftmin << " " << leftmax << "\n";
//  cout << "      " << s << " -> " << d[s][0] << " <> " << d[s][1] << "\n";
  if(leftmin<rmin) {
    if(leftmax > rmax) fail();
    swap(d[s][0],d[s][1]);
    steps++;
  }
  if(d[s][0] != -1){
    h2(d[s][0],asd+1);
  }
  if(d[s][1] != -1){
    h2(d[s][1],asd+1);
  }
}

int ok = 1;

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0);
  int n; cin >> n;
  for(int i=1;i<=n;i++) {
    cin >> d[i][0] >> d[i][1];
  }
  h(1,0);
  int l = 0;
  for(int i=1;i<=n;i++) {
    if(p[i] && l && l+1!=i) fail();
    if(p[i]) l = i;
  }
  // sort the binary tree
  // sort by every toy's level
  // 1. calculate maximum (& minumum) for every sub tree
  // 2. swap the subtrees
  
  h1(1,1); h2(1,1); 
  cout << steps <<"\n";
}

Test details

Test 1

Verdict: ACCEPTED

input
1
-1 -1

correct output
0

user output
0

Test 2

Verdict: ACCEPTED

input
2
2 -1
-1 -1

correct output
0

user output
0

Test 3

Verdict: ACCEPTED

input
2
-1 2
-1 -1

correct output
1

user output
1

Test 4

Verdict: ACCEPTED

input
4
2 3
4 -1
-1 -1
-1 -1

correct output
0

user output
0

Test 5

Verdict: ACCEPTED

input
4
2 3
-1 4
-1 -1
-1 -1

correct output
1

user output
1

Test 6

Verdict:

input
4
2 3
-1 -1
4 -1
-1 -1

correct output
1

user output
0

Test 7

Verdict:

input
4
2 3
-1 -1
-1 4
-1 -1

correct output
2

user output
1

Test 8

Verdict: ACCEPTED

input
5
2 3
4 5
-1 -1
-1 -1
...

correct output
0

user output
0

Test 9

Verdict:

input
5
2 3
4 -1
5 -1
-1 -1
...

correct output
-1

user output
0

Test 10

Verdict:

input
5
2 3
4 -1
-1 5
-1 -1
...

correct output
-1

user output
1

Test 11

Verdict:

input
5
2 3
-1 4
5 -1
-1 -1
...

correct output
-1

user output
1

Test 12

Verdict:

input
5
2 3
-1 4
-1 5
-1 -1
...

correct output
-1

user output
2

Test 13

Verdict: ACCEPTED

input
5
2 3
-1 -1
4 5
-1 -1
...

correct output
1

user output
1

Test 14

Verdict: ACCEPTED

input
6
2 3
4 5
6 -1
-1 -1
...

correct output
0

user output
0

Test 15

Verdict: ACCEPTED

input
6
2 3
4 5
-1 6
-1 -1
...

correct output
1

user output
1

Test 16

Verdict: ACCEPTED

input
6
2 3
4 -1
5 6
-1 -1
...

correct output
1

user output
1

Test 17

Verdict: ACCEPTED

input
6
2 3
-1 4
5 6
-1 -1
...

correct output
2

user output
2

Test 18

Verdict: ACCEPTED

input
19
2 3
6 5
4 7
-1 -1
...

correct output
-1

user output
-1

Test 19

Verdict: ACCEPTED

input
9
3 2
4 5
-1 -1
6 9
...

correct output
-1

user output
-1

Test 20

Verdict: ACCEPTED

input
4
-1 2
3 4
-1 -1
-1 -1

correct output
-1

user output
-1

Test 21

Verdict: ACCEPTED

input
12
3 2
4 5
-1 6
7 9
...

correct output
-1

user output
-1

Test 22

Verdict:

input
10
3 2
7 5
4 6
8 10
...

correct output
-1

user output
0

Test 23

Verdict:

input
1000
2 -1
-1 3
4 -1
5 -1
...

correct output
-1

user output
507

Test 24

Verdict:

input
10000
2 -1
-1 3
4 -1
5 -1
...

correct output
-1

user output
5086

Test 25

Verdict:

input
100000
2 -1
-1 3
4 -1
5 -1
...

correct output
-1

user output
50244

Test 26

Verdict: ACCEPTED

input
10
2 3
6 5
7 4
-1 -1
...

correct output
2

user output
2

Test 27

Verdict:

input
18
2 3
7 6
5 4
11 9
...

correct output
3

user output
1

Test 28

Verdict:

input
13
3 2
4 5
7 6
-1 -1
...

correct output
-1

user output
3

Test 29

Verdict:

input
660
3 2
5 6
7 4
10 8
...

correct output
-1

user output
54

Test 30

Verdict:

input
1250
2 3
6 7
5 4
10 14
...

correct output
7

user output
4

Test 31

Verdict:

input
5000
2 3
6 7
5 4
11 14
...

correct output
4

user output
1

Test 32

Verdict: ACCEPTED

input
32767
2 3
5 4
7 6
13 14
...

correct output
0

user output
0

Test 33

Verdict:

input
100000
2 3
5 7
4 6
15 14
...

correct output
7

user output
3

Test 34

Verdict:

input
98348
3 2
7 5
6 4
10 8
...

correct output
-1

user output
11838

Test 35

Verdict:

input
100000
2 3
5 7
4 6
15 14
...

correct output
7

user output
3

Test 36

Verdict:

input
99999
3 2
7 5
6 4
10 8
...

correct output
5

user output
3

Test 37

Verdict:

input
98348
3 2
7 5
6 4
10 8
...

correct output
-1

user output
11838

Test 38

Verdict: ACCEPTED

input
98303
3 2
5 4
7 6
9 8
...

correct output
1

user output
1

Test 39

Verdict:

input
98304
3 2
5 4
7 6
9 8
...

correct output
16

user output
2

Test 40

Verdict:

input
99989
3 2
5 4
7 6
9 8
...

correct output
15

user output
7

Test 41

Verdict:

input
99989
2 3
4 5
6 7
8 9
...

correct output
15

user output
7

Test 42

Verdict:

input
100000
3 2
5 4
7 6
9 8
...

correct output
16

user output
6