Submission #71682185


Source Code Expand

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#define debug(arg) cout << "[" << #arg << "]: " << arg << endl
#else
#define debug(arg) 42
#endif

using llu = uint64_t;
using ll = int64_t;

#define vec vector
#define pb push_back
#define all(n) begin(n), end(n)

void solv() {
	int n; cin >> n;
	vec<int> p(n); for (auto &i : p) cin >> i, --i;
	vec<bool> vis(n);
	llu ans = 0;
	for (int i = 0; i < n; ++i) if (!vis[i]) {
		llu sz = 0;
		int j = i;
		while (!vis[p[j]]) ++sz, vis[p[j]] = 1, j = p[j];
		ans += sz*(sz - 1)/2;
	}
	cout << ans << '\n';
}

signed main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	int t = 1;
	//cin >> t;
	while (t--) solv();
	return 0;
}

Submission Info

Submission Time
Task E - Minimum Swap
User fisher199
Language C++23 (Clang 21.1.0)
Score 475
Code Size 723 Byte
Status AC
Exec Time 100 ms
Memory 4264 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 475 / 475
Status
AC × 3
AC × 53
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt, 01_random_49.txt, 01_random_50.txt, 01_random_51.txt, 01_random_52.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 2856 KiB
00_sample_01.txt AC 1 ms 2880 KiB
00_sample_02.txt AC 1 ms 3152 KiB
01_random_03.txt AC 98 ms 4264 KiB
01_random_04.txt AC 97 ms 4200 KiB
01_random_05.txt AC 96 ms 4172 KiB
01_random_06.txt AC 97 ms 4200 KiB
01_random_07.txt AC 96 ms 4220 KiB
01_random_08.txt AC 97 ms 4220 KiB
01_random_09.txt AC 97 ms 4196 KiB
01_random_10.txt AC 98 ms 4220 KiB
01_random_11.txt AC 97 ms 4200 KiB
01_random_12.txt AC 96 ms 4208 KiB
01_random_13.txt AC 97 ms 4116 KiB
01_random_14.txt AC 96 ms 4116 KiB
01_random_15.txt AC 97 ms 4172 KiB
01_random_16.txt AC 53 ms 3580 KiB
01_random_17.txt AC 50 ms 3552 KiB
01_random_18.txt AC 7 ms 3248 KiB
01_random_19.txt AC 23 ms 3140 KiB
01_random_20.txt AC 96 ms 4108 KiB
01_random_21.txt AC 97 ms 4220 KiB
01_random_22.txt AC 96 ms 4164 KiB
01_random_23.txt AC 96 ms 4232 KiB
01_random_24.txt AC 96 ms 4264 KiB
01_random_25.txt AC 96 ms 4048 KiB
01_random_26.txt AC 98 ms 4200 KiB
01_random_27.txt AC 97 ms 4172 KiB
01_random_28.txt AC 98 ms 4160 KiB
01_random_29.txt AC 97 ms 4196 KiB
01_random_30.txt AC 96 ms 4232 KiB
01_random_31.txt AC 99 ms 4232 KiB
01_random_32.txt AC 98 ms 4164 KiB
01_random_33.txt AC 97 ms 4236 KiB
01_random_34.txt AC 98 ms 4244 KiB
01_random_35.txt AC 97 ms 4264 KiB
01_random_36.txt AC 96 ms 4052 KiB
01_random_37.txt AC 42 ms 3604 KiB
01_random_38.txt AC 52 ms 3592 KiB
01_random_39.txt AC 62 ms 3696 KiB
01_random_40.txt AC 96 ms 4044 KiB
01_random_41.txt AC 30 ms 3340 KiB
01_random_42.txt AC 38 ms 3404 KiB
01_random_43.txt AC 11 ms 3228 KiB
01_random_44.txt AC 48 ms 3580 KiB
01_random_45.txt AC 31 ms 3368 KiB
01_random_46.txt AC 96 ms 4164 KiB
01_random_47.txt AC 100 ms 4160 KiB
01_random_48.txt AC 99 ms 4216 KiB
01_random_49.txt AC 100 ms 4236 KiB
01_random_50.txt AC 96 ms 4216 KiB
01_random_51.txt AC 97 ms 4200 KiB
01_random_52.txt AC 96 ms 4072 KiB