Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bits/stdc++.h"
- using namespace std;
- const int maxN = 1e5 + 10;
- vector<int> adj[maxN];
- int color[maxN];
- bool visited[maxN];
- bool dfs (int node,int col) {
- visited[node] = true;
- color[node] = col;
- for (int child : adj[node]) {
- if (visited[child] == false) {
- if (dfs(child,col == 1 ? 2 : 1) == false) {
- return false;
- } //dfs(2,1) == false
- } else {
- if (color[child] == color[node]) {
- return false;
- }
- }
- }
- //ei jaygay kokhonoi as
- return true;
- }
- int main () {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int n, m; // n = number of nodes m = number of edges
- cin >> n >> m;
- for (int i = 1; i <= m; ++i) {
- int u, v;
- cin >> u >> v;
- adj[u].push_back(v);
- adj[v].push_back(u);
- }
- for (int i = 1; i <= n; ++i) {
- if (visited[i] == false) {
- bool f = dfs(i,2);
- if (f == false) {
- cout << "IMPOSSIBLE\n";
- return 0;
- }
- }
- }
- for (int i = 1; i <= n; ++i) cout << color[i] << ' ';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement