結果
問題 |
No.806 木を道に
|
ユーザー |
![]() |
提出日時 | 2019-04-24 13:28:37 |
言語 | Go (1.23.4) |
結果 |
AC
|
実行時間 | 32 ms / 2,000 ms |
コード長 | 1,170 bytes |
コンパイル時間 | 14,950 ms |
コンパイル使用メモリ | 237,452 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-07 18:41:45 |
合計ジャッジ時間 | 16,587 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 27 |
ソースコード
package main import ( "bufio" "fmt" "math" "os" "strconv" ) func main() { var n, a, b int _, _ = fmt.Scan(&n) sc := bufio.NewScanner(os.Stdin) sc.Split(bufio.ScanWords) nodes := make([]int, n) for i := 0; i < n-1; i++ { sc.Scan() a, _ = strconv.Atoi(sc.Text()) sc.Scan() b, _ = strconv.Atoi(sc.Text()) nodes[a-1]++ nodes[b-1]++ } // fmt.Println(nodes) // 各ノードが持つ枝の数が分かったので、枝の数で集計しなおす branches := make(map[int]int) for _, n := range nodes { branches[n]++ } // fmt.Println(branches) // 1: 2, 2: n-2 になるまで一番大きいのと一番小さいのから減らして、その隣に加算する作業をする ans := 0 for { if branches[1] == 2 && branches[2] == n-2 { break } max := 0 min := math.MaxInt64 for b := range branches { if max < b { max = b } if min > b { min = b } } branches[max]-- branches[max-1]++ branches[min+1]++ branches[min]-- if branches[max] == 0 { delete(branches, max) } if branches[min] == 0 { delete(branches, min) } ans++ // fmt.Println(ans, branches) } fmt.Println(ans) }