
二分圖
#include<bits/stdc++.h>
#define int long long
#define f first
#define s second
#define fastio ios_base::sync_with_stdio(0);cin.tie(0)
using namespace std;
//declare
const int maxn = 1e5+5;
int n,m,colors[maxn] = {0};
vector<int> G[maxn];
//
void dfs(int cur,int color){
if(colors[cur]){
if(colors[cur] != color){
cout<<"IMPOSSIBLE\n";
exit(0);
}
return;
}
colors[cur] = color;
for(int adj : G[cur]){
dfs(adj, 3-color);
}
}
signed main(){
fastio;
cin>>n>>m;
for(int i=0;i<m;i++){
int a,b; cin>>a>>b;
G[a].push_back(b);
G[b].push_back(a);
}
for(int i=1;i<=n;i++){
if(!colors[i]) dfs(i,1);
}
for(int i=1;i<=n;i++){
cout<<colors[i]<<" ";
}
cout<<"\n";
return 0;
}

發佈留言