Submission #7971444


Source Code Expand

package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout

	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
		if len(os.Args) > 2 {
			wfp, _ = os.Create(os.Args[2])
		}
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)

	n := getNextInt(scanner)
	m := getNextInt(scanner)

	nn := make([]int, n)
	mm := make([]int, m)

	for i := 0; i < n; i++ {
		nn[i] = i
	}

	for i := 0; i < m; i++ {
		mm[i] = -1
	}

	for i := 0; i < n; i++ {
		k := getNextInt(scanner)
		for ii := 0; ii < k; ii++ {
			l := getNextInt(scanner) - 1
			if mm[l] == -1 {
				mm[l] = i
				continue
			}

			r1 := root(i, nn)
			r2 := root(mm[l], nn)
			if r1 != r2 {
				nn[r2] = r1
			}
		}
	}

	counter := 0
	prev := -1

	ans := "YES"
	for i := 0; i < n; i++ {
		r := root(i, nn)
		if prev != r {
			prev = r
			counter++
			if counter > 1 {
				ans = "NO"
				break
			}
		}
	}

	fmt.Fprintln(writer, ans)
	writer.Flush()
}

func root(i int, nodes []int) int {
	if i == nodes[i] {
		return i
	}
	r := root(nodes[i], nodes)
	nodes[i] = r
	return nodes[i]
}

Submission Info

Submission Time
Task C - Interpretation
User ccppjsrb
Language Go (1.6)
Score 400
Code Size 1950 Byte
Status AC
Exec Time 69 ms
Memory 18688 KB

Judge Result

Set Name sample dataset1 dataset2
Score / Max Score 0 / 0 200 / 200 200 / 200
Status
AC × 2
AC × 12
AC × 27
Set Name Test Cases
sample sample-01.txt, sample-02.txt
dataset1 sample-01.txt, sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt
dataset2 sample-01.txt, sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 02-01.txt, 02-02.txt, 02-03.txt, 02-04.txt, 02-05.txt, 02-06.txt, 02-07.txt, 02-08.txt, 02-09.txt, 02-10.txt, 02-11.txt, 02-12.txt, 02-13.txt, sample-01.txt, sample-02.txt
Case Name Status Exec Time Memory
01-01.txt AC 1 ms 640 KB
01-02.txt AC 2 ms 640 KB
01-03.txt AC 2 ms 640 KB
01-04.txt AC 2 ms 640 KB
01-05.txt AC 2 ms 640 KB
01-06.txt AC 2 ms 640 KB
01-07.txt AC 2 ms 640 KB
01-08.txt AC 2 ms 640 KB
01-09.txt AC 2 ms 640 KB
01-10.txt AC 2 ms 768 KB
02-01.txt AC 25 ms 2560 KB
02-02.txt AC 35 ms 2304 KB
02-03.txt AC 26 ms 2176 KB
02-04.txt AC 33 ms 3072 KB
02-05.txt AC 39 ms 2560 KB
02-06.txt AC 31 ms 3072 KB
02-07.txt AC 38 ms 2688 KB
02-08.txt AC 43 ms 10368 KB
02-09.txt AC 69 ms 18688 KB
02-10.txt AC 43 ms 9088 KB
02-11.txt AC 42 ms 9088 KB
02-12.txt AC 29 ms 2944 KB
02-13.txt AC 28 ms 3072 KB
sample-01.txt AC 1 ms 640 KB
sample-02.txt AC 1 ms 640 KB