Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int n, m;
- vector<vector<int>> adj;
- vector<int> vis;
- bool isBipartite = true;
- void dfs(int node, int color)
- {
- vis[node] = color;
- for(auto it: adj[node])
- {
- if(vis[it] == 0)
- {
- dfs(it, 3 - color);
- }
- if(vis[it] == vis[node])
- {
- isBipartite = false;
- return;
- }
- }
- }
- int main()
- {
- cin >> n >> m;
- adj = vector<vector<int>>(n+1);
- vis = vector<int>(n+1, 0);
- for(int i = 0; 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(vis[i] == 0)
- {
- dfs(i, 1);
- if(!isBipartite) break;
- }
- }
- if(!isBipartite)
- {
- cout << "IMPOSSIBLE" << '\n';
- }
- else
- {
- for(int i = 1; i <= n; i++)
- {
- cout << vis[i] << " ";
- }
- cout << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement