For problem statement at 0-999/0-99/20-29/22/problemE.txt this is a correct solution, but verifier at 0-999/0-99/20-29/22/verifierE.go ends with case 1 failed: expected:
2
7 6
7 2
got:
2
3 2
3 6
input:
9
4 1 9 3 7 5 3 5 8
exit status 1 can you fix the verifier? package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanWords)
buf := make([]byte, 1024*1024)
scanner.Buffer(buf, 1024*1024)
scanInt := func() int {
scanner.Scan()
res, _ := strconv.Atoi(scanner.Text())
return res
}
if !scanner.Scan() {
return
}
n, _ := strconv.Atoi(scanner.Text())
f := make([]int, n+1)
inDegree := make([]int, n+1)
for i := 1; i <= n; i++ {
f[i] = scanInt()
inDegree[f[i]]++
}
compID := make([]int, n+1)
var cycles []int
var leavesOfComp [][]int
currentComp := 0
for i := 1; i <= n; i++ {
if inDegree[i] == 0 {
curr := i
var path []int
for compID[curr] == 0 {
compID[curr] = -1
path = append(path, curr)
curr = f[curr]
}
c := compID[curr]
if c == -1 {
currentComp++
c = currentComp
cycles = append(cycles, curr)
leavesOfComp = append(leavesOfComp, []int{})
}
for _, v := range path {
compID[v] = c
}
leavesOfComp[c-1] = append(leavesOfComp[c-1], i)
}
}
for i := 1; i <= n; i++ {
if compID[i] == 0 {
curr := i
var path []int
for compID[curr] == 0 {
compID[curr] = -1
path = append(path, curr)
curr = f[curr]
}
c := compID[curr]
if c == -1 {
currentComp++
c = currentComp
cycles = append(cycles, curr)
leavesOfComp = append(leavesOfComp, []int{curr})
}
for _, v := range path {
compID[v] = c
}
}
}
numLeaves := 0
for i := 1; i <= n; i++ {
if inDegree[i] == 0 {
numLeaves++
}
}
if currentComp == 1 && numLeaves == 0 {
fmt.Println(0)
return
}
var ans [][]int
k := currentComp
for i := 0; i < k; i++ {
u := cycles[i]
v := leavesOfComp[(i+1)%k][0]
ans = append(ans, []int{u, v})
for j := 1; j < len(leavesOfComp[i]); j++ {
ans = append(ans, []int{u, leavesOfComp[i][j]})
}
}
fmt.Println(len(ans))
writer := bufio.NewWriter(os.Stdout)
for _, edge := range ans {
fmt.Fprintf(writer, "%d %d\n", edge[0], edge[1])
}
writer.Flush()
}