結果

問題 No.806 木を道に
ユーザー ccppjsrbccppjsrb
提出日時 2020-05-27 13:06:32
言語 Go
(1.22.1)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 1,964 bytes
コンパイル時間 12,017 ms
コンパイル使用メモリ 208,496 KB
実行使用メモリ 26,396 KB
最終ジャッジ日時 2023-08-03 05:58:41
合計ジャッジ時間 13,916 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 9 ms
5,548 KB
testcase_11 AC 7 ms
5,540 KB
testcase_12 AC 42 ms
12,092 KB
testcase_13 AC 30 ms
9,980 KB
testcase_14 AC 37 ms
10,036 KB
testcase_15 AC 43 ms
12,088 KB
testcase_16 AC 14 ms
5,428 KB
testcase_17 AC 37 ms
10,024 KB
testcase_18 AC 4 ms
5,512 KB
testcase_19 AC 8 ms
5,548 KB
testcase_20 AC 37 ms
10,028 KB
testcase_21 AC 21 ms
7,728 KB
testcase_22 AC 50 ms
12,124 KB
testcase_23 AC 47 ms
12,128 KB
testcase_24 AC 31 ms
22,232 KB
testcase_25 AC 41 ms
26,396 KB
testcase_26 AC 10 ms
7,816 KB
testcase_27 AC 31 ms
16,192 KB
testcase_28 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}
func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0
	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 2
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}
	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	solve(scanner, writer)
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	ee := make([][]edge, n)
	for i := 0; i < n-1; i++ {
		a := getNextInt(scanner) - 1
		b := getNextInt(scanner) - 1
		appendEdge(a, b, i, ee)
		appendEdge(b, a, i, ee)
	}
	fmt.Fprintln(writer, dfs(0, -1, ee))
}
func dfs(c, p int, ee [][]edge) int {
	cnt := len(ee[c]) - 2
	if len(ee[c]) == 1 {
		if p != -1 {
			return 0
		}
		cnt = 0
	}
	for _, e := range ee[c] {
		if e.to == p {
			continue
		}
		cnt += dfs(e.to, c, ee)
	}

	return cnt
}
func appendEdge(from, to, id int, ee [][]edge) {
	ee[from] = append(ee[from], edge{
		to: to,
		id: id,
	})
}

type edge struct {
	to, id int
}
0