← Home
For problem statement at 0-999/800-899/850-859/859/problemE.txt this is a correct solution, but verifier at 0-999/800-899/850-859/859/verifierE.go ends with All tests passed can you fix the verifier? package main

import (
	"fmt"
	"io"
	"os"
)

func main() {
	buf, _ := io.ReadAll(os.Stdin)
	var pos int
	readInt := func() int {
		for pos < len(buf) && (buf[pos] < '0' || buf[pos] > '9') {
			pos++
		}
		if pos >= len(buf) {
			return 0
		}
		res := 0
		for pos < len(buf) && buf[pos] >= '0' && buf[pos] <= '9' {
			res = res*10 + int(buf[pos]-'0')
			pos++
		}
		return res
	}

	N := readInt()
	if N == 0 {
		return
	}

	parent := make([]int, 2*N+1)
	vCount := make([]int, 2*N+1)
	eCount := make([]int, 2*N+1)
	hasSelfLoop := make([]bool, 2*N+1)

	for i := 1; i <= 2*N; i++ {
		parent[i] = i
		vCount[i] = 1
	}

	var find func(i int) int
	find = func(i int) int {
		if parent[i] == i {
			return i
		}
		parent[i] = find(parent[i])
		return parent[i]
	}

	for i := 0; i < N; i++ {
		u := readInt()
		v := readInt()
		if u == v {
			rootU := find(u)
			eCount[rootU]++
			hasSelfLoop[rootU] = true
		} else {
			rootU := find(u)
			rootV := find(v)
			if rootU != rootV {
				parent[rootV] = rootU
				vCount[rootU] += vCount[rootV]
				eCount[rootU] += eCount[rootV] + 1
				hasSelfLoop[rootU] = hasSelfLoop[rootU] || hasSelfLoop[rootV]
			} else {
				eCount[rootU]++
			}
		}
	}

	ans := 1
	mod := 1000000007

	for i := 1; i <= 2*N; i++ {
		if parent[i] == i && eCount[i] > 0 {
			if eCount[i] == vCount[i]-1 {
				ans = (ans * vCount[i]) % mod
			} else if eCount[i] == vCount[i] {
				if hasSelfLoop[i] {
					ans = (ans * 1) % mod
				} else {
					ans = (ans * 2) % mod
				}
			} else {
				ans = 0
			}
		}
	}

	fmt.Println(ans)
}