CodeForces 794D. Labelling Cities

来源:互联网 发布:解析json为格式化 编辑:程序博客网 时间:2024/06/05 18:48

Oleg the bank client lives in Bankopolia. There are n cities in Bankopolia and some pair of cities are connected directly by bi-directional roads. The cities are numbered from 1 to n. There are a total of m roads in Bankopolia, the i-th road connects cities ui and vi. It is guaranteed that from each city it is possible to travel to any other city using some of the roads.

Oleg wants to give a label to each city. Suppose the label of city i is equal to xi. Then, it must hold that for all pairs of cities (u, v) the condition |xu - xv| ≤ 1 holds if and only if there is a road connecting u and v.

Oleg wonders if such a labeling is possible. Find an example of such labeling if the task is possible and state that it is impossible otherwise.

Input

The first line of input contains two space-separated integers n and m (2 ≤ n ≤ 3·1051 ≤ m ≤ 3·105) — the number of cities and the number of roads.

Next, m lines follow. The i-th line contains two space-separated integers ui and vi (1 ≤ ui, vi ≤ nui ≠ vi) — the cities connected by the i-th road. It is guaranteed that there is at most one road between each pair of cities and it is possible to travel from any city to any other city using some roads.

Output

If the required labeling is not possible, output a single line containing the string "NO" (without quotes).

Otherwise, output the string "YES" (without quotes) on the first line. On the next line, output n space-separated integers, x1, x2, ..., xn. The condition 1 ≤ xi ≤ 109 must hold for all i, and for all pairs of cities (u, v) the condition |xu - xv| ≤ 1 must hold if and only if there is a road connecting u and v.

Examples
input
4 41 21 31 43 4
output
YES2 3 1 1 
input
5 101 21 31 41 52 32 42 53 43 55 4
output
YES1 1 1 1 1 
input
4 31 21 31 4
output
NO
Note

For the first sample, x1 = 2x2 = 3x3 = x4 = 1 is a valid labeling. Indeed, (3, 4)(1, 2)(1, 3)(1, 4) are the only pairs of cities with difference of labels not greater than 1, and these are precisely the roads of Bankopolia.

For the second sample, all pairs of cities have difference of labels not greater than 1 and all pairs of cities have a road connecting them.

For the last sample, it is impossible to construct a labeling satisfying the given constraints.

题意:给一个图,给每个点确定一个label,记为x[i],要求两个点(i, j)有边当且仅当|x[i] - x[j]| = 1

题解:

考虑把相同label的点缩起来,最后一定是一条链

我们如果从链的一端S(dis[S] = 1)开始BFS,则dis就是最后的label(注意特判有的dis = 2的点本质上应该放到dis = 1里面)

考虑我们先BFS(1),然后dis最大的就是链的一端了

注意如果max{dis[i]}=2需要特判

然后判一判边数即可

#include <bits/stdc++.h>#define xx first#define yy second#define mp make_pair#define pb push_back#define fill( x, y ) memset( x, y, sizeof x )#define copy( x, y ) memcpy( x, y, sizeof x )using namespace std;typedef long long LL;typedef pair < int, int > pa;inline int read(){int sc = 0, f = 1; char ch = getchar();while( ch < '0' || ch > '9' ) { if( ch == '-' ) f = -1; ch = getchar(); }while( ch >= '0' && ch <= '9' ) sc = sc * 10 + ch - '0', ch = getchar();return sc * f;}const int MAXN = 300005;int rt, n, m, d[MAXN], dis[MAXN], q[MAXN], ql, qr, size[MAXN];vector < int > G[MAXN];inline void bfs(int x){fill( dis, 0 );for( dis[ q[ ql = 0 ] = x ] = qr = 1 ; ql ^ qr ; x = q[ ++ql ] )for( auto y : G[ x ] ) if( !dis[ y ] ) dis[ q[ qr++ ] = y ] = dis[ x ] + 1;}int main(){#ifdef wxh010910freopen( "data.in", "r", stdin );#endifn = read(), m = read();if( m == 1LL * n * ( n - 1 ) / 2 ){puts( "YES" );for( int i = 1 ; i <= n ; i++ ) printf( "1 " );return 0;}for( int i = 1, x, y ; i <= m ; i++ ) G[ x = read() ].pb( y = read() ), G[ y ].pb( x ), d[ x ]++, d[ y ]++;int mx = 0;bfs( 1 );for( int i = 1 ; i <= n ; i++ ) mx = max( mx, dis[ i ] );if( mx == 2 ) for( int i = 1 ; i <= n ; i++ ) if( d[ i ] ^ d[ 1 ] ) rt = i;if( !rt ) rt = q[ n - 1 ];bfs( rt );for( int i = 1 ; i <= n ; i++ ) if( dis[ i ] == 2 && d[ i ] == d[ rt ] ) dis[ i ] = 1;for( int i = 1 ; i <= n ; i++ ) size[ dis[ i ] ]++;LL c = 0;for( int i = 1 ; i <= n ; i++ ) c += 1LL * size[ i ] * ( size[ i ] - 1 ) >> 1, c += 1LL * size[ i ] * size[ i - 1 ];if( c ^ m ) return puts( "NO" ), 0;for( int x = 1 ; x <= n ; x++ )for( auto y : G[ x ] ) if( abs( dis[ y ] - dis[ x ] ) > 1 )return puts( "NO" ), 0;puts( "YES" );for( int i = 1 ; i <= n ; i++ ) printf( "%d ", dis[ i ] );return 0;}


原创粉丝点击