Codeforces Round #550 (Div. 3) 1144 F. Graph Without Long Directed Paths

F. Graph Without Long Directed Paths

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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的路径的图。即任何路径的长度只能为1。

分析:因为长度只能为1,那么对于任意一个点来说,如果它只有出度或者只有入度,那么一定不存在长度为2的路径。

问题就变成了能否将每个点都变成仅有入度或者出度。

跑一个bfs就可以了。

#include "bits/stdc++.h"
using namespace std;
struct node
{
    int v,id,ok;
};
struct point
{
    int u,k;
};
vector<node>v[200004];
bool vis[200004];
string ans;
bool bfs()
{
    queue<point>q;
    q.push({1,1});
    while(!q.empty())
    {
        point t=q.front();q.pop();vis[t.u]=1;
        int u=t.u;
        for (int i = 0; i < v[u].size(); ++i) {
            int x=v[u][i].v;
            if(vis[x]&&v[u][i].ok!=t.k)return false;
            if(vis[x])continue;
            if(t.k==v[u][i].ok)
            {
                q.push({x,t.k^1});
            }
            else
            {
                ans[v[u][i].id]='1';
                v[u][i].ok^=1;
                for (int j = 0; j < v[ v[u][i].v ].size(); ++j) {
                    if(v[ v[u][i].v ][j].v==u){
                        v[ v[u][i].v ][j].ok^=1;
                        break;
                    }
                }
                q.push({x,t.k^1});
            }
        }
    }
    return true;
}

int main()
{
    int n,m;
    cin>>n>>m;
    memset(vis,0, sizeof(vis));
    for (int i = 0; i < m; ++i) {
        ans+='0';
        int x,y;
        scanf("%d%d",&x,&y);
        v[x].push_back({y,i,0});
        v[y].push_back({x,i,1});
    }
    vis[1]=1;
    if(bfs()){
        puts("YES");
        cout<<ans<<endl;
    }
    else puts("NO");
}

猜你喜欢

转载自blog.csdn.net/qq_42671946/article/details/88981560