CSES - APIO 2007 - Results
Submission details
Task:Mobiles
Sender:Olli
Submission time:2019-03-07 14:12:39 +0200
Language:C++
Status:READY
Result:0
Feedback
groupverdictscore
#10
Test results
testverdicttime
#1ACCEPTED0.01 sdetails
#20.01 sdetails
#3ACCEPTED0.02 sdetails
#40.01 sdetails
#5ACCEPTED0.03 sdetails
#60.02 sdetails
#7ACCEPTED0.02 sdetails
#80.01 sdetails
#9ACCEPTED0.01 sdetails
#10ACCEPTED0.03 sdetails
#11ACCEPTED0.02 sdetails
#12ACCEPTED0.01 sdetails
#13ACCEPTED0.01 sdetails
#140.01 sdetails
#15ACCEPTED0.02 sdetails
#160.03 sdetails
#17ACCEPTED0.01 sdetails
#18ACCEPTED0.02 sdetails
#19ACCEPTED0.03 sdetails
#20ACCEPTED0.01 sdetails
#21ACCEPTED0.01 sdetails
#22ACCEPTED0.02 sdetails
#23ACCEPTED0.02 sdetails
#24ACCEPTED0.02 sdetails
#25ACCEPTED0.11 sdetails
#26ACCEPTED0.01 sdetails
#27ACCEPTED0.02 sdetails
#28ACCEPTED0.02 sdetails
#29ACCEPTED0.02 sdetails
#300.01 sdetails
#310.02 sdetails
#32ACCEPTED0.05 sdetails
#330.07 sdetails
#34ACCEPTED0.08 sdetails
#350.08 sdetails
#360.09 sdetails
#37ACCEPTED0.08 sdetails
#38ACCEPTED0.08 sdetails
#39ACCEPTED0.07 sdetails
#40ACCEPTED0.09 sdetails
#41ACCEPTED0.08 sdetails
#42ACCEPTED0.07 sdetails

Code

#include <iostream>
#include <vector>
#include <set>

using namespace std;

const int N = 1e5 + 5;

int l[N];
int r[N];
int p[N];
int d[N];
int sl[N];
int sr[N];
int cl[N];
int cr[N];

int main() {
	int n;
	cin >> n;
	set<int> dep;
	for(int i = 1; i <= n; ++i) {
		int a, b;
		cin >> a >> b;
		l[i] = a;
		r[i] = b;
		if(a != -1) {
			p[a] = i;
			d[a] = d[i] + 1;
		} else {
			dep.insert(d[i] + 1);
		}
		if(b != -1) {
			p[b] = i;
			d[b] = d[i] + 1;
		} else {
			dep.insert(d[i] + 1);
		}
	}

	if(dep.size() > 2) {
		cout << -1 << "\n";
		return 0;
	} else if (dep.size() == 1) {
		cout << 0 << "\n";
		return 0;
	} else {
		auto it = dep.begin();
		int a = *it;
		++it;
		int b = *it;
		if(b - a != 1) {
			cout << -1 << "\n";
			return 0;
		}
	}

/*	cout << "Here\n";


	for(int i = 1; i <= n; ++i) {
		cout << l[i] << " " << r[i] << "\n";
	}

	cout << "\n\n";
*/

	int hi = *(dep.begin());

	for(int i = n; i >= 1; --i) {
		if(l[i] != -1) {
			sl[i] = sl[l[i]] + sr[l[i]];
		} else {
			sl[i] = 1;
		}
		if(r[i] != -1) {
			sr[i] = sl[r[i]] + sr[r[i]];
		} else {
			sr[i] = 1;
		}

		if(d[i] == hi - 1) {
			if(l[i] == -1) {
				cl[i] = 1;
			}
			if(r[i] == -1) {
				cr[i] = 1;
			}
		} else {
			if(l[i] != -1) {
				cl[i] = cl[l[i]] + cr[l[i]];
			} else {
				cl[i] = 0;
			}
			if(r[i] != -1) {
				cr[i] = cr[r[i]] + cl[r[i]];
			} else {
				cr[i] = 0;
			}
		}
	}


	int ans = 0;
	int cu = 1;

	while(cu != -1) {
		int i = cu;
		if(cl[i] > 0 && cl[i] < sl[i] && cr[i] > 0 && cr[i] < sr[i]) {
			cout << -1 << "\n";
			return 0;
		}
		if(cr[i] == sr[i]) {
			cu = l[cu];
			continue;
		}
		if(cr[i] == 0) {
			++ans;
			cu = l[cu];
			continue;
		}
		//0 < cr[i] < sr[i]

		if(cl[i] != sl[i] && cl[i] != 0) {
			cout << -1 << "\n";
			return 0;
		}

		if(cl[i] == 0) {
			cu = r[cu];
			continue;
		}
		++ans;
		cu = r[cu];
	}

	cout << ans << "\n";


}

Test details

Test 1

Verdict: ACCEPTED

input
1
-1 -1

correct output
0

user output
0

Test 2

Verdict:

input
2
2 -1
-1 -1

correct output
0

user output
1

Test 3

Verdict: ACCEPTED

input
2
-1 2
-1 -1

correct output
1

user output
1

Test 4

Verdict:

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

correct output
0

user output
1

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
2

Test 7

Verdict: ACCEPTED

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

correct output
2

user output
2

Test 8

Verdict:

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

correct output
0

user output
2

Test 9

Verdict: ACCEPTED

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

correct output
-1

user output
-1

Test 10

Verdict: ACCEPTED

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

correct output
-1

user output
-1

Test 11

Verdict: ACCEPTED

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

correct output
-1

user output
-1

Test 12

Verdict: ACCEPTED

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

correct output
-1

user output
-1

Test 13

Verdict: ACCEPTED

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

correct output
1

user output
1

Test 14

Verdict:

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

correct output
0

user output
1

Test 15

Verdict: ACCEPTED

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

correct output
1

user output
1

Test 16

Verdict:

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

correct output
1

user output
2

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: ACCEPTED

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

correct output
-1

user output
-1

Test 23

Verdict: ACCEPTED

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

correct output
-1

user output
-1

Test 24

Verdict: ACCEPTED

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

correct output
-1

user output
-1

Test 25

Verdict: ACCEPTED

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

correct output
-1

user output
-1

Test 26

Verdict: ACCEPTED

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

correct output
2

user output
2

Test 27

Verdict: ACCEPTED

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

correct output
3

user output
3

Test 28

Verdict: ACCEPTED

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

correct output
-1

user output
-1

Test 29

Verdict: ACCEPTED

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

correct output
-1

user output
-1

Test 30

Verdict:

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

correct output
7

user output
8

Test 31

Verdict:

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

correct output
4

user output
5

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
8

Test 34

Verdict: ACCEPTED

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

correct output
-1

user output
-1

Test 35

Verdict:

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

correct output
7

user output
8

Test 36

Verdict:

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

correct output
5

user output
11

Test 37

Verdict: ACCEPTED

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

correct output
-1

user output
-1

Test 38

Verdict: ACCEPTED

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

correct output
1

user output
1

Test 39

Verdict: ACCEPTED

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

correct output
16

user output
16

Test 40

Verdict: ACCEPTED

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

correct output
15

user output
15

Test 41

Verdict: ACCEPTED

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

correct output
15

user output
15

Test 42

Verdict: ACCEPTED

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

correct output
16

user output
16