結果
| 問題 |
No.806 木を道に
|
| コンテスト | |
| ユーザー |
tsuchinaga
|
| 提出日時 | 2019-05-28 14:17:48 |
| 言語 | Go (1.23.4) |
| 結果 |
AC
|
| 実行時間 | 16 ms / 2,000 ms |
| コード長 | 891 bytes |
| コンパイル時間 | 11,389 ms |
| コンパイル使用メモリ | 222,520 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-09-17 15:39:52 |
| 合計ジャッジ時間 | 10,623 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge6 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 27 |
ソースコード
package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func main() {
var n, a, b int
_, _ = fmt.Scan(&n)
sc := bufio.NewScanner(os.Stdin)
sc.Split(bufio.ScanWords)
// 枝情報の取り出し
branches := make([]int, n)
for i := 1; i < n; i++ {
sc.Scan()
a, _ = strconv.Atoi(sc.Text())
sc.Scan()
b, _ = strconv.Atoi(sc.Text())
branches[a-1]++
branches[b-1]++
}
// fmt.Println(branches)
// 枝の数で集計する
nodes := make(map[int]int) // 枝の数: ノードの数
for _, b := range branches {
nodes[b]++
}
// fmt.Println(nodes)
// 枝を3本以上持つノードからはぎとる
ans := 0
for k, v := range nodes {
if k > 2 {
ans += (k - 2) * v
}
}
// fmt.Println(ans)
// 枝の数が2本のノードがn-2以上あるなら、n-2になるようにはぎとる
if nodes[2] > n-2 {
ans += nodes[2] - (n - 2)
}
fmt.Println(ans)
}
tsuchinaga