結果

問題 No.1390 Get together
ユーザー aru aruaru aru
提出日時 2021-02-19 20:40:10
言語 Go
(1.22.1)
結果
AC  
実行時間 123 ms / 2,000 ms
コード長 3,326 bytes
コンパイル時間 11,899 ms
コンパイル使用メモリ 208,112 KB
実行使用メモリ 33,228 KB
最終ジャッジ日時 2023-10-14 23:02:44
合計ジャッジ時間 16,014 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2 ms
4,356 KB
testcase_09 AC 3 ms
4,352 KB
testcase_10 AC 2 ms
4,372 KB
testcase_11 AC 1 ms
4,352 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,352 KB
testcase_15 AC 1 ms
4,352 KB
testcase_16 AC 85 ms
31,496 KB
testcase_17 AC 82 ms
33,228 KB
testcase_18 AC 69 ms
29,368 KB
testcase_19 AC 109 ms
24,828 KB
testcase_20 AC 118 ms
27,080 KB
testcase_21 AC 112 ms
27,076 KB
testcase_22 AC 105 ms
29,072 KB
testcase_23 AC 109 ms
29,068 KB
testcase_24 AC 104 ms
29,064 KB
testcase_25 AC 114 ms
24,920 KB
testcase_26 AC 108 ms
24,920 KB
testcase_27 AC 109 ms
24,920 KB
testcase_28 AC 123 ms
24,912 KB
testcase_29 AC 109 ms
24,832 KB
testcase_30 AC 102 ms
24,916 KB
testcase_31 AC 110 ms
24,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

var sc = bufio.NewScanner(os.Stdin)
var wr = bufio.NewWriter(os.Stdout)

func out(x ...interface{}) {
	fmt.Fprintln(wr, x...)
}

func getI() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func getF() float64 {
	sc.Scan()
	i, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int) []int {
	ret := make([]int, N)
	for i := 0; i < N; i++ {
		ret[i] = getI()
	}
	return ret
}

func getS() string {
	sc.Scan()
	return sc.Text()
}

// min, max, asub, absなど基本関数
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

// min for n entry
func nmin(a ...int) int {
	ret := a[0]
	for _, e := range a {
		ret = min(ret, e)
	}
	return ret
}

// max for n entry
func nmax(a ...int) int {
	ret := a[0]
	for _, e := range a {
		ret = max(ret, e)
	}
	return ret
}

func asub(a, b int) int {
	if a > b {
		return a - b
	}
	return b - a
}

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

//
// Disjoint Set Union: Union Find Tree
//

// DSU :
type DSU struct {
	parentOrSize []int
	n            int
}

// newDsu :
func newDsu(n int) *DSU {
	var d DSU
	d.n = n
	d.parentOrSize = make([]int, n)
	for i := 0; i < n; i++ {
		d.parentOrSize[i] = -1
	}
	return &d
}

// Merge :
func (d DSU) Merge(a, b int) int {
	x, y := d.Leader(a), d.Leader(b)
	if x == y {
		return x
	}
	if -d.parentOrSize[x] < -d.parentOrSize[y] {
		x, y = y, x
	}
	d.parentOrSize[x] += d.parentOrSize[y]
	d.parentOrSize[y] = x
	return x
}

// Same :
func (d DSU) Same(a, b int) bool {
	return d.Leader(a) == d.Leader(b)
}

// Leader :
func (d DSU) Leader(a int) int {
	if d.parentOrSize[a] < 0 {
		return a
	}
	d.parentOrSize[a] = d.Leader(d.parentOrSize[a])
	return d.parentOrSize[a]
}

// Size :
func (d DSU) Size(a int) int {
	return -d.parentOrSize[d.Leader(a)]
}

// Groups : original implement
func (d DSU) Groups() [][]int {
	m := make(map[int][]int)
	for i := 0; i < d.n; i++ {
		x := d.Leader(i)
		if x < 0 {
			m[i] = append(m[i], i)
		} else {
			m[x] = append(m[x], i)
		}
	}
	ret := make([][]int, len(m))
	idx := 0
	for _, e := range m {
		ret[idx] = make([]int, len(e))
		copy(ret[idx], e)
		idx++
	}
	return ret
}

type pair struct {
	x, y int
}

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer([]byte{}, math.MaxInt32)
	// this template is new version.
	// use getI(), getS(), getInts(), getF()
	N, M := getI(), getI()
	box := make([][]pair, M)
	col := make([][]pair, N)
	for i := 0; i < N; i++ {
		b, c := getI()-1, getI()-1
		box[b] = append(box[b], pair{c, i})
		col[c] = append(col[c], pair{b, i})
	}
	uf := newDsu(N)
	for _, e := range box {
		if len(e) > 1 {
			p := e[0].y
			for _, v := range e {
				uf.Merge(p, v.y)
			}
		}
	}

	ans := 0
	for _, e := range col {
		if len(e) > 1 {
			p := e[0].y
			for _, v := range e {
				if uf.Same(p, v.y) == false {
					uf.Merge(p, v.y)
					ans++
				}
			}
		}
	}
	out(ans)
}
0