Advertisement
Guest User

building teams

a guest
Jul 20th, 2023
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n, m;
  5. vector<vector<int>> adj;
  6. vector<int> vis;
  7. bool isBipartite = true;
  8.  
  9. void dfs(int node, int color)
  10. {
  11. vis[node] = color;
  12.  
  13. for(auto it: adj[node])
  14. {
  15. if(vis[it] == 0)
  16. {
  17. dfs(it, 3 - color);
  18. }
  19. if(vis[it] == vis[node])
  20. {
  21. isBipartite = false;
  22. return;
  23. }
  24. }
  25. }
  26.  
  27. int main()
  28. {
  29. cin >> n >> m;
  30.  
  31. adj = vector<vector<int>>(n+1);
  32. vis = vector<int>(n+1, 0);
  33.  
  34. for(int i = 0; i < m; i++)
  35. {
  36. int u, v;
  37. cin >> u >> v;
  38.  
  39. adj[u].push_back(v);
  40. adj[v].push_back(u);
  41. }
  42.  
  43. for(int i = 1; i <= n; i++)
  44. {
  45. if(vis[i] == 0)
  46. {
  47. dfs(i, 1);
  48. if(!isBipartite) break;
  49. }
  50. }
  51.  
  52. if(!isBipartite)
  53. {
  54. cout << "IMPOSSIBLE" << '\n';
  55. }
  56. else
  57. {
  58. for(int i = 1; i <= n; i++)
  59. {
  60. cout << vis[i] << " ";
  61. }
  62. cout << '\n';
  63. }
  64.  
  65.  
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement