CSES - Datatähti Open 2017 - Results
Submission details
Task:Witch game
Sender:MrQubo
Submission time:2017-01-22 21:21:40 +0200
Language:C++
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED23
#2ACCEPTED24
#3ACCEPTED53
Test results
testverdicttimegroup
#1ACCEPTED0.05 s1details
#2ACCEPTED0.04 s1details
#3ACCEPTED0.05 s1details
#4ACCEPTED0.04 s1details
#5ACCEPTED0.06 s1details
#6ACCEPTED0.06 s2details
#7ACCEPTED0.06 s2details
#8ACCEPTED0.05 s2details
#9ACCEPTED0.05 s2details
#10ACCEPTED0.06 s2details
#11ACCEPTED0.09 s3details
#12ACCEPTED0.14 s3details
#13ACCEPTED0.13 s3details
#14ACCEPTED0.11 s3details
#15ACCEPTED0.14 s3details

Code

#include <iostream>
#include <set>
using namespace std;

int constexpr N = 1e5 + 10;

bool cycle3[N];
int child[N];
set<int> adj[N];

long long bn2(long long n) {
	return n*(n-1)/2;
}

int main() {
	int n, m = 0;
	cin >> n;

	for (int i = 0 ; i != n ; ++i) {
		int x;
		cin >> x; --x;
		adj[x].emplace(i);
		adj[i].emplace(x);
		child[i] = x;
	}

	for(int v = 0 ; v != n ; ++v)
		m += adj[v].size();
	m /= 2;

	for(int v = 0 ; v != n ; ++v)
		cycle3[v] = child[v] > v && child[child[v]] > v && adj[v].find(child[child[v]]) != adj[v].end();

	long long sum = 0;

	for(int v = 0 ; v != n ; ++v) {
		int deg = adj[v].size();
		int deg2 = 0;

		for(auto const & s : adj[v])
			deg2 += adj[s].size();

		deg2 -= cycle3[v];

		sum += bn2(n - v - 1 - deg) - (m - deg2);

		for(auto const & s : adj[v])
			adj[s].erase(v);
		m -= deg;
	}

	cout << sum;
}

Test details

Test 1

Group: 1

Verdict: ACCEPTED

input
100
2 1 4 3 6 5 8 7 10 9 12 11 14 ...

correct output
156800

user output
156800

Test 2

Group: 1

Verdict: ACCEPTED

input
100
2 3 4 5 6 7 8 9 10 11 12 13 14...

correct output
152000

user output
152000

Test 3

Group: 1

Verdict: ACCEPTED

input
100
2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
156849

user output
156849

Test 4

Group: 1

Verdict: ACCEPTED

input
100
2 3 1 5 6 4 8 9 7 11 12 10 14 ...

correct output
151968

user output
151968

Test 5

Group: 1

Verdict: ACCEPTED

input
100
8 98 100 62 42 36 95 70 22 49 ...

correct output
152040

user output
152040

Test 6

Group: 2

Verdict: ACCEPTED

input
5000
2 1 4 3 6 5 8 7 10 9 12 11 14 ...

correct output
20808340000

user output
20808340000

Test 7

Group: 2

Verdict: ACCEPTED

input
5000
2 3 4 5 6 7 8 9 10 11 12 13 14...

correct output
20795850000

user output
20795850000

Test 8

Group: 2

Verdict: ACCEPTED

input
5000
2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
20808342499

user output
20808342499

Test 9

Group: 2

Verdict: ACCEPTED

input
5000
2 3 1 5 6 4 8 9 7 11 12 10 14 ...

correct output
20795848337

user output
20795848337

Test 10

Group: 2

Verdict: ACCEPTED

input
5000
283 2880 2565 3289 4160 936 39...

correct output
20795852465

user output
20795852465

Test 11

Group: 3

Verdict: ACCEPTED

input
100000
2 1 4 3 6 5 8 7 10 9 12 11 14 ...

correct output
166656666800000

user output
166656666800000

Test 12

Group: 3

Verdict: ACCEPTED

input
100000
2 3 4 5 6 7 8 9 10 11 12 13 14...

correct output
166651667000000

user output
166651667000000

Test 13

Group: 3

Verdict: ACCEPTED

input
100000
2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
166656666849999

user output
166656666849999

Test 14

Group: 3

Verdict: ACCEPTED

input
100000
2 3 1 5 6 4 8 9 7 11 12 10 14 ...

correct output
166651666966668

user output
166651666966668

Test 15

Group: 3

Verdict: ACCEPTED

input
100000
186 62491 95379 37431 88427 93...

correct output
166651667250100

user output
166651667250100