F. Graph Without Long Directed Paths(二分图染色)

连接:http://codeforces.com/problemset/problem/1144/F

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:

题意:

给你n个点m条边的无向连通图,请你给每个边附一个方向,令图中没有长度>=2的边出现。

思路:一个点开始染色 和他相连的点染相反颜色 然后再继续递归下去染色,判断下一节点的颜色和自己的一样不,一样就输出NO,反之能满足题意。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include<set>
#include<vector>
#include<queue>
#include<map>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
#define N 200005
vector<int>ve[N];
int a[N],b[N];
int color[N];
int flag;
void dfs(int x,int fa,int v)
{
    color[x]=v;//染颜色,和其父节点颜色不同
    for(int i=0;i<ve[x].size();i++)
    {
        int u=ve[x][i];//下一节点
        if(u==fa)//下一节点如果是其父节点
            continue;
        if(color[u]==-1)//还没染过颜色
            dfs(u,x,1-v);//搜索下一个,颜色改变
        else if(color[u]==color[x])
            flag=0;//下一节点与其颜色相同
    }
    return;
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    memset(color,-1,sizeof(color));
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&a[i],&b[i]);
        ve[a[i]].push_back(b[i]);
        ve[b[i]].push_back(a[i]);
    }
    flag=1;
    dfs(1,-1,0);
    if(!flag)
    {
        printf("NO\n");
        return 0;
    }
    printf("YES\n");
    for(int i=0;i<m;i++)
    {
        if(color[a[i]]==1)
            printf("1");
        else
            printf("0");
    }printf("\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Kuguotao/article/details/88997494