CSES - APIO 2007 - Results
Submission details
Task:Mobiles
Sender:ArktinenKarpalo
Submission time:2019-03-07 15:00:05 +0200
Language:C++
Status:READY
Result:0
Feedback
groupverdictscore
#10
Test results
testverdicttime
#1ACCEPTED0.03 sdetails
#2ACCEPTED0.03 sdetails
#3ACCEPTED0.03 sdetails
#4ACCEPTED0.04 sdetails
#5ACCEPTED0.04 sdetails
#60.03 sdetails
#70.04 sdetails
#8ACCEPTED0.04 sdetails
#9ACCEPTED0.04 sdetails
#10ACCEPTED0.03 sdetails
#11ACCEPTED0.04 sdetails
#12ACCEPTED0.04 sdetails
#13ACCEPTED0.04 sdetails
#14ACCEPTED0.04 sdetails
#15ACCEPTED0.03 sdetails
#16ACCEPTED0.04 sdetails
#17ACCEPTED0.04 sdetails
#18ACCEPTED0.04 sdetails
#19ACCEPTED0.04 sdetails
#20ACCEPTED0.04 sdetails
#21ACCEPTED0.03 sdetails
#22ACCEPTED0.04 sdetails
#23ACCEPTED0.03 sdetails
#24ACCEPTED0.04 sdetails
#250.09 sdetails
#26ACCEPTED0.03 sdetails
#270.04 sdetails
#28ACCEPTED0.04 sdetails
#29ACCEPTED0.05 sdetails
#300.04 sdetails
#310.04 sdetails
#32ACCEPTED0.05 sdetails
#330.08 sdetails
#34ACCEPTED0.08 sdetails
#350.08 sdetails
#360.08 sdetails
#37ACCEPTED0.08 sdetails
#38ACCEPTED0.07 sdetails
#390.08 sdetails
#400.07 sdetails
#410.07 sdetails
#420.08 sdetails

Compiler report

input/code.cpp: In function 'void haku2(int)':
input/code.cpp:31:7: warning: variable 'ok' set but not used [-Wunused-but-set-variable]
  bool ok = false;
       ^~

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, par[401010], lvl[401010], cnt=2, rs[401010], l[402020], r[402020], ans;
vector<int> laps[401010], v, v2; // laps
vector<int> taso[402020];

void haku(int s) {
	if(l[s]) {
		lvl[l[s]] = lvl[s]+1;
		haku(l[s]);
	}
	if(r[s]) {
		lvl[r[s]] = lvl[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 {
	}
}

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;
		laps[ss(i)].push_back(aS);
		laps[ss(i)].push_back(bS);
	}
	haku(1);
	for(int i=200020; i>=0; i--) {
		for(auto u:taso[i]) {
			if(l[u]) {
				lvl[u] = max(lvl[u], lvl[l[u]]);
			}
			if(r[u]) {
				lvl[u] = max(lvl[u], lvl[r[u]]);
			}
		}
	}
	for(int i=200020; i>=0; i--) {
		for(auto u:taso[i]) {
			if(l[u]) {
				lvl[u] = min(lvl[u], lvl[l[u]]);
			}
			if(r[u]) {
				lvl[u] = min(lvl[u], lvl[r[u]]);
			}
			if(l[u] && r[u]) {
				if(lvl[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: 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
-1

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

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

correct output
-1

user output
(empty)

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

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
-1

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
-1

Test 36

Verdict:

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

correct output
5

user output
-1

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:

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

correct output
16

user output
-1

Test 40

Verdict:

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

correct output
15

user output
-1

Test 41

Verdict:

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

correct output
15

user output
-1

Test 42

Verdict:

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

correct output
16

user output
-1