結果

問題 No.2072 Anatomy
ユーザー オカモトハルキオカモトハルキ
提出日時 2023-01-10 21:54:49
言語 Go
(1.22.1)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 1,785 bytes
コンパイル時間 14,824 ms
コンパイル使用メモリ 210,104 KB
実行使用メモリ 11,964 KB
最終ジャッジ日時 2023-08-23 10:21:37
合計ジャッジ時間 16,676 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 60 ms
10,232 KB
testcase_09 AC 62 ms
10,624 KB
testcase_10 AC 59 ms
9,864 KB
testcase_11 AC 75 ms
10,488 KB
testcase_12 AC 47 ms
8,108 KB
testcase_13 AC 74 ms
11,372 KB
testcase_14 AC 42 ms
7,796 KB
testcase_15 AC 40 ms
7,888 KB
testcase_16 AC 73 ms
11,380 KB
testcase_17 AC 80 ms
11,204 KB
testcase_18 AC 34 ms
7,792 KB
testcase_19 AC 72 ms
11,964 KB
testcase_20 AC 77 ms
11,964 KB
testcase_21 AC 87 ms
11,960 KB
testcase_22 AC 78 ms
11,960 KB
testcase_23 AC 78 ms
11,960 KB
testcase_24 AC 79 ms
11,960 KB
testcase_25 AC 71 ms
11,964 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 83 ms
11,960 KB
testcase_28 AC 47 ms
11,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

var sc = bufio.NewScanner(os.Stdin)

func main() {
	sc.Buffer(make([]byte, 1000), 500000)
	sc.Split(bufio.ScanWords)
	n, m := nextInt(), nextInt()
	a, b := make([]int, m), make([]int, m)
	for i := range a {
		a[i], b[i] = nextInt()-1, nextInt()-1
	}
	uf := initUnionFindTree(n)
	cost := make([]int, n)
	for i := m - 1; i >= 0; i-- {
		tmp := Max(cost[uf.getRoot(a[i])], cost[uf.getRoot(b[i])])
		uf.uniteTree(a[i], b[i])
		cost[uf.getRoot(a[i])] = tmp + 1
	}
	ans := 0
	for i := range cost {
		ans = Max(ans, cost[i])
	}
	fmt.Println(ans)
}

func Max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

type UnionFind struct {
	root []int
	rank []int
	size []int
}

func initUnionFindTree(n int) *UnionFind {
	uf := new(UnionFind)
	uf.root, uf.rank, uf.size = make([]int, n), make([]int, n), make([]int, n)
	for i := 0; i < n; i++ {
		uf.root[i] = -1
		uf.rank[i] = 0
		uf.size[i] = 1
	}
	return uf
}

func (uf UnionFind) getRoot(x int) int {
	if uf.root[x] == -1 {
		return x
	}
	uf.root[x] = uf.getRoot(uf.root[x])
	return uf.root[x]
}

func (uf UnionFind) uniteTree(x, y int) {
	rootx, rooty := uf.getRoot(x), uf.getRoot(y)
	if rootx != rooty {
		if uf.rank[rootx] > uf.rank[rooty] {
			rooty, rootx = rootx, rooty
		}
		uf.root[rootx] = rooty
		if uf.rank[rootx] == uf.rank[rooty] {
			uf.rank[rooty]++
		}
		uf.size[rooty] += uf.size[rootx]
	}
}

func (uf UnionFind) getSize(x int) int {
	rootx := uf.getRoot(x)
	return uf.size[rootx]
}

func (uf UnionFind) isSame(x, y int) bool {
	if uf.getRoot(x) == uf.getRoot(y) {
		return true
	}
	return false
}

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

func nextString() string {
	sc.Scan()
	return sc.Text()
}
0