Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update BuildingTeams PySol content/3_Silver/Graph_Traversal.mdx #4917

Merged
merged 17 commits into from
Nov 19, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 62 additions & 29 deletions content/3_Silver/Graph_Traversal.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,47 @@ public class BuildingTeams {
```

</JavaSection>
<PySection>

Note that the below solution passes in CPython3 but exceeds the time limit on test case #7 in PyPy3.
CryoJS marked this conversation as resolved.
Show resolved Hide resolved

```py
import sys

input = sys.stdin.readline
SansPapyrus683 marked this conversation as resolved.
Show resolved Hide resolved

sys.setrecursionlimit(int(1e5 + 1))
CryoJS marked this conversation as resolved.
Show resolved Hide resolved

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):
CryoJS marked this conversation as resolved.
Show resolved Hide resolved
for next_node in adj[node]:
if team[next_node]:
if team[next_node] == team[node]:
print("IMPOSSIBLE")
raise SystemExit
CryoJS marked this conversation as resolved.
Show resolved Hide resolved
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)
```

</PySection>
</LanguageSection>

### BFS Solution
Expand Down Expand Up @@ -1210,46 +1251,38 @@ public class BuildingTeams {
<PySection>

```py
import sys
from collections import deque

n, m = map(int, input().split())
input = sys.stdin.readline
SansPapyrus683 marked this conversation as resolved.
Show resolved Hide resolved

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, input().split())
a, b = map(int, map(int, input().strip().split()))
adj[a - 1].append(b - 1)
adj[b - 1].append(a - 1)

assigned = [0 for _ in range(n)]
valid = True
SansPapyrus683 marked this conversation as resolved.
Show resolved Hide resolved
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_)
for node in range(n):
if not team[node]:
team[node] = 1
queue = deque([node])

if not valid:
break
while queue:
node = queue.popleft()

if not valid:
break
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 valid:
print(" ".join(str(i) for i in assigned))
else:
print("IMPOSSIBLE")
print(*team)
```

</PySection>
Expand Down
Loading