File tree 1 file changed +25
-25
lines changed
solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony
1 file changed +25
-25
lines changed Original file line number Diff line number Diff line change @@ -81,31 +81,31 @@ tags:
81
81
82
82
``` python
83
83
class Solution :
84
- def waysToBuildRooms (self , prevRoom : List[int ]) -> int :
85
- modulo = 10 ** 9 + 7
86
- ingoing = defaultdict(set )
87
- outgoing = defaultdict(set )
88
-
89
- for i in range (1 , len (prevRoom)):
90
- ingoing[i].add(prevRoom[i])
91
- outgoing[prevRoom[i]].add(i)
92
- ans = [1 ]
93
-
94
- def recurse (i ):
95
- if len (outgoing[i]) == 0 :
96
- return 1 # just self
97
-
98
- nodes_in_tree = 0
99
- for v in outgoing[i]:
100
- cn = recurse(v)
101
- if nodes_in_tree != 0 :
102
- ans[0 ] *= comb(nodes_in_tree + cn, cn)
103
- ans[0 ] %= modulo
104
- nodes_in_tree += cn
105
- return nodes_in_tree + 1
106
-
107
- recurse(0 )
108
- return ans[0 ] % modulo
84
+ def waysToBuildRooms (self , prevRoom : List[int ]) -> int :
85
+ modulo = 10 ** 9 + 7
86
+ ingoing = defaultdict(set )
87
+ outgoing = defaultdict(set )
88
+
89
+ for i in range (1 , len (prevRoom)):
90
+ ingoing[i].add(prevRoom[i])
91
+ outgoing[prevRoom[i]].add(i)
92
+ ans = [1 ]
93
+
94
+ def recurse (i ):
95
+ if len (outgoing[i]) == 0 :
96
+ return 1
97
+
98
+ nodes_in_tree = 0
99
+ for v in outgoing[i]:
100
+ cn = recurse(v)
101
+ if nodes_in_tree != 0 :
102
+ ans[0 ] *= comb(nodes_in_tree + cn, cn)
103
+ ans[0 ] %= modulo
104
+ nodes_in_tree += cn
105
+ return nodes_in_tree + 1
106
+
107
+ recurse(0 )
108
+ return ans[0 ] % modulo
109
109
```
110
110
111
111
#### Java
You can’t perform that action at this time.
0 commit comments