Skip to content

Conversation

@vingoel26
Copy link
Contributor

@vingoel26 vingoel26 commented Oct 22, 2025

🎯 Problem Information

Problem Name: Building Teams
Category: Graph (Bipartite Check, DFS/BFS)
CSES Link: https://cses.fi/problemset/task/1668
Difficulty: [Medium]
#120


📝 Description

This PR adds a clean, well-documented C++ solution for the Building Teams problem from CSES.
The program determines if it is possible to divide all students into two teams such that no two friends are in the same team.
If possible, it prints one valid team assignment for each student (either 1 or 2).
If not possible, it prints IMPOSSIBLE.


🧩 Solution Approach

  • Algorithm Used: Graph Coloring (Bipartite Check using BFS or DFS).
  • Time Complexity: O(N + M) — each vertex and edge is processed once.
  • Space Complexity: O(N + M) — adjacency list + color array + queue.
  • Key Insights:
    • Represent pupils as graph nodes and friendships as undirected edges.
    • The goal is to check if the graph is bipartite — meaning we can color nodes with two colors such that no adjacent nodes share the same color.
    • Use BFS:
      • Assign color 1 to a node, and alternate colors for its neighbors.
      • If a conflict occurs (adjacent nodes have the same color), print IMPOSSIBLE.
    • Process all connected components (graph may be disconnected).
    • If successful, print the colors for all students (1 or 2).

✅ Checklist

Please ensure your PR meets these requirements:

Code Quality

  • Solution follows the required template format
  • Code is clean and well-commented (inline comments explain BFS and coloring logic)
  • Variable names are descriptive (adj, color, queue, ok)
  • Proper handling of disconnected components and conflict detection

Testing

  • Solution passes all CSES test cases
  • Tested with custom edge cases (odd cycles, disconnected graphs, fully connected components)
  • Handles large input constraints efficiently (N ≤ 1e5, M ≤ 2e5)
  • No runtime errors or timeouts (iterative BFS ensures safety)

Documentation

  • Added solution to appropriate category folder (graphs/)
  • File name follows naming convention (building_teams.cpp)
  • Added brief comment explaining the approach (top of file header block)
  • Updated any relevant documentation if needed

Style Guide

  • Uses required header (#include <bits/stdc++.h>)
  • Includes fast I/O optimization (ios::sync_with_stdio(false); cin.tie(nullptr);)
  • Uses appropriate data types (int acceptable here)
  • Follows competitive programming best practices (1-indexed adjacency)

🏷️ Type of Change

  • 🐛 Bug fix (fixes an existing solution)
  • ✨ New problem solution
  • 📚 Documentation improvement
  • 🔧 Code optimization/refactoring
  • 🎃 Hacktoberfest contribution

🧪 Testing Details

  • Verified correctness on CSES judge (Accepted).
  • Ran multiple custom tests including edge and stress cases (odd cycles, disconnected graphs).
  • Verified no stack overflow (uses BFS) and memory usage within limits.

Test Cases Used:

Input:
5 3
1 2
1 3
4 5

Expected Output:
1 2 2 1 2

Actual Output:
1 2 2 1 2


Input:
3 3
1 2
2 3
3 1

Expected Output:
IMPOSSIBLE

Actual Output:
IMPOSSIBLE


Input:
1 0

Expected Output:
1

Actual Output:
1


📸 Screenshots (if applicable)

image ---

📎 Additional Notes

  • The implementation uses BFS to color the graph level-by-level and detect conflicts.
  • Works for both connected and disconnected graphs.
  • Prints IMPOSSIBLE if any component is not bipartite.
  • Otherwise, prints team assignment for every pupil (1 or 2).

For Maintainers:

  • Code review completed
  • Solution verified on CSES judge
  • Documentation updated if needed
  • Labels applied appropriately (suggested: graphs, good first issue, `hacktoberfest

@ks-iitjmu ks-iitjmu added enhancement New feature or request good first issue Good for newcomers hacktoberfest Issues/PRs for Hacktoberfest participation hacktoberfest-accepted Approved PRs for Hacktoberfest hacktoberfest_2025 Hacktoberfest 2025 specific contributions medium Medium difficulty problems category: graph Graph algorithm problems labels Oct 22, 2025
@ks-iitjmu ks-iitjmu linked an issue Oct 22, 2025 that may be closed by this pull request
9 tasks
@ks-iitjmu ks-iitjmu merged commit d798ce3 into ks-iitjmu:main Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: graph Graph algorithm problems enhancement New feature or request good first issue Good for newcomers hacktoberfest Issues/PRs for Hacktoberfest participation hacktoberfest_2025 Hacktoberfest 2025 specific contributions hacktoberfest-accepted Approved PRs for Hacktoberfest medium Medium difficulty problems

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[NEW] Add solution for Building Teams

2 participants