CSES - Aalto Competitive Programming 2024 - wk2 - Mon - Results
Submission details
Task:Building Teams
Sender:Niilo
Submission time:2024-09-09 17:07:49 +0300
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'void f(int, int)':
input/code.cpp:14:36: error: 'w' was not declared in this scope
   14 |                 } else if (Z[i] == w) {
      |                                    ^

Code

#include <iostream>
#include <vector>
using namespace std;

vector<int> A[200'001];
int Z[200'001];

void f(int i, int z) {
	Z[i] = z;
	z = z == 1 ? 2 : 1;
	for (int j : A[i]) {
		if (!Z[i]) {
			f(j,z);
		} else if (Z[i] == w) {
			cout << "IMPOSSIBLE";
			exit(0);
		}
	}
}

int main() {
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < m; ++i) {
		int a, b;
		cin >> a >> b;
		A[a].push_back(b);
		A[b].push_back(a);
	}
	for (int i = 0; i < n; ++i) {
		if (!Z[i]) f(i, 1);
	}
	for (int i = 0; i < n; ++i) {
		cout << (Z[i] ? "1 " : "2 ");
	}
}