結果
| 問題 |
No.2072 Anatomy
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-01-10 21:54:49 |
| 言語 | Go (1.23.4) |
| 結果 |
AC
|
| 実行時間 | 60 ms / 2,000 ms |
| コード長 | 1,785 bytes |
| コンパイル時間 | 10,259 ms |
| コンパイル使用メモリ | 234,676 KB |
| 実行使用メモリ | 11,736 KB |
| 最終ジャッジ日時 | 2024-12-21 12:20:34 |
| 合計ジャッジ時間 | 14,073 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 27 |
ソースコード
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()
}