結果

問題 No.1103 Directed Length Sum
ユーザー tatt61880
提出日時 2021-04-05 16:39:17
言語 Kuin
(KuinC++ v.2021.9.17)
結果
AC  
実行時間 2,499 ms / 3,000 ms
コード長 1,433 bytes
コンパイル時間 2,715 ms
コンパイル使用メモリ 149,860 KB
実行使用メモリ 394,240 KB
最終ジャッジ日時 2024-09-16 12:25:50
合計ジャッジ時間 24,005 ms
ジャッジサーバーID
(参考情報)
judge6 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

func main()
	const mod: int :: 1000000007
	class Node()
		+var parent: Node
		+var children: list<Node>
		+var depth: int
		+var num: int
		*func ctor()
			do me.parent :: null
			do me.children :: #list<Node>
		end func
		+func calcDepth()
			if(me.parent =& null)
				do me.depth :: 1
			else
				do me.depth :: me.parent.depth + 1
			end if
			do me.children.head()
			for i(0, ^me.children - 1)
				do me.children.get().calcDepth()
				do me.children.next()
			end for
		end func
		+func calcNum()
			if(^me.children = 0)
				do me.num :: 1
				ret
			end if
			do me.children.head()
			for i(0, ^me.children - 1)
				do me.children.get().calcNum()
				do me.num :+ me.children.get().num
				do me.children.next()
			end for
			do me.num :+ 1
		end func
	end class
	
	var n: int :: cui@inputInt()
	var nodes: []Node :: #[n]Node
	for i(0, n - 1)
		do nodes[i] :: #Node
	end for
	var as: []int :: #[n - 1]int
	var bs: []int :: #[n - 1]int
	for i(0, n - 2)
		var a: int :: cui@inputInt() - 1
		var b: int :: cui@inputInt() - 1
		do as[i] :: a
		do bs[i] :: b
		do nodes[a].children.add(nodes[b])
		do nodes[b].parent :: nodes[a]
	end for
	var root: Node
	for i(0, n - 1)
		if(nodes[i].parent =& null)
			do root :: nodes[i]
		end if
	end for
	
	do root.calcDepth()
	do root.calcNum()
	
	var ans: int :: 0
	for i(0, n - 2)
		do ans :+ nodes[as[i]].depth * nodes[bs[i]].num
		do ans :% mod
	end for
	do cui@print("\{ans}\n")
end func
0