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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
93 changes: 64 additions & 29 deletions content/3_Silver/Graph_Traversal.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,49 @@ public class BuildingTeams {
```

</JavaSection>
<PySection>

<Warning>
You have to submit with CPython (not PyPy3) to avoid exceeding the time limit.
</Warning>

```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 +1253,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
Comment on lines -1223 to -1224
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just now realizing that this is replacing an old sol
uneasy about this bc the old one was consistent w/ java and c++, while this one isn't

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Updated/changed the pre-existing Python BFS solution as I had a simpler, faster solution.

Yeah, I mentioned it in the description. If that's a concern, we just just use the old one. If so, I can try to see if there are any optimizations that can be made while still keeping it consistent with the other language's solutions (unless it's exactly the same).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can try to see if there are any optimizations that can be made while still keeping it consistent

i'd love this

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