CSES - APIO 2007 - Results
Submission details
Task:Mobiles
Sender:ArktinenKarpalo
Submission time:2019-03-07 15:33:38 +0200
Language:C++
Status:READY
Result:0
Feedback
groupverdictscore
#10
Test results
testverdicttime
#10.02 sdetails
#20.03 sdetails
#30.02 sdetails
#40.02 sdetails
#50.02 sdetails
#60.02 sdetails
#70.02 sdetails
#80.02 sdetails
#90.04 sdetails
#100.03 sdetails
#110.03 sdetails
#120.02 sdetails
#130.02 sdetails
#140.02 sdetails
#150.02 sdetails
#160.02 sdetails
#170.02 sdetails
#180.02 sdetails
#190.02 sdetails
#200.02 sdetails
#210.02 sdetails
#220.03 sdetails
#230.02 sdetails
#240.02 sdetails
#250.06 sdetails
#260.03 sdetails
#270.02 sdetails
#280.03 sdetails
#290.02 sdetails
#300.03 sdetails
#310.02 sdetails
#320.05 sdetails
#330.06 sdetails
#340.06 sdetails
#350.04 sdetails
#360.06 sdetails
#370.05 sdetails
#380.06 sdetails
#390.05 sdetails
#400.06 sdetails
#410.05 sdetails
#420.05 sdetails

Code

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
#define N (1<<18)
#define M 1000000007
#define P complex<long long>
#define X real()
#define Y imag()

using namespace std;

int n, a, b, lvl[401010], lvlm[404040], cnt=2, rs[401010], l[402020], r[402020], ans;
vector<int> v, v2; // laps
vector<int> taso[202020];

void haku(int s) {
	if(l[s]) {
		lvl[l[s]] = lvl[s]+1;
		lvlm[l[s]] = lvlm[s]+1;
		haku(l[s]);
	}
	if(r[s]) {
		lvl[r[s]] = lvl[s]+1;
		lvlm[r[s]] = lvlm[s]+1;
		haku(r[s]);
	}
	v.push_back(s);
	taso[lvl[s]].push_back(s);
}

void haku2(int s) {
	bool ok = false;
	if(l[s]) {
		haku2(l[s]);
	} else {
		v2.push_back(lvl[s]);
		ok = true;
	}
	if(r[s]) {
		haku2(r[s]);
	} else {
		if(!ok)
			v2.push_back(lvl[s]);
	}
}

int ss(int x) {
	if(x == -1) {
		cnt++;
		return cnt-1;
	}
	if(!rs[x]) {
		rs[x] = cnt;
		cnt++;
	}
	return rs[x];
}
int main() {
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(0);
	cin >> n;
	rs[1] = 1;
	for(int i=1; i<=n; i++) {
		cin >> a >> b;
		int aS = ss(a);
		int bS = ss(b);
		l[ss(i)] = aS;
		r[ss(i)] = bS;
	}
	haku(1);
	for(int i=400020; i>=0; i--) {
		for(auto u:taso[i]) {
			if(l[u]) {
				lvl[u] = max(lvl[l[u]], lvl[r[u]]);
			}
			if(l[u]) {
				lvlm[u] = min(lvlm[l[u]], lvlm[r[u]]);
			}
		}
	}
	for(int i=400020; i>=0; i--) {
		for(auto u:taso[i]) {
			if(l[u] && r[u]) {
				if(lvlm[l[u]] < lvl[r[u]]) {
					ans++;
					swap(r[u], l[u]);
				}
			}
		}
	}
	haku2(1);
	bool ok = false;
	int ed = v2[0];
	for(auto u:v2) {
		if(u != ed) {
			if(ed-u != 1) {
				cout << -1;
				exit(0);
			} else {
				if(ok) {
					cout << -1;
					exit(0);
				} else {
					ok = true;
				}
			}
		}
		ed = u;
	}
	cout << ans << endl;
}

Test details

Test 1

Verdict:

input
1
-1 -1

correct output
0

user output
(empty)

Test 2

Verdict:

input
2
2 -1
-1 -1

correct output
0

user output
(empty)

Test 3

Verdict:

input
2
-1 2
-1 -1

correct output
1

user output
(empty)

Test 4

Verdict:

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

correct output
0

user output
(empty)

Test 5

Verdict:

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

correct output
1

user output
(empty)

Test 6

Verdict:

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

correct output
1

user output
(empty)

Test 7

Verdict:

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

correct output
2

user output
(empty)

Test 8

Verdict:

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

correct output
0

user output
(empty)

Test 9

Verdict:

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

correct output
-1

user output
(empty)

Test 10

Verdict:

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

correct output
-1

user output
(empty)

Test 11

Verdict:

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

correct output
-1

user output
(empty)

Test 12

Verdict:

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

correct output
-1

user output
(empty)

Test 13

Verdict:

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

correct output
1

user output
(empty)

Test 14

Verdict:

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

correct output
0

user output
(empty)

Test 15

Verdict:

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

correct output
1

user output
(empty)

Test 16

Verdict:

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

correct output
1

user output
(empty)

Test 17

Verdict:

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

correct output
2

user output
(empty)

Test 18

Verdict:

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

correct output
-1

user output
(empty)

Test 19

Verdict:

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

correct output
-1

user output
(empty)

Test 20

Verdict:

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

correct output
-1

user output
(empty)

Test 21

Verdict:

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

correct output
-1

user output
(empty)

Test 22

Verdict:

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

correct output
-1

user output
(empty)

Test 23

Verdict:

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

correct output
-1

user output
(empty)

Test 24

Verdict:

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

correct output
-1

user output
(empty)

Test 25

Verdict:

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

correct output
-1

user output
(empty)

Test 26

Verdict:

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

correct output
2

user output
(empty)

Test 27

Verdict:

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

correct output
3

user output
(empty)

Test 28

Verdict:

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

correct output
-1

user output
(empty)

Test 29

Verdict:

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

correct output
-1

user output
(empty)

Test 30

Verdict:

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

correct output
7

user output
(empty)

Test 31

Verdict:

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

correct output
4

user output
(empty)

Test 32

Verdict:

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

correct output
0

user output
(empty)

Test 33

Verdict:

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

correct output
7

user output
(empty)

Test 34

Verdict:

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

correct output
-1

user output
(empty)

Test 35

Verdict:

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

correct output
7

user output
(empty)

Test 36

Verdict:

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

correct output
5

user output
(empty)

Test 37

Verdict:

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

correct output
-1

user output
(empty)

Test 38

Verdict:

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

correct output
1

user output
(empty)

Test 39

Verdict:

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

correct output
16

user output
(empty)

Test 40

Verdict:

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

correct output
15

user output
(empty)

Test 41

Verdict:

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

correct output
15

user output
(empty)

Test 42

Verdict:

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

correct output
16

user output
(empty)