Graph Without Long Directed Paths

F. Graph Without Long Directed Paths

            time limit per test

            2 seconds

            memory limit per test

            256 megabytes


You are given a connected undirected graph consisting of nn vertices and mm edges. There are no self-loops or multiple edges in the given graph.

You have to direct its edges in such a way that the obtained directed graph does not contain any paths of length two or greater (where the length of path is denoted as the number of traversed edges).

Input

The first line contains two integer numbers nn and mm (2≤n≤2⋅1052≤n≤2⋅105, n−1≤m≤2⋅105n−1≤m≤2⋅105) — the number of vertices and edges, respectively.

The following mm lines contain edges: edge ii is given as a pair of vertices uiui, vivi (1≤ui,vi≤n1≤ui,vi≤n, ui≠viui≠vi). There are no multiple edges in the given graph, i. e. for each pair (ui,viui,vi) there are no other pairs (ui,viui,vi) and (vi,uivi,ui) in the list of edges. It is also guaranteed that the given graph is connected (there is a path between any pair of vertex in the given graph).

Output

If it is impossible to direct edges of the given graph in such a way that the obtained directed graph does not contain paths of length at least two, print "NO" in the first line.

Otherwise print "YES" in the first line, and then print any suitable orientation of edges: a binary string (the string consisting only of '0' and '1') of length mm. The ii-th element of this string should be '0' if the ii-th edge of the graph should be directed from uiuito vivi, and '1' otherwise. Edges are numbered in the order they are given in the input.

Example

input 

6 5
1 5
2 1
1 4
3 1
6 1

output 

YES
10100

Note

The picture corresponding to the first example:

And one of possible answers:

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cassert>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
const ll mod = 1000000007;
const int N = 1e6; 

int n,m,k;	int ans=0;
string s;
int b[1000006];  // 染色记录 
vector <int> G[200005]; 
bool vis[200005];
void dfs(int r,int d){
	
	b[r]=d;
	vis[r]= true;
	for(int i=0;i<G[r].size();i++){
		if(!vis[G[r][i]]) dfs( G[r][i],d^1);
	}
	return ;
}

PII edge[200005];
int main() {
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int u,v;
		cin>>u>>v;
		edge[i].first = u;
		edge[i].second = v;
		G[v].push_back(u);
		G[u].push_back(v);
	}
	dfs(1,0);
	
	for(int i=0;i<m;i++){
		int v,u;
		v = edge[i].first;
		u = edge[i].second;
		if(b[v]==b[u]){
			cout<<"NO"<<'\n';
			return 0;
		}
		else if( b[v] ==0 ){
			s+='1';
		}
		else s+='0';
	}
	cout<<"YES"<<'\n';
	cout<<s<<'\n';
	return 0; 
}
发布了46 篇原创文章 · 获赞 81 · 访问量 5553

猜你喜欢

转载自blog.csdn.net/zfq17796515982/article/details/88956225