Sie sind auf Seite 1von 3

#include <bits/stdc++.

h>
#define endl '\n'

#pragma GCC optimize ("Ofast")


#pragma GCC target ("sse4")

#define SZ(x) ((int)x.size())


#define ALL(V) V.begin(), V.end()
#define L_B lower_bound
#define U_B upper_bound
#define pb push_back

using namespace std;


template<class T, class T2> inline int chkmax(T &x, const T2 &y) { return x < y ? x
= y, 1 : 0; }
template<class T, class T2> inline int chkmin(T &x, const T2 &y) { return x > y ? x
= y, 1 : 0; }
const int MAXN = (1 << 20);

int n, m;
vector<int> adj[MAXN];

int read_int();

void read()
{
n = read_int();
m = read_int();
for(int i = 1; i <= m; i++)
{
int u, v;
u = read_int(), v = read_int();
adj[u].pb(v);
adj[v].pb(u);
}
}

int w[MAXN];
int col[MAXN];
int used[MAXN], used_timer;
bool visited[MAXN];

void solve()
{
for(int i = 1; i <= n; i++)
visited[i] = false;

priority_queue<pair<int, int> > Q;


for(int i = 1; i <= n; i++)
{
w[i] = -SZ(adj[i]);
Q.push({w[i], i});
}

vector<int> order;
for(int i = 1; i <= n; i++)
{
while(!Q.empty() && used[Q.top().second] == 1)
Q.pop();
int u = Q.top().second;
Q.pop();
w[u] = 0;
used[u] = 1;

order.pb(u);

for(int v: adj[u])
if(w[v] != 0)
{
w[v]++;
Q.push({w[v], v});
}
}

reverse(ALL(order));
for(int i = 1; i <= n; i++)
col[i] = -1;

used_timer = 42;
for(int u: order)
{
used_timer++;
for(int v: adj[u])
if(col[v] != -1)
used[col[v]] = used_timer;

col[u] = 1;
while(used[col[u]] == used_timer) col[u]++;
}

int cnt_colours = 0;
for(int i = 1; i <= n; i++)
chkmax(cnt_colours, col[i]);

cout << cnt_colours << endl;


for(int i = 1; i <= n; i++)
cout << col[i] << " \n"[i == n];
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);

/*double beg1,end1;
beg1=clock();*/
read();
solve();
/*end1=clock();
cerr << (end1-beg1)/CLOCKS_PER_SEC ;*/
return 0;
}

const int maxl = 100000;


char buff[maxl];
int ret_int, pos_buff = 0;
void next_char() { if(++pos_buff == maxl) fread(buff, 1, maxl, stdin), pos_buff =
0; }

int read_int()
{
ret_int = 0;
for(; buff[pos_buff] < '0' || buff[pos_buff] > '9'; next_char());
for(; buff[pos_buff] >= '0' && buff[pos_buff] <= '9'; next_char())
ret_int = ret_int * 10 + buff[pos_buff] - '0';
return ret_int;
}

Das könnte Ihnen auch gefallen