Submission details
Task:Building Teams
Sender:ariadna.roga
Submission time:2025-09-08 17:30:29 +0300
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:47:27: error: 'colors' was not declared in this scope
   47 |             if(!dfs(i, 0, colors, adj))
      |                           ^~~~~~
input/code.cpp:47:35: error: 'adj' was not declared in this scope
   47 |             if(!dfs(i, 0, colors, adj))
      |                                   ^~~
input/code.cpp:53:17: error: 'team' was not declared in this scope; did you mean 'teams'?
   53 |         cout << team[i] << " ";
      |                 ^~~~
      |                 teams

Code

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool dfs(int pupil, int team, vector<int> &teams, vector<vector<int>> &friendships) {
    teams[pupil] = team; 

    for(auto fr : friendships[pupil]) {
        if(teams[fr] == team) {
            return false;
        }
        else if(teams[fr] == -1) {
            int other_team;
            if (team == 1)
                other_team = 2;
            else
                other_team = 1;

            if(!dfs(fr, other_team, teams, friendships))
                return false;
        }
    }
    return true;
}

int main() {

    int n, m;
    cin >> n >> m;
 
    vector<vector<int>> friendships(n+1);
 
    // Read friendships
    for (int i = 0; i < m; ++i) {
        int f1, f2;
        cin >> f1 >> f2;
        friendships[f1].push_back(f2);
        friendships[f2].push_back(f1);
    }
    
    vector<int> teams(n+1, -1);

    for(int i = 1; i <= n; i++) {
        if(teams[i] == -1) { 
            if(!dfs(i, 0, colors, adj))
                cout << "IMPOSSIBLE" << endl; 
        }
    }
    
    for (int i = 1; i <= n; ++i)
        cout << team[i] << " ";
    cout << endl;
}