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 uiui to vivi, and '1' otherwise. Edges are numbered in the order they are given in the input.
Example
input
Copy
6 5
1 5
2 1
1 4
3 1
6 1
output
Copy
YES
10100
Note
The picture corresponding to the first example:
And one of possible answers:
题意:给无向图(无环)的每一条边确定方向,使整个图没有长度大于1的通路。
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
vector<int> m[200010];
int vis[200010],t[200010];
int flag;
void dfs(int x)
{
for(int i=0;i<m[x].size();i++)
{
int temp=m[x][i];
if(vis[temp]==vis[x])
{
cout<<"NO"<<endl;
flag=0;
return ;
}
if(vis[temp]==-vis[x])
continue;
vis[temp]=-vis[x];
dfs(temp);
}
}
int main()
{
int n,k;
cin>>n>>k;
flag=1;
for(int i=0;i<k;i++)
{
int u ,v;
cin>>u>>v;
m[u].push_back(v);
m[v].push_back(u);
t[i]=u;
}
vis[1]=1;
dfs(1);
if(flag)
{
cout<<"YES"<<endl;
for(int i=0;i<k;i++)
{
if(vis[t[i]]==1)
cout<<1;
else
cout<<0;
}
}
return 0;
}