結果

問題 No.872 All Tree Path
ユーザー ccppjsrbccppjsrb
提出日時 2020-05-28 15:18:55
言語 Go
(1.22.1)
結果
AC  
実行時間 191 ms / 3,000 ms
コード長 2,119 bytes
コンパイル時間 12,577 ms
コンパイル使用メモリ 232,472 KB
実行使用メモリ 77,056 KB
最終ジャッジ日時 2024-04-21 05:40:10
合計ジャッジ時間 16,116 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 173 ms
24,448 KB
testcase_01 AC 170 ms
24,320 KB
testcase_02 AC 150 ms
24,448 KB
testcase_03 AC 161 ms
77,056 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 148 ms
24,652 KB
testcase_06 AC 168 ms
24,192 KB
testcase_07 AC 191 ms
24,320 KB
testcase_08 AC 11 ms
5,376 KB
testcase_09 AC 10 ms
5,376 KB
testcase_10 AC 10 ms
5,376 KB
testcase_11 AC 10 ms
5,376 KB
testcase_12 AC 11 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 1 ms
5,376 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 = 1
	}
	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++ {
		u := getNextInt(scanner) - 1
		v := getNextInt(scanner) - 1
		w := getNextInt(scanner)
		appendEdge(u, v, i, w, ee)
		appendEdge(v, u, i, w, ee)
	}
	vv := make([]int, n)
	var ans int64
	dfs(0, -1, vv, ee, &ans)
	fmt.Fprintln(writer, ans*2)
}

func dfs(c, p int, vv []int, ee [][]edge, ans *int64) int {
	cnt := 1
	for _, e := range ee[c] {
		if e.to == p {
			continue
		}
		children := dfs(e.to, c, vv, ee, ans)
		*ans += int64(children) * int64(len(vv)-children) * int64(e.w)
		cnt += children
	}
	vv[c] = cnt
	return cnt
}
func appendEdge(from, to, id, w int, ee [][]edge) {
	ee[from] = append(ee[from], edge{
		to: to,
		id: id,
		w:  w,
	})
}

type edge struct {
	to, id, w int
}
0