Advertisement
newb_ie

Untitled

Oct 23rd, 2021
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. using namespace std;
  3.  
  4. const int maxN = 1e5 + 10;
  5. vector<int> adj[maxN];
  6. int color[maxN];
  7. bool visited[maxN];
  8.  
  9. bool dfs (int node,int col) {
  10. visited[node] = true;
  11. color[node] = col;
  12. for (int child : adj[node]) {
  13. if (visited[child] == false) {
  14. if (dfs(child,col == 1 ? 2 : 1) == false) {
  15. return false;
  16. } //dfs(2,1) == false
  17. } else {
  18. if (color[child] == color[node]) {
  19. return false;
  20. }
  21. }
  22. }
  23. //ei jaygay kokhonoi as
  24. return true;
  25. }
  26.  
  27. int main () {
  28. ios::sync_with_stdio(false);
  29. cin.tie(nullptr);
  30. cout.tie(nullptr);
  31. int n, m; // n = number of nodes m = number of edges
  32. cin >> n >> m;
  33. for (int i = 1; i <= m; ++i) {
  34. int u, v;
  35. cin >> u >> v;
  36. adj[u].push_back(v);
  37. adj[v].push_back(u);
  38. }
  39. for (int i = 1; i <= n; ++i) {
  40. if (visited[i] == false) {
  41. bool f = dfs(i,2);
  42. if (f == false) {
  43. cout << "IMPOSSIBLE\n";
  44. return 0;
  45. }
  46. }
  47. }
  48. for (int i = 1; i <= n; ++i) cout << color[i] << ' ';
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement