結果

問題 No.1103 Directed Length Sum
ユーザー aru aruaru aru
提出日時 2020-10-25 21:44:09
言語 Go
(1.22.1)
結果
AC  
実行時間 647 ms / 3,000 ms
コード長 1,954 bytes
コンパイル時間 12,047 ms
コンパイル使用メモリ 211,096 KB
実行使用メモリ 181,520 KB
最終ジャッジ日時 2023-09-29 02:19:01
合計ジャッジ時間 19,094 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,356 KB
testcase_01 AC 1 ms
4,356 KB
testcase_02 AC 384 ms
181,520 KB
testcase_03 AC 171 ms
61,480 KB
testcase_04 AC 358 ms
33,124 KB
testcase_05 AC 647 ms
56,200 KB
testcase_06 AC 224 ms
22,608 KB
testcase_07 AC 35 ms
7,868 KB
testcase_08 AC 75 ms
10,560 KB
testcase_09 AC 21 ms
5,540 KB
testcase_10 AC 97 ms
14,172 KB
testcase_11 AC 390 ms
37,284 KB
testcase_12 AC 224 ms
22,620 KB
testcase_13 AC 107 ms
14,188 KB
testcase_14 AC 15 ms
5,528 KB
testcase_15 AC 172 ms
18,408 KB
testcase_16 AC 453 ms
41,492 KB
testcase_17 AC 474 ms
41,528 KB
testcase_18 AC 100 ms
14,180 KB
testcase_19 AC 409 ms
37,308 KB
testcase_20 AC 25 ms
6,108 KB
testcase_21 AC 64 ms
9,964 KB
testcase_22 AC 340 ms
30,996 KB
testcase_23 AC 190 ms
20,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

var sc = bufio.NewScanner(os.Stdin)
var wr = bufio.NewWriter(os.Stdout)

func out(x ...interface{}) {
	fmt.Fprintln(wr, x...)
}

func getI() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func getF() float64 {
	sc.Scan()
	i, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int) []int {
	ret := make([]int, N)
	for i := 0; i < N; i++ {
		ret[i] = getI()
	}
	return ret
}

func getS() string {
	sc.Scan()
	return sc.Text()
}

// min, max, asub, absなど基本関数
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func asub(a, b int) int {
	if a > b {
		return a - b
	}
	return b - a
}

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

var sz []int
var dp []int

const mod = int(1e9 + 7)

func dfs(p int) {
	sz[p] = 1
	for _, e := range node[p] {
		dfs(e)
		sz[p] += sz[e]
		sz[p] %= mod
		dp[p] += dp[e]
		dp[p] %= mod
	}
	dp[p] += sz[p] - 1
	if dp[p] < 0 {
		dp[p] += mod
	}
}

var node [][]int
var cnt int

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer([]byte{}, 1000000)
	// this template is new version.
	// use getI(), getS(), getInts(), getF()
	N := getI()
	node = make([][]int, N)
	sub := make([]int, N)
	for i := 0; i < N-1; i++ {
		from, to := getI()-1, getI()-1
		node[from] = append(node[from], to)
		sub[to]++
	}
	top := 0
	for i := 0; i < N; i++ {
		if sub[i] == 0 {
			top = i
		}
	}

	sz = make([]int, N)
	dp = make([]int, N)
	dfs(top)
	ans := 0

	for i := 0; i < N; i++ {
		ans += dp[i]
		ans %= mod
	}
	out(ans)
}
0