For problem statement at 0-999/900-999/980-989/982/problemF.txt this is a correct solution, but verifier at 0-999/900-999/980-989/982/verifierF.go ends with can you fix the verifier? package main
import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"time"
)
type Scanner struct {
buf []byte
pos int
}
func NewScanner() *Scanner {
b, _ := ioutil.ReadAll(os.Stdin)
return &Scanner{buf: b, pos: 0}
}
func (s *Scanner) NextInt() int {
for s.pos < len(s.buf) && s.buf[s.pos] <= ' ' {
s.pos++
}
if s.pos >= len(s.buf) {
return 0
}
res := 0
for s.pos < len(s.buf) && s.buf[s.pos] > ' ' {
res = res*10 + int(s.buf[s.pos]-'0')
s.pos++
}
return res
}
func main() {
rand.Seed(time.Now().UnixNano())
scanner := NewScanner()
n := scanner.NextInt()
m := scanner.NextInt()
if n == 0 {
return
}
adj := make([][]int, n+1)
for i := 0; i < m; i++ {
u := scanner.NextInt()
v := scanner.NextInt()
adj[u] = append(adj[u], v)
}
for i := 1; i <= n; i++ {
rand.Shuffle(len(adj[i]), func(j, k int) {
adj[i][j], adj[i][k] = adj[i][k], adj[i][j]
})
}
visited := make([]int, n+1)
parent := make([]int, n+1)
depth := make([]int, n+1)
findCycleWithout := func(x int) []int {
for i := 1; i <= n; i++ {
visited[i] = 0
}
var cycle []int
var dfs func(u int) bool
dfs = func(u int) bool {
visited[u] = 1
bestV := 0
maxD := -1
for _, v := range adj[u] {
if v == x {
continue
}
if visited[v] == 1 {
if depth[v] > maxD {
maxD = depth[v]
bestV = v
}
}
}
if bestV != 0 {
curr := u
cycle = append(cycle, bestV)
for curr != bestV {
cycle = append(cycle, curr)
curr = parent[curr]
}
return true
}
for _, v := range adj[u] {
if v == x {
continue
}
if visited[v] == 0 {
parent[v] = u
depth[v] = depth[u] + 1
if dfs(v) {
return true
}
}
}
visited[u] = 2
return false
}
for i := 1; i <= n; i++ {
if i == x {
continue
}
if visited[i] == 0 {
parent[i] = 0
depth[i] = 0
if dfs(i) {
return cycle
}
}
}
return nil
}
cycle := findCycleWithout(0)
if cycle == nil {
fmt.Println("-1")
return
}
candidates := make([]int, len(cycle))
copy(candidates, cycle)
inCycle := make([]bool, n+1)
startTime := time.Now()
for len(candidates) > 0 {
if time.Since(startTime) > 1800*time.Millisecond {
fmt.Println(candidates[0])
return
}
idx := rand.Intn(len(candidates))
x := candidates[idx]
c := findCycleWithout(x)
if c == nil {
fmt.Println(x)
return
}
for _, v := range c {
inCycle[v] = true
}
var nextCandidates []int
for _, v := range candidates {
if inCycle[v] {
nextCandidates = append(nextCandidates, v)
}
}
for _, v := range c {
inCycle[v] = false
}
candidates = nextCandidates
}
fmt.Println("-1")
}