You are given a connected undirected graph consisting of ? vertices and ?
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 ?
and ? (2≤?≤2⋅105, ?−1≤?≤2⋅105
) — the number of vertices and edges, respectively.
The following ?
lines contain edges: edge ? is given as a pair of vertices ??, ?? (1≤??,??≤?, ??≠??). There are no multiple edges in the given graph, i. e. for each pair (??,??) there are no other pairs (??,??) and (??,??
) 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 ?
. The ?-th element of this string should be '0' if the ?-th edge of the graph should be directed from ?? to ??
, 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,其实就是从一个点出发只能走一步了,我们可以将建边的工作转化为给点进行的操作,可以给这n个点进行01染色操作,使得任意两点有直达边且该两点染色不同。不妨设该点染色为0表示与这个点相连的边都是出边,1表示与这个点相连的边都是入边。由于01的地位是一样的,不管从那个点开始或者染什么颜色都是一样的结果,则我们可以找任意一个点进行DFS染色操作,若染色操作中遇到了连续两点染色相同的情况,则输出NO,否则对于每一条边输出该边起点被染的颜色就好了。
#include<bits/stdc++.h>
using namespace std;
const int maxn=200010;
int color[maxn],a[maxn],b[maxn];
vector<int>G[maxn];
void DFS(int now,int pre,int col)
{
color[now]=col;
for(int i=0;i<G[now].size();i++){
int to=G[now][i];
if(to==pre)
continue;
else if(color[to]==-1)
DFS(to,now,1-col);
else if(color[now]==color[to]){
puts("NO");
exit(0);
}
}
}
int main()
{
memset(color,-1,sizeof(color));
int n,m;
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>a[i]>>b[i];
G[a[i]].push_back(b[i]);
G[b[i]].push_back(a[i]);
}
DFS(1,0,0);
puts("YES");
for(int i=1;i<=m;i++)
{
if(color[a[i]]==1)
cout<<"1";
else
cout<<"0";
}
cout<<endl;
return 0;
}