| Task: | Freshman's Database |
| Sender: | PLS2020 |
| Submission time: | 2020-10-03 13:50:47 +0300 |
| Language: | Python3 (CPython3) |
| Status: | READY |
| Result: | TIME LIMIT EXCEEDED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.02 s | details |
| #2 | ACCEPTED | 0.02 s | details |
| #3 | TIME LIMIT EXCEEDED | -- | details |
| #4 | TIME LIMIT EXCEEDED | -- | details |
| #5 | TIME LIMIT EXCEEDED | -- | details |
| #6 | TIME LIMIT EXCEEDED | -- | details |
| #7 | TIME LIMIT EXCEEDED | -- | details |
| #8 | TIME LIMIT EXCEEDED | -- | details |
| #9 | TIME LIMIT EXCEEDED | -- | details |
| #10 | TIME LIMIT EXCEEDED | -- | details |
Code
# We have managed to create a database with cycles
n = input()
# Array format - each entry in the array points to another entry in the array
array = [0] + [int(x) for x in input().split()]
visited = [-1] * (int(n) + 1)
cycles = 0
for i in range(1, len(array)):
# Start from node i
# Does node i have a parent?
#if has_parent[i] == True:
# continue
# Node does not have a parent, start DFS from this node
#
# Thankfully nobody can have more than one parent (only multiple children)
latest = i
while True:
if visited[latest] == i:
cycles += 1
#print("cycle", i, latest)
break
# Already would have seen this cycle
if visited[latest] > -1:
#print("skip", i, latest, visited[latest])
break
visited[latest] = i
latest = array[latest]
print(cycles)
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: TIME LIMIT EXCEEDED
| input |
|---|
| 1000000 906853 1 1 1 3 4 3 2 5 5 5 10 ... |
| correct output |
|---|
| 1 |
| user output |
|---|
| (empty) |
Test 4
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 1000000 227998 891986 290950 887622 37... |
| correct output |
|---|
| 6736 |
| user output |
|---|
| (empty) |
Test 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 1000000 832833 455297 341097 88590 258... |
| correct output |
|---|
| 16 |
| user output |
|---|
| (empty) |
Test 6
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 1000000 635299 635243 476863 88031 195... |
| correct output |
|---|
| 73 |
| user output |
|---|
| (empty) |
Test 7
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 1000000 444011 366349 710148 901981 81... |
| correct output |
|---|
| 244 |
| user output |
|---|
| (empty) |
Test 8
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 1000000 248398 271880 881725 521008 33... |
| correct output |
|---|
| 332 |
| user output |
|---|
| (empty) |
Test 9
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 999999 938280 731633 536902 381480 65... |
| correct output |
|---|
| 6771 |
| user output |
|---|
| (empty) |
Test 10
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 999999 196127 288846 245904 406819 13... |
| correct output |
|---|
| 105 |
| user output |
|---|
| (empty) |
