From 9dcc1603187d67d797c4d6cb88a1d72be1f903b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sarvex=20=E2=98=A0=20Jatasra?= Date: Mon, 19 Aug 2024 06:30:40 +0530 Subject: [PATCH 1/8] Create 1916.Count Ways to Build Rooms in an Ant Colony.py --- ...nt Ways to Build Rooms in an Ant Colony.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony.py diff --git a/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony.py b/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony.py new file mode 100644 index 0000000000000..6f31d848ebb94 --- /dev/null +++ b/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony.py @@ -0,0 +1,31 @@ +from collections import defaultdict +from math import comb +from typing import List + + +class Solution: + def waysToBuildRooms(self, prevRoom: List[int]) -> int: + modulo = 10 ** 9 + 7 + ingoing = defaultdict(set) + outgoing = defaultdict(set) + + for i in range(1, len(prevRoom)): + ingoing[i].add(prevRoom[i]) + outgoing[prevRoom[i]].add(i) + ans = [1] + + def recurse(i): + if len(outgoing[i]) == 0: + return 1 # just self + + nodes_in_tree = 0 + for v in outgoing[i]: + cn = recurse(v) + if nodes_in_tree != 0: + ans[0] *= comb(nodes_in_tree + cn, cn) + ans[0] %= modulo + nodes_in_tree += cn + return nodes_in_tree + 1 + + recurse(0) + return ans[0] % modulo From 1ca96b844e61b279d1a4e8a3937ce2dd398b9f28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sarvex=20=E2=98=A0=20Jatasra?= Date: Mon, 19 Aug 2024 06:32:15 +0530 Subject: [PATCH 2/8] Update README.md for 1916 --- .../README.md | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/README.md b/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/README.md index 7ef328b99decf..7e7ef4f24b21e 100644 --- a/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/README.md +++ b/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/README.md @@ -80,7 +80,32 @@ tags: #### Python3 ```python - +class Solution: + def waysToBuildRooms(self, prevRoom: List[int]) -> int: + modulo = 10 ** 9 + 7 + ingoing = defaultdict(set) + outgoing = defaultdict(set) + + for i in range(1, len(prevRoom)): + ingoing[i].add(prevRoom[i]) + outgoing[prevRoom[i]].add(i) + ans = [1] + + def recurse(i): + if len(outgoing[i]) == 0: + return 1 # just self + + nodes_in_tree = 0 + for v in outgoing[i]: + cn = recurse(v) + if nodes_in_tree != 0: + ans[0] *= comb(nodes_in_tree + cn, cn) + ans[0] %= modulo + nodes_in_tree += cn + return nodes_in_tree + 1 + + recurse(0) + return ans[0] % modulo ``` #### Java From c4681fa1f27aa93317ad04d9a31f6eaef3e58f83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sarvex=20=E2=98=A0=20Jatasra?= Date: Mon, 19 Aug 2024 06:32:38 +0530 Subject: [PATCH 3/8] Update README_EN.md for 1916 --- .../README_EN.md | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/README_EN.md b/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/README_EN.md index c560e572345c8..b028439ae1cbd 100644 --- a/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/README_EN.md +++ b/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/README_EN.md @@ -104,7 +104,32 @@ tags: #### Python3 ```python - +class Solution: + def waysToBuildRooms(self, prevRoom: List[int]) -> int: + modulo = 10 ** 9 + 7 + ingoing = defaultdict(set) + outgoing = defaultdict(set) + + for i in range(1, len(prevRoom)): + ingoing[i].add(prevRoom[i]) + outgoing[prevRoom[i]].add(i) + ans = [1] + + def recurse(i): + if len(outgoing[i]) == 0: + return 1 # just self + + nodes_in_tree = 0 + for v in outgoing[i]: + cn = recurse(v) + if nodes_in_tree != 0: + ans[0] *= comb(nodes_in_tree + cn, cn) + ans[0] %= modulo + nodes_in_tree += cn + return nodes_in_tree + 1 + + recurse(0) + return ans[0] % modulo ``` #### Java From 7a277f7249aae2ce37ede9c87dcb0904237b717d Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 19 Aug 2024 22:26:34 +0800 Subject: [PATCH 4/8] Update 1916.Count Ways to Build Rooms in an Ant Colony.py --- ...nt Ways to Build Rooms in an Ant Colony.py | 47 +++++++++---------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony.py b/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony.py index 6f31d848ebb94..f3f7d0ad8b0c4 100644 --- a/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony.py +++ b/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony.py @@ -1,31 +1,26 @@ -from collections import defaultdict -from math import comb -from typing import List - - class Solution: - def waysToBuildRooms(self, prevRoom: List[int]) -> int: - modulo = 10 ** 9 + 7 - ingoing = defaultdict(set) - outgoing = defaultdict(set) + def waysToBuildRooms(self, prevRoom: List[int]) -> int: + modulo = 10**9 + 7 + ingoing = defaultdict(set) + outgoing = defaultdict(set) - for i in range(1, len(prevRoom)): - ingoing[i].add(prevRoom[i]) - outgoing[prevRoom[i]].add(i) - ans = [1] + for i in range(1, len(prevRoom)): + ingoing[i].add(prevRoom[i]) + outgoing[prevRoom[i]].add(i) + ans = [1] - def recurse(i): - if len(outgoing[i]) == 0: - return 1 # just self + def recurse(i): + if len(outgoing[i]) == 0: + return 1 - nodes_in_tree = 0 - for v in outgoing[i]: - cn = recurse(v) - if nodes_in_tree != 0: - ans[0] *= comb(nodes_in_tree + cn, cn) - ans[0] %= modulo - nodes_in_tree += cn - return nodes_in_tree + 1 + nodes_in_tree = 0 + for v in outgoing[i]: + cn = recurse(v) + if nodes_in_tree != 0: + ans[0] *= comb(nodes_in_tree + cn, cn) + ans[0] %= modulo + nodes_in_tree += cn + return nodes_in_tree + 1 - recurse(0) - return ans[0] % modulo + recurse(0) + return ans[0] % modulo From d9d4ef1d52b7cd931a6cbabfa7b5c46978aeb194 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 19 Aug 2024 22:26:57 +0800 Subject: [PATCH 5/8] Delete solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony.py --- ...nt Ways to Build Rooms in an Ant Colony.py | 26 ------------------- 1 file changed, 26 deletions(-) delete mode 100644 solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony.py diff --git a/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony.py b/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony.py deleted file mode 100644 index f3f7d0ad8b0c4..0000000000000 --- a/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony.py +++ /dev/null @@ -1,26 +0,0 @@ -class Solution: - def waysToBuildRooms(self, prevRoom: List[int]) -> int: - modulo = 10**9 + 7 - ingoing = defaultdict(set) - outgoing = defaultdict(set) - - for i in range(1, len(prevRoom)): - ingoing[i].add(prevRoom[i]) - outgoing[prevRoom[i]].add(i) - ans = [1] - - def recurse(i): - if len(outgoing[i]) == 0: - return 1 - - nodes_in_tree = 0 - for v in outgoing[i]: - cn = recurse(v) - if nodes_in_tree != 0: - ans[0] *= comb(nodes_in_tree + cn, cn) - ans[0] %= modulo - nodes_in_tree += cn - return nodes_in_tree + 1 - - recurse(0) - return ans[0] % modulo From f1fe6d9369f7863ce1438da811b7f190a02bdcb3 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 19 Aug 2024 22:27:34 +0800 Subject: [PATCH 6/8] Update README.md --- .../README.md | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/README.md b/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/README.md index 7e7ef4f24b21e..416d0b878b353 100644 --- a/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/README.md +++ b/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/README.md @@ -81,31 +81,31 @@ tags: ```python class Solution: - def waysToBuildRooms(self, prevRoom: List[int]) -> int: - modulo = 10 ** 9 + 7 - ingoing = defaultdict(set) - outgoing = defaultdict(set) - - for i in range(1, len(prevRoom)): - ingoing[i].add(prevRoom[i]) - outgoing[prevRoom[i]].add(i) - ans = [1] - - def recurse(i): - if len(outgoing[i]) == 0: - return 1 # just self - - nodes_in_tree = 0 - for v in outgoing[i]: - cn = recurse(v) - if nodes_in_tree != 0: - ans[0] *= comb(nodes_in_tree + cn, cn) - ans[0] %= modulo - nodes_in_tree += cn - return nodes_in_tree + 1 - - recurse(0) - return ans[0] % modulo + def waysToBuildRooms(self, prevRoom: List[int]) -> int: + modulo = 10**9 + 7 + ingoing = defaultdict(set) + outgoing = defaultdict(set) + + for i in range(1, len(prevRoom)): + ingoing[i].add(prevRoom[i]) + outgoing[prevRoom[i]].add(i) + ans = [1] + + def recurse(i): + if len(outgoing[i]) == 0: + return 1 + + nodes_in_tree = 0 + for v in outgoing[i]: + cn = recurse(v) + if nodes_in_tree != 0: + ans[0] *= comb(nodes_in_tree + cn, cn) + ans[0] %= modulo + nodes_in_tree += cn + return nodes_in_tree + 1 + + recurse(0) + return ans[0] % modulo ``` #### Java From 481bd296d4593a194005afe47c44e4118a977d57 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 19 Aug 2024 22:27:59 +0800 Subject: [PATCH 7/8] Update README_EN.md --- .../README_EN.md | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/README_EN.md b/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/README_EN.md index b028439ae1cbd..f306ec994eb05 100644 --- a/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/README_EN.md +++ b/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/README_EN.md @@ -105,31 +105,31 @@ tags: ```python class Solution: - def waysToBuildRooms(self, prevRoom: List[int]) -> int: - modulo = 10 ** 9 + 7 - ingoing = defaultdict(set) - outgoing = defaultdict(set) - - for i in range(1, len(prevRoom)): - ingoing[i].add(prevRoom[i]) - outgoing[prevRoom[i]].add(i) - ans = [1] - - def recurse(i): - if len(outgoing[i]) == 0: - return 1 # just self - - nodes_in_tree = 0 - for v in outgoing[i]: - cn = recurse(v) - if nodes_in_tree != 0: - ans[0] *= comb(nodes_in_tree + cn, cn) - ans[0] %= modulo - nodes_in_tree += cn - return nodes_in_tree + 1 - - recurse(0) - return ans[0] % modulo + def waysToBuildRooms(self, prevRoom: List[int]) -> int: + modulo = 10**9 + 7 + ingoing = defaultdict(set) + outgoing = defaultdict(set) + + for i in range(1, len(prevRoom)): + ingoing[i].add(prevRoom[i]) + outgoing[prevRoom[i]].add(i) + ans = [1] + + def recurse(i): + if len(outgoing[i]) == 0: + return 1 + + nodes_in_tree = 0 + for v in outgoing[i]: + cn = recurse(v) + if nodes_in_tree != 0: + ans[0] *= comb(nodes_in_tree + cn, cn) + ans[0] %= modulo + nodes_in_tree += cn + return nodes_in_tree + 1 + + recurse(0) + return ans[0] % modulo ``` #### Java From ae989ee20de03318abc539976b119b7238cab8d4 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 19 Aug 2024 22:28:41 +0800 Subject: [PATCH 8/8] Create Solution.py --- .../Solution.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/Solution.py diff --git a/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/Solution.py b/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/Solution.py new file mode 100644 index 0000000000000..f3f7d0ad8b0c4 --- /dev/null +++ b/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/Solution.py @@ -0,0 +1,26 @@ +class Solution: + def waysToBuildRooms(self, prevRoom: List[int]) -> int: + modulo = 10**9 + 7 + ingoing = defaultdict(set) + outgoing = defaultdict(set) + + for i in range(1, len(prevRoom)): + ingoing[i].add(prevRoom[i]) + outgoing[prevRoom[i]].add(i) + ans = [1] + + def recurse(i): + if len(outgoing[i]) == 0: + return 1 + + nodes_in_tree = 0 + for v in outgoing[i]: + cn = recurse(v) + if nodes_in_tree != 0: + ans[0] *= comb(nodes_in_tree + cn, cn) + ans[0] %= modulo + nodes_in_tree += cn + return nodes_in_tree + 1 + + recurse(0) + return ans[0] % modulo