← Home
For problem statement at 1000-1999/1800-1899/1870-1879/1876/problemC.txt this is a correct solution, but verifier at 1000-1999/1800-1899/1870-1879/1876/verifierC.go ends with All tests passed can you fix the verifier? ```go
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	line := strings.Fields(scanner.Text())
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i], _ = strconv.Atoi(line[i])
		a[i]--
	}

	pre := make([][]int, n)
	for i := 0; i < n; i++ {
		pre[i] = []int{}
	}
	indeg := make([]int, n)
	for i, v := range a {
		pre[v] = append(pre[v], i)
		indeg[v]++
	}

	processed := make([]bool, n)
	fineU := make([]bool, n)
	fineS := make([]bool, n)

	queue := make([]int, 0)
	for i := 0; i < n; i++ {
		if indeg[i] == 0 {
			queue = append(queue, i)
		}
	}

	for len(queue) > 0 {
		v := queue[0]
		queue = queue[1:]
		processed[v] = true

		allS := true
		hasU := false
		allValid := true
		for _, c := range pre[v] {
			if !fineU[c] && !fineS[c] {
				allValid = false
			}
			if !fineS[c] {
				allS = false
			}
			if fineU[c] {
				hasU = true
			}
		}
		if allValid {
			fineU[v] = allS
			fineS[v] = hasU
		}

		p := a[v]
		indeg[p]--
		if indeg[p] == 0 {
			queue = append(queue, p)
		}
	}

	for i := 0; i < n; i++ {
		if processed[i] && !fineU[i] && !fineS[i] {
			fmt.Println(-1)
			return
		}
	}

	state := make([]int, n)
	for i := range state {
		state[i] = -1
	}

	visitedCycle := make([]bool, n)

	for i := 0; i < n; i++ {
		if processed[i] || visitedCycle[i] {
			continue
		}
		cycle := []int{}
		cur := i
		for !visitedCycle[cur] {
			visitedCycle[cur] = true
			cycle = append(cycle, cur)
			cur = a[cur]
		}

		m := len(cycle)
		type cycleOpt struct {
			okU, okS_if_p_U, okS_if_p_S bool
		}
		opt := make([]cycleOpt, m)

		for idx, v := range cycle {
			allS := true
			hasU := false
			allValid := true
			for _, c := range pre[v] {
				if processed[c] {
					if !fineU[c] && !fineS[c] {
						allValid = false
					}
					if !fineS[c] {
						allS = false
					}
					if fineU[c] {
						hasU = true
					}
				}
			}
			opt[idx].okU = allS
			opt[idx].okS_if_p_U = allValid
			opt[idx].okS_if_p_S = allValid && hasU
		}

		foundCycle := false
		var X []int

		for startState := 1; startState >= 0 && !foundCycle; startState-- {
			if startState == 1 && !opt[0].okU {
				continue
			}
			poss := make([]int, m+1)
			if startState == 1 {
				poss[0] = 1 << 1
			} else {
				poss[0] = 1 << 0
			}

			for j := 0; j < m; j++ {
				nextIdx := (j + 1) % m
				nextOpt := opt[nextIdx]
				for prevState := 0; prevState <= 1; prevState++ {
					if poss[j]&(1<<prevState) == 0 {
						continue
					}
					if prevState == 1 {
						if nextOpt.okS_if_p_U {
							poss[j+1] |= 1 << 0
						}
					} else {
						if nextOpt.okU {
							poss[j+1] |= 1 << 1
						}
						if nextOpt.okS_if_p_S {
							poss[j+1] |= 1 << 0
						}
					}
				}
			}

			if poss[m]&(1<<startState) != 0 {
				X = make([]int, m)
				currState := startState
				for j := m - 1; j >= 0; j-- {
					nextIdx := (j + 1) % m
					nextOpt := opt[nextIdx]
					for prevState := 0; prevState <= 1; prevState++ {
						if poss[j]&(1<<prevState) != 0 {
							valid := false
							if prevState == 1 && currState == 0 && nextOpt.okS_if_p_U {
								valid = true
							} else if prevState == 0 {
								if currState == 1 && nextOpt.okU {
									valid = true
								} else if currState == 0 && nextOpt.okS_if_p_S {
									valid = true
								}
							}
							if valid {
								X[j] = prevState
								currState = prevState
								break
							}
						}
					}
				}
				foundCycle = true
			}
		}

		if !foundCycle {
			fmt.Println(-1)
			return
		}

		for idx, v := range cycle {
			if X[idx] == 1 {
				state[v] = 1
			} else {
				state[v] = 0
			}
		}
	}

	queue2 := make([]int, 0)
	for i := 0; i < n; i++ {
		if state[i] != -1 {
			queue2 = append(queue2, i)
		}
	}

	for len(queue2) > 0 {
		v := queue2[0]
		queue2 = queue2[1:]
		sv := state[v]

		covered := false
		for _, c := range pre[v] {
			if state[c] == 1 {
				covered = true
				break
			}
		}

		if sv == 1 {
			for _, c := range pre[v] {
				if state[c] == -1 {
					state[c] = 0
					queue2 = append(queue2, c)
				} else if state[c] == 1 {
					fmt.Println(-1)
					return
				}
			}
		} else {
			if covered {
				for _, c := range pre[v] {
					if state[c] == -1 {
						if fineS[c] {
							state[c] = 0
						} else {
							state[c] = 1
						}
						queue2 = append(queue2, c)
					}
				}
			} else {
				chosen := false
				for _, c := range pre[v] {
					if state[c] == -1 && fineU[c] {
						state[c] = 1
						queue2 = append(queue2, c)
						chosen = true
						break
					}
				}
				if !chosen {
					fmt.Println(-1)
					return
				}
				for _, c := range pre[v] {
					if state[c] == -1 {
						if fineS[c] {
							state[c] = 0
						} else {
							state[c] = 1
						}
						queue2 = append(queue2, c)
					}
				}
			}
		}
	}

	U := make([]int, 0)
	for i := 0; i < n; i++ {
		if state[i] == 1 {
			U = append(U, i)
		}
	}

	z := len(U)
	p := make([]int, z)
	for idx, i := range U {
		p[idx] = a[i] + 1
	}

	fmt.Println(z)
	if z > 0 {
		fmt.Println(strings.Trim(fmt.Sprint(p), "[]"))
	} else {
		fmt.Println()
	}
}
```