Task: | Freshman's Database |
Sender: | PLS2020 |
Submission time: | 2020-10-03 14:01:06 +0300 |
Language: | C++ (C++11) |
Status: | READY |
Result: | ACCEPTED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.01 s | details |
#2 | ACCEPTED | 0.01 s | details |
#3 | ACCEPTED | 0.12 s | details |
#4 | ACCEPTED | 0.15 s | details |
#5 | ACCEPTED | 0.15 s | details |
#6 | ACCEPTED | 0.15 s | details |
#7 | ACCEPTED | 0.15 s | details |
#8 | ACCEPTED | 0.15 s | details |
#9 | ACCEPTED | 0.15 s | details |
#10 | ACCEPTED | 0.15 s | details |
Compiler report
input/code.cpp: In function 'int main()': input/code.cpp:16:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for (int i = 0; i < n; ++i) { ~~^~~ input/code.cpp:26:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] if (visited[latest] == i) {
Code
#include <iostream> #include <vector> int main(void) { unsigned int n = 0; std::ios_base::sync_with_stdio(false); std::cin >> n; std::vector<unsigned int> array(n + 1); std::vector<int> visited(n + 1, -1); array[0] = 0; for (int i = 0; i < n; ++i) { std::cin >> array[i + 1]; } unsigned int cycles = 0; for (unsigned int i = 1; i < (n + 1); ++i) { unsigned int latest = i; while (true) { if (visited[latest] == i) { cycles += 1; //std::cout << "cycle " << i << " " << latest << std::endl; break; } // Already would have seen this cycle if (visited[latest] > -1) { //std::cout << "skip " << i << " " << latest << " " << visited[latest] << std::endl; break; } visited[latest] = i; latest = array[latest]; } } std::cout << cycles << std::endl; }
Test details
Test 1
Verdict: ACCEPTED
input |
---|
16
2 3 4 5 6 1 4 7 7 4 12 11 14 1... |
correct output |
---|
3 |
user output |
---|
3 |
Test 2
Verdict: ACCEPTED
input |
---|
2
2 1 |
correct output |
---|
1 |
user output |
---|
1 |
Test 3
Verdict: ACCEPTED
input |
---|
1000000 906853 1 1 1 3 4 3 2 5 5 5 10 ... |
correct output |
---|
1 |
user output |
---|
1 |
Test 4
Verdict: ACCEPTED
input |
---|
1000000 227998 891986 290950 887622 37... |
correct output |
---|
6736 |
user output |
---|
6736 |
Test 5
Verdict: ACCEPTED
input |
---|
1000000 832833 455297 341097 88590 258... |
correct output |
---|
16 |
user output |
---|
16 |
Test 6
Verdict: ACCEPTED
input |
---|
1000000 635299 635243 476863 88031 195... |
correct output |
---|
73 |
user output |
---|
73 |
Test 7
Verdict: ACCEPTED
input |
---|
1000000 444011 366349 710148 901981 81... |
correct output |
---|
244 |
user output |
---|
244 |
Test 8
Verdict: ACCEPTED
input |
---|
1000000 248398 271880 881725 521008 33... |
correct output |
---|
332 |
user output |
---|
332 |
Test 9
Verdict: ACCEPTED
input |
---|
999999 938280 731633 536902 381480 65... |
correct output |
---|
6771 |
user output |
---|
6771 |
Test 10
Verdict: ACCEPTED
input |
---|
999999 196127 288846 245904 406819 13... |
correct output |
---|
105 |
user output |
---|
105 |