From 375f3e312e17702d90fa50ed9be3a4e6fd7c1c25 Mon Sep 17 00:00:00 2001
From: Jason Sun <117045786+CryoJS@users.noreply.github.com>
Date: Sat, 9 Nov 2024 21:07:02 -0500
Subject: [PATCH 01/10] Update content/3_Silver/Graph_Traversal.mdx
---
content/3_Silver/Graph_Traversal.mdx | 101 +++++++++++++++++----------
1 file changed, 65 insertions(+), 36 deletions(-)
diff --git a/content/3_Silver/Graph_Traversal.mdx b/content/3_Silver/Graph_Traversal.mdx
index adc926ed5e..5ad8b09f61 100644
--- a/content/3_Silver/Graph_Traversal.mdx
+++ b/content/3_Silver/Graph_Traversal.mdx
@@ -1082,6 +1082,44 @@ public class BuildingTeams {
```
+
+
+Note that the below solution passes in CPython3 but exceeds the time limit on test case #7 in PyPy3.
+
+```py
+import sys
+input = sys.stdin.readline
+
+sys.setrecursionlimit(int(1e5 + 1))
+
+n, m = map(int, input().strip().split())
+adj = [[] for _ in range(n)]
+team = [0] * n # 0 -> has not been assigned a team yet
+
+for _ in range(m):
+ a, b = map(int, map(int, input().strip().split()))
+ adj[a - 1].append(b - 1)
+ adj[b - 1].append(a - 1)
+
+def dfs(node):
+ for next_node in adj[node]:
+ if team[next_node]:
+ if team[next_node] == team[node]:
+ print("IMPOSSIBLE")
+ raise SystemExit
+ else:
+ team[next_node] = 2 if team[node] == 1 else 1
+ dfs(next_node)
+
+for node in range(n):
+ if not team[node]:
+ team[node] = 1
+ dfs(node)
+
+print(*team)
+```
+
+
### BFS Solution
@@ -1210,46 +1248,37 @@ public class BuildingTeams {
```py
+import sys
from collections import deque
+input = sys.stdin.readline
-n, m = map(int, input().split())
-
+n, m = map(int, input().strip().split())
adj = [[] for _ in range(n)]
-for _ in range(m):
- a, b = map(int, input().split())
- adj[a - 1].append(b - 1)
- adj[b - 1].append(a - 1)
-
-assigned = [0 for _ in range(n)]
-valid = True
-for i in range(n):
- if assigned[i] != 0:
- continue
-
- assigned[i] = 1
- todo = deque([i])
- while todo:
- curr = todo.popleft()
- n_color = 2 if assigned[curr] == 1 else 1
- for next_ in adj[curr]:
- if assigned[next_] != 0:
- if assigned[next_] != n_color:
- valid = False
- break
- else:
- assigned[next_] = n_color
- todo.append(next_)
-
- if not valid:
- break
+team = [0] * n # 0 -> has not been assigned a team yet
- if not valid:
- break
-
-if valid:
- print(" ".join(str(i) for i in assigned))
-else:
- print("IMPOSSIBLE")
+for _ in range(m):
+ a, b = map(int, map(int, input().strip().split()))
+ adj[a - 1].append(b - 1)
+ adj[b - 1].append(a - 1)
+
+for node in range(n):
+ if not team[node]:
+ team[node] = 1
+ queue = deque([node])
+
+ while queue:
+ node = queue.popleft()
+
+ for next_node in adj[node]:
+ if team[next_node]:
+ if team[next_node] == team[node]:
+ print("IMPOSSIBLE")
+ raise SystemExit
+ else:
+ team[next_node] = 2 if team[node] == 1 else 1
+ queue.append(next_node)
+
+print(*team)
```
From 37455a882138f715dd1c1eb4ba8c1cce4add986e Mon Sep 17 00:00:00 2001
From: "pre-commit-ci[bot]"
<66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date: Sun, 10 Nov 2024 02:15:58 +0000
Subject: [PATCH 02/10] [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
---
content/3_Silver/Graph_Traversal.mdx | 72 +++++++++++++++-------------
1 file changed, 38 insertions(+), 34 deletions(-)
diff --git a/content/3_Silver/Graph_Traversal.mdx b/content/3_Silver/Graph_Traversal.mdx
index 5ad8b09f61..f3d11faaaf 100644
--- a/content/3_Silver/Graph_Traversal.mdx
+++ b/content/3_Silver/Graph_Traversal.mdx
@@ -1088,33 +1088,36 @@ Note that the below solution passes in CPython3 but exceeds the time limit on te
```py
import sys
+
input = sys.stdin.readline
sys.setrecursionlimit(int(1e5 + 1))
n, m = map(int, input().strip().split())
adj = [[] for _ in range(n)]
-team = [0] * n # 0 -> has not been assigned a team yet
+team = [0] * n # 0 -> has not been assigned a team yet
for _ in range(m):
- a, b = map(int, map(int, input().strip().split()))
- adj[a - 1].append(b - 1)
- adj[b - 1].append(a - 1)
+ a, b = map(int, map(int, input().strip().split()))
+ adj[a - 1].append(b - 1)
+ adj[b - 1].append(a - 1)
+
def dfs(node):
- for next_node in adj[node]:
- if team[next_node]:
- if team[next_node] == team[node]:
- print("IMPOSSIBLE")
- raise SystemExit
- else:
- team[next_node] = 2 if team[node] == 1 else 1
- dfs(next_node)
+ for next_node in adj[node]:
+ if team[next_node]:
+ if team[next_node] == team[node]:
+ print("IMPOSSIBLE")
+ raise SystemExit
+ else:
+ team[next_node] = 2 if team[node] == 1 else 1
+ dfs(next_node)
+
for node in range(n):
- if not team[node]:
- team[node] = 1
- dfs(node)
+ if not team[node]:
+ team[node] = 1
+ dfs(node)
print(*team)
```
@@ -1250,33 +1253,34 @@ public class BuildingTeams {
```py
import sys
from collections import deque
+
input = sys.stdin.readline
n, m = map(int, input().strip().split())
adj = [[] for _ in range(n)]
-team = [0] * n # 0 -> has not been assigned a team yet
+team = [0] * n # 0 -> has not been assigned a team yet
for _ in range(m):
- a, b = map(int, map(int, input().strip().split()))
- adj[a - 1].append(b - 1)
- adj[b - 1].append(a - 1)
+ a, b = map(int, map(int, input().strip().split()))
+ adj[a - 1].append(b - 1)
+ adj[b - 1].append(a - 1)
for node in range(n):
- if not team[node]:
- team[node] = 1
- queue = deque([node])
-
- while queue:
- node = queue.popleft()
-
- for next_node in adj[node]:
- if team[next_node]:
- if team[next_node] == team[node]:
- print("IMPOSSIBLE")
- raise SystemExit
- else:
- team[next_node] = 2 if team[node] == 1 else 1
- queue.append(next_node)
+ if not team[node]:
+ team[node] = 1
+ queue = deque([node])
+
+ while queue:
+ node = queue.popleft()
+
+ for next_node in adj[node]:
+ if team[next_node]:
+ if team[next_node] == team[node]:
+ print("IMPOSSIBLE")
+ raise SystemExit
+ else:
+ team[next_node] = 2 if team[node] == 1 else 1
+ queue.append(next_node)
print(*team)
```
From c3b0443ca02bc58feb95359e87d5921b15b306f8 Mon Sep 17 00:00:00 2001
From: Jason Sun <117045786+CryoJS@users.noreply.github.com>
Date: Sat, 9 Nov 2024 23:38:14 -0500
Subject: [PATCH 03/10] Update content/3_Silver/Graph_Traversal.mdx
---
content/3_Silver/Graph_Traversal.mdx | 76 ++++++++++++++--------------
1 file changed, 37 insertions(+), 39 deletions(-)
diff --git a/content/3_Silver/Graph_Traversal.mdx b/content/3_Silver/Graph_Traversal.mdx
index f3d11faaaf..0a31dd4825 100644
--- a/content/3_Silver/Graph_Traversal.mdx
+++ b/content/3_Silver/Graph_Traversal.mdx
@@ -1084,40 +1084,39 @@ public class BuildingTeams {
-Note that the below solution passes in CPython3 but exceeds the time limit on test case #7 in PyPy3.
+
+You have to submit with CPython (not PyPy3) to avoid exceeding the time limit.
+
```py
import sys
-
input = sys.stdin.readline
sys.setrecursionlimit(int(1e5 + 1))
n, m = map(int, input().strip().split())
adj = [[] for _ in range(n)]
-team = [0] * n # 0 -> has not been assigned a team yet
+team = [0] * n # 0 -> has not been assigned a team yet
for _ in range(m):
- a, b = map(int, map(int, input().strip().split()))
- adj[a - 1].append(b - 1)
- adj[b - 1].append(a - 1)
-
+ a, b = map(int, map(int, input().strip().split()))
+ adj[a - 1].append(b - 1)
+ adj[b - 1].append(a - 1)
def dfs(node):
- for next_node in adj[node]:
- if team[next_node]:
- if team[next_node] == team[node]:
- print("IMPOSSIBLE")
- raise SystemExit
- else:
- team[next_node] = 2 if team[node] == 1 else 1
- dfs(next_node)
-
+ for next_node in adj[node]:
+ if team[next_node]:
+ if team[next_node] == team[node]:
+ print("IMPOSSIBLE")
+ raise SystemExit
+ else:
+ team[next_node] = 2 if team[node] == 1 else 1
+ dfs(next_node)
for node in range(n):
- if not team[node]:
- team[node] = 1
- dfs(node)
+ if not team[node]:
+ team[node] = 1
+ dfs(node)
print(*team)
```
@@ -1253,34 +1252,33 @@ public class BuildingTeams {
```py
import sys
from collections import deque
-
input = sys.stdin.readline
n, m = map(int, input().strip().split())
adj = [[] for _ in range(n)]
-team = [0] * n # 0 -> has not been assigned a team yet
+team = [0] * n # 0 -> has not been assigned a team yet
for _ in range(m):
- a, b = map(int, map(int, input().strip().split()))
- adj[a - 1].append(b - 1)
- adj[b - 1].append(a - 1)
+ a, b = map(int, map(int, input().strip().split()))
+ adj[a - 1].append(b - 1)
+ adj[b - 1].append(a - 1)
for node in range(n):
- if not team[node]:
- team[node] = 1
- queue = deque([node])
-
- while queue:
- node = queue.popleft()
-
- for next_node in adj[node]:
- if team[next_node]:
- if team[next_node] == team[node]:
- print("IMPOSSIBLE")
- raise SystemExit
- else:
- team[next_node] = 2 if team[node] == 1 else 1
- queue.append(next_node)
+ if not team[node]:
+ team[node] = 1
+ queue = deque([node])
+
+ while queue:
+ node = queue.popleft()
+
+ for next_node in adj[node]:
+ if team[next_node]:
+ if team[next_node] == team[node]:
+ print("IMPOSSIBLE")
+ raise SystemExit
+ else:
+ team[next_node] = 2 if team[node] == 1 else 1
+ queue.append(next_node)
print(*team)
```
From 4782a68bb0e59d4d2b501196374320f323d0a5f8 Mon Sep 17 00:00:00 2001
From: "pre-commit-ci[bot]"
<66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date: Sun, 10 Nov 2024 04:39:23 +0000
Subject: [PATCH 04/10] [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
---
content/3_Silver/Graph_Traversal.mdx | 72 +++++++++++++++-------------
1 file changed, 38 insertions(+), 34 deletions(-)
diff --git a/content/3_Silver/Graph_Traversal.mdx b/content/3_Silver/Graph_Traversal.mdx
index 0a31dd4825..d123b351bd 100644
--- a/content/3_Silver/Graph_Traversal.mdx
+++ b/content/3_Silver/Graph_Traversal.mdx
@@ -1090,33 +1090,36 @@ You have to submit with CPython (not PyPy3) to avoid exceeding the time limit.
```py
import sys
+
input = sys.stdin.readline
sys.setrecursionlimit(int(1e5 + 1))
n, m = map(int, input().strip().split())
adj = [[] for _ in range(n)]
-team = [0] * n # 0 -> has not been assigned a team yet
+team = [0] * n # 0 -> has not been assigned a team yet
for _ in range(m):
- a, b = map(int, map(int, input().strip().split()))
- adj[a - 1].append(b - 1)
- adj[b - 1].append(a - 1)
+ a, b = map(int, map(int, input().strip().split()))
+ adj[a - 1].append(b - 1)
+ adj[b - 1].append(a - 1)
+
def dfs(node):
- for next_node in adj[node]:
- if team[next_node]:
- if team[next_node] == team[node]:
- print("IMPOSSIBLE")
- raise SystemExit
- else:
- team[next_node] = 2 if team[node] == 1 else 1
- dfs(next_node)
+ for next_node in adj[node]:
+ if team[next_node]:
+ if team[next_node] == team[node]:
+ print("IMPOSSIBLE")
+ raise SystemExit
+ else:
+ team[next_node] = 2 if team[node] == 1 else 1
+ dfs(next_node)
+
for node in range(n):
- if not team[node]:
- team[node] = 1
- dfs(node)
+ if not team[node]:
+ team[node] = 1
+ dfs(node)
print(*team)
```
@@ -1252,33 +1255,34 @@ public class BuildingTeams {
```py
import sys
from collections import deque
+
input = sys.stdin.readline
n, m = map(int, input().strip().split())
adj = [[] for _ in range(n)]
-team = [0] * n # 0 -> has not been assigned a team yet
+team = [0] * n # 0 -> has not been assigned a team yet
for _ in range(m):
- a, b = map(int, map(int, input().strip().split()))
- adj[a - 1].append(b - 1)
- adj[b - 1].append(a - 1)
+ a, b = map(int, map(int, input().strip().split()))
+ adj[a - 1].append(b - 1)
+ adj[b - 1].append(a - 1)
for node in range(n):
- if not team[node]:
- team[node] = 1
- queue = deque([node])
-
- while queue:
- node = queue.popleft()
-
- for next_node in adj[node]:
- if team[next_node]:
- if team[next_node] == team[node]:
- print("IMPOSSIBLE")
- raise SystemExit
- else:
- team[next_node] = 2 if team[node] == 1 else 1
- queue.append(next_node)
+ if not team[node]:
+ team[node] = 1
+ queue = deque([node])
+
+ while queue:
+ node = queue.popleft()
+
+ for next_node in adj[node]:
+ if team[next_node]:
+ if team[next_node] == team[node]:
+ print("IMPOSSIBLE")
+ raise SystemExit
+ else:
+ team[next_node] = 2 if team[node] == 1 else 1
+ queue.append(next_node)
print(*team)
```
From a53d5446c4135c44fbfbf41912469f5a1d3bd1ce Mon Sep 17 00:00:00 2001
From: Jason Sun <117045786+CryoJS@users.noreply.github.com>
Date: Sun, 10 Nov 2024 21:27:52 -0500
Subject: [PATCH 05/10] Update content/3_Silver/Graph_Traversal.mdx
---
content/3_Silver/Graph_Traversal.mdx | 48 +++++++++++++---------------
1 file changed, 22 insertions(+), 26 deletions(-)
diff --git a/content/3_Silver/Graph_Traversal.mdx b/content/3_Silver/Graph_Traversal.mdx
index d123b351bd..9d3eed4b66 100644
--- a/content/3_Silver/Graph_Traversal.mdx
+++ b/content/3_Silver/Graph_Traversal.mdx
@@ -1090,37 +1090,34 @@ You have to submit with CPython (not PyPy3) to avoid exceeding the time limit.
```py
import sys
-
input = sys.stdin.readline
-sys.setrecursionlimit(int(1e5 + 1))
-
n, m = map(int, input().strip().split())
-adj = [[] for _ in range(n)]
-team = [0] * n # 0 -> has not been assigned a team yet
+sys.setrecursionlimit(n + 3) # preventing recursion limit exceeded error
+adj = [[] for _ in range(n)]
+team = [0] * n # 0: not assigned yet, 1: team 1, 2: team 2
+
for _ in range(m):
- a, b = map(int, map(int, input().strip().split()))
- adj[a - 1].append(b - 1)
- adj[b - 1].append(a - 1)
-
-
-def dfs(node):
- for next_node in adj[node]:
- if team[next_node]:
- if team[next_node] == team[node]:
- print("IMPOSSIBLE")
- raise SystemExit
- else:
- team[next_node] = 2 if team[node] == 1 else 1
- dfs(next_node)
-
-
+ a, b = map(int, map(int, input().strip().split()))
+ adj[a - 1].append(b - 1)
+ adj[b - 1].append(a - 1)
+
+def dfs(node: int):
+ for next_node in adj[node]:
+ if team[next_node]:
+ if team[next_node] == team[node]:
+ print("IMPOSSIBLE")
+ raise SystemExit
+ else:
+ team[next_node] = 2 if team[node] == 1 else 1
+ dfs(next_node)
+
for node in range(n):
- if not team[node]:
- team[node] = 1
- dfs(node)
-
+ if not team[node]:
+ team[node] = 1
+ dfs(node)
+
print(*team)
```
@@ -1255,7 +1252,6 @@ public class BuildingTeams {
```py
import sys
from collections import deque
-
input = sys.stdin.readline
n, m = map(int, input().strip().split())
From 2d1ebab6a85b1c134a2abd23e4cfb29026d47db7 Mon Sep 17 00:00:00 2001
From: "pre-commit-ci[bot]"
<66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date: Mon, 11 Nov 2024 02:29:13 +0000
Subject: [PATCH 06/10] [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
---
content/3_Silver/Graph_Traversal.mdx | 44 +++++++++++++++-------------
1 file changed, 24 insertions(+), 20 deletions(-)
diff --git a/content/3_Silver/Graph_Traversal.mdx b/content/3_Silver/Graph_Traversal.mdx
index 9d3eed4b66..08ef9ed2ab 100644
--- a/content/3_Silver/Graph_Traversal.mdx
+++ b/content/3_Silver/Graph_Traversal.mdx
@@ -1090,34 +1090,37 @@ You have to submit with CPython (not PyPy3) to avoid exceeding the time limit.
```py
import sys
+
input = sys.stdin.readline
n, m = map(int, input().strip().split())
-sys.setrecursionlimit(n + 3) # preventing recursion limit exceeded error
+sys.setrecursionlimit(n + 3) # preventing recursion limit exceeded error
adj = [[] for _ in range(n)]
-team = [0] * n # 0: not assigned yet, 1: team 1, 2: team 2
-
+team = [0] * n # 0: not assigned yet, 1: team 1, 2: team 2
+
for _ in range(m):
- a, b = map(int, map(int, input().strip().split()))
- adj[a - 1].append(b - 1)
- adj[b - 1].append(a - 1)
-
+ a, b = map(int, map(int, input().strip().split()))
+ adj[a - 1].append(b - 1)
+ adj[b - 1].append(a - 1)
+
+
def dfs(node: int):
- for next_node in adj[node]:
- if team[next_node]:
- if team[next_node] == team[node]:
- print("IMPOSSIBLE")
- raise SystemExit
- else:
- team[next_node] = 2 if team[node] == 1 else 1
- dfs(next_node)
-
+ for next_node in adj[node]:
+ if team[next_node]:
+ if team[next_node] == team[node]:
+ print("IMPOSSIBLE")
+ raise SystemExit
+ else:
+ team[next_node] = 2 if team[node] == 1 else 1
+ dfs(next_node)
+
+
for node in range(n):
- if not team[node]:
- team[node] = 1
- dfs(node)
-
+ if not team[node]:
+ team[node] = 1
+ dfs(node)
+
print(*team)
```
@@ -1252,6 +1255,7 @@ public class BuildingTeams {
```py
import sys
from collections import deque
+
input = sys.stdin.readline
n, m = map(int, input().strip().split())
From ca77264291c8c169b3c0160e987e144f79855e71 Mon Sep 17 00:00:00 2001
From: Jason Sun <117045786+CryoJS@users.noreply.github.com>
Date: Mon, 11 Nov 2024 08:24:24 -0500
Subject: [PATCH 07/10] Update content/3_Silver/Graph_Traversal.mdx
---
content/3_Silver/Graph_Traversal.mdx | 44 +++++++++++++---------------
1 file changed, 20 insertions(+), 24 deletions(-)
diff --git a/content/3_Silver/Graph_Traversal.mdx b/content/3_Silver/Graph_Traversal.mdx
index 08ef9ed2ab..076e7a60bf 100644
--- a/content/3_Silver/Graph_Traversal.mdx
+++ b/content/3_Silver/Graph_Traversal.mdx
@@ -1090,37 +1090,34 @@ You have to submit with CPython (not PyPy3) to avoid exceeding the time limit.
```py
import sys
-
input = sys.stdin.readline
-n, m = map(int, input().strip().split())
-sys.setrecursionlimit(n + 3) # preventing recursion limit exceeded error
+sys.setrecursionlimit(int(1e9)) # disable recursion limit
+n, m = map(int, input().strip().split())
adj = [[] for _ in range(n)]
team = [0] * n # 0: not assigned yet, 1: team 1, 2: team 2
-
+
for _ in range(m):
- a, b = map(int, map(int, input().strip().split()))
- adj[a - 1].append(b - 1)
- adj[b - 1].append(a - 1)
-
-
+ a, b = map(int, map(int, input().strip().split()))
+ adj[a - 1].append(b - 1)
+ adj[b - 1].append(a - 1)
+
def dfs(node: int):
- for next_node in adj[node]:
- if team[next_node]:
- if team[next_node] == team[node]:
- print("IMPOSSIBLE")
- raise SystemExit
- else:
- team[next_node] = 2 if team[node] == 1 else 1
- dfs(next_node)
-
-
+ for next_node in adj[node]:
+ if team[next_node]:
+ if team[next_node] == team[node]:
+ print("IMPOSSIBLE")
+ raise SystemExit
+ else:
+ team[next_node] = 2 if team[node] == 1 else 1
+ dfs(next_node)
+
for node in range(n):
- if not team[node]:
- team[node] = 1
- dfs(node)
-
+ if not team[node]:
+ team[node] = 1
+ dfs(node)
+
print(*team)
```
@@ -1255,7 +1252,6 @@ public class BuildingTeams {
```py
import sys
from collections import deque
-
input = sys.stdin.readline
n, m = map(int, input().strip().split())
From 02b9f5e512a5732db9cfd531ec7b264e124d4de0 Mon Sep 17 00:00:00 2001
From: "pre-commit-ci[bot]"
<66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date: Mon, 11 Nov 2024 13:25:48 +0000
Subject: [PATCH 08/10] [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
---
content/3_Silver/Graph_Traversal.mdx | 40 +++++++++++++++-------------
1 file changed, 22 insertions(+), 18 deletions(-)
diff --git a/content/3_Silver/Graph_Traversal.mdx b/content/3_Silver/Graph_Traversal.mdx
index 076e7a60bf..64b58b300c 100644
--- a/content/3_Silver/Graph_Traversal.mdx
+++ b/content/3_Silver/Graph_Traversal.mdx
@@ -1090,6 +1090,7 @@ You have to submit with CPython (not PyPy3) to avoid exceeding the time limit.
```py
import sys
+
input = sys.stdin.readline
sys.setrecursionlimit(int(1e9)) # disable recursion limit
@@ -1097,27 +1098,29 @@ sys.setrecursionlimit(int(1e9)) # disable recursion limit
n, m = map(int, input().strip().split())
adj = [[] for _ in range(n)]
team = [0] * n # 0: not assigned yet, 1: team 1, 2: team 2
-
+
for _ in range(m):
- a, b = map(int, map(int, input().strip().split()))
- adj[a - 1].append(b - 1)
- adj[b - 1].append(a - 1)
-
+ a, b = map(int, map(int, input().strip().split()))
+ adj[a - 1].append(b - 1)
+ adj[b - 1].append(a - 1)
+
+
def dfs(node: int):
- for next_node in adj[node]:
- if team[next_node]:
- if team[next_node] == team[node]:
- print("IMPOSSIBLE")
- raise SystemExit
- else:
- team[next_node] = 2 if team[node] == 1 else 1
- dfs(next_node)
-
+ for next_node in adj[node]:
+ if team[next_node]:
+ if team[next_node] == team[node]:
+ print("IMPOSSIBLE")
+ raise SystemExit
+ else:
+ team[next_node] = 2 if team[node] == 1 else 1
+ dfs(next_node)
+
+
for node in range(n):
- if not team[node]:
- team[node] = 1
- dfs(node)
-
+ if not team[node]:
+ team[node] = 1
+ dfs(node)
+
print(*team)
```
@@ -1252,6 +1255,7 @@ public class BuildingTeams {
```py
import sys
from collections import deque
+
input = sys.stdin.readline
n, m = map(int, input().strip().split())
From c66514b5c26568c8429de104f50deb9703b47632 Mon Sep 17 00:00:00 2001
From: Jason Sun <117045786+CryoJS@users.noreply.github.com>
Date: Mon, 11 Nov 2024 20:36:21 -0500
Subject: [PATCH 09/10] Update content/3_Silver/Graph_Traversal.mdx
---
content/3_Silver/Graph_Traversal.mdx | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/content/3_Silver/Graph_Traversal.mdx b/content/3_Silver/Graph_Traversal.mdx
index 64b58b300c..49c3581d3a 100644
--- a/content/3_Silver/Graph_Traversal.mdx
+++ b/content/3_Silver/Graph_Traversal.mdx
@@ -1110,7 +1110,7 @@ def dfs(node: int):
if team[next_node]:
if team[next_node] == team[node]:
print("IMPOSSIBLE")
- raise SystemExit
+ exit()
else:
team[next_node] = 2 if team[node] == 1 else 1
dfs(next_node)
@@ -1279,7 +1279,7 @@ for node in range(n):
if team[next_node]:
if team[next_node] == team[node]:
print("IMPOSSIBLE")
- raise SystemExit
+ exit()
else:
team[next_node] = 2 if team[node] == 1 else 1
queue.append(next_node)
From fd29cd848f375e2f45186636e7de4545ef4316bd Mon Sep 17 00:00:00 2001
From: Kevin Sheng <55369003+SansPapyrus683@users.noreply.github.com>
Date: Mon, 11 Nov 2024 20:16:16 -0800
Subject: [PATCH 10/10] Update Graph_Traversal.mdx
---
content/3_Silver/Graph_Traversal.mdx | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/content/3_Silver/Graph_Traversal.mdx b/content/3_Silver/Graph_Traversal.mdx
index 49c3581d3a..ce35ef29e8 100644
--- a/content/3_Silver/Graph_Traversal.mdx
+++ b/content/3_Silver/Graph_Traversal.mdx
@@ -1085,16 +1085,18 @@ public class BuildingTeams {
-You have to submit with CPython (not PyPy3) to avoid exceeding the time limit.
+
+You have to submit with CPython (not PyPy3) to avoid TLE.
+
```py
import sys
-input = sys.stdin.readline
-
sys.setrecursionlimit(int(1e9)) # disable recursion limit
+input = sys.stdin.readline
+
n, m = map(int, input().strip().split())
adj = [[] for _ in range(n)]
team = [0] * n # 0: not assigned yet, 1: team 1, 2: team 2