Submission #717927


Source Code Expand

M=10**9+7
n=gets.to_i
$edges=Array.new(n){Array.new}
(n-1).times{
	a,b=gets.split.map(&:to_i)
	a-=1
	b-=1
	$edges[a]<<b
	$edges[b]<<a
}

$dp={}
def dfs(node,color,parent)
	return $dp[[node,color]] if $dp[[node,color]]
	ret=1
	if color==:black
		$edges[node].each{|next_node|
			next if next_node==parent
			ret=(ret*dfs(next_node,:white,node))%M
		}
	else
		$edges[node].each{|next_node|
			next if next_node==parent
			ret=(ret*(dfs(next_node,:white,node)+dfs(next_node,:black,node)))%M
		}		
	end
	$dp[[node,color]]=ret
end

puts (dfs(0,:black,-1)+dfs(0,:white,-1))%M

Submission Info

Submission Time
Task D - 塗り絵
User urutom
Language Ruby (2.3.3)
Score 100
Code Size 598 Byte
Status AC
Exec Time 1647 ms
Memory 35708 KB

Judge Result

Set Name All
Score / Max Score 100 / 100
Status
AC × 22
Set Name Test Cases
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt
Case Name Status Exec Time Memory
000.txt AC 18 ms 1788 KB
001.txt AC 18 ms 1788 KB
002.txt AC 1227 ms 27516 KB
003.txt AC 1606 ms 35324 KB
004.txt AC 1018 ms 23292 KB
005.txt AC 1647 ms 35324 KB
006.txt AC 1433 ms 33276 KB
007.txt AC 1640 ms 35196 KB
008.txt AC 1114 ms 24700 KB
009.txt AC 1641 ms 35068 KB
010.txt AC 1134 ms 25212 KB
011.txt AC 1619 ms 35068 KB
012.txt AC 954 ms 23036 KB
013.txt AC 1616 ms 35196 KB
014.txt AC 739 ms 17788 KB
015.txt AC 1589 ms 35580 KB
016.txt AC 962 ms 22396 KB
017.txt AC 1622 ms 34940 KB
018.txt AC 294 ms 8060 KB
019.txt AC 1639 ms 34940 KB
020.txt AC 170 ms 5244 KB
021.txt AC 1586 ms 35708 KB