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 这个题的题意就是修改一些边的方向让有向图不包含长度为2或更大的任何路径(其中路径长度表示为遍历边的数量),修改的边标记为1,没修改的标记为0,最后按顺序输出所有的边的标记
如下图

思路在下面代码的注释中

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
const int maxn=2e5+;
const int inf=0x3f3f3f3f;
int n,m;
int first[maxn],sign;
int vis[maxn];
int a[maxn][];
struct node
{
int to,w,next;
} edge[maxn<<];
void init()
{
for(int i = ; i <=n; i++)
first[i]=-;
sign=;
}
void add_edge(int u,int v,int w)
{
edge[sign].to=v;
edge[sign].w=w;
edge[sign].next=first[u];
first[u]=sign++;
}
bool flag1=;
void dfs(int x,int flag)///对整个图进行染色1 0 1 0...,
{
vis[x]=flag;
for(int i=first[x]; ~i; i=edge[i].next)
{
int to=edge[i].to;
if(vis[to]==vis[x])///判断是是否有环
{
flag1=;
return ;
}
if(vis[to]==-)
{
dfs(to,flag^);
}
}
return ; }
int main()
{
scanf("%d%d",&n,&m);
init();
for(int i=; i<=m; i++)
{
int u,v;
scanf("%d%d",&u,&v);
add_edge(u,v,);
add_edge(v,u,);
a[i][]=u;
a[i][]=v;
}
memset(vis,-,sizeof(vis));
dfs(,);
if(flag1)
puts("NO");///如果图中有环的话,肯定就会存在dist>=2的点,可以自己画一个三角形看看,就很容易理解了
else
{
puts("YES");
for(int i=; i<=m; i++)
{
if(vis[a[i][]]==)///a[i][0]为第i条边的起点,a[i][1]为第i条边的终点。
///这里我们已经将整个图染色,整个图分为两类,标记为1的点和标记为0的点
///以标记为1的点为起点,出发的边不做修改,即边的标记为0
///以标记为0的点为起点,出发的边改为相反的方向,即边的标记为1,
///这里其实是不难理解的,两个相邻的点必定为1 0或者0 1,1->0->1
///(1出发的边保持原来的方向,0出发的边改为相反的方向)或者(0出发
///的边保持原来的方向,1出发的边改为相反的方向)才能使得整个图的不出现长度为2或者更大的路径
printf("");
else
printf("");
}
} return ;
}

Codeforces Round #550 (Div. 3) F. Graph Without Long Directed Paths的更多相关文章

  1. Codeforces Round #550 (Div. 3) F. Graph Without Long Directed Paths (二分图染色)

    题意:有\(n\)个点和\(m\)条无向边,现在让你给你这\(m\)条边赋方向,但是要满足任意一条边的路径都不能大于\(1\),问是否有满足条件的构造方向,如果有,输出一个二进制串,表示所给的边的方向 ...

  2. Codeforces Round #496 (Div. 3) F - Berland and the Shortest Paths

    F - Berland and the Shortest Paths 思路:还是很好想的,处理出来最短路径图,然后搜k个就好啦. #include<bits/stdc++.h> #defi ...

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

    F. Graph Without Long Directed Paths time limit per test 2 seconds memory limit per test 256 megabyt ...

  4. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  5. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  6. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  7. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  8. CodeForces Round #550 Div.3

    http://codeforces.com/contest/1144 A. Diverse Strings A string is called diverse if it contains cons ...

  9. Codeforces Round #375 (Div. 2) F. st-Spanning Tree 生成树

    F. st-Spanning Tree 题目连接: http://codeforces.com/contest/723/problem/F Description You are given an u ...

随机推荐

  1. python的排序方式

    """ 冒泡排序: 冒泡排序的思想: 每次比较两个相邻的元素, 如果他们的顺序错误就把他们交换位置 比如有五个数: 12, 35, 99, 18, 76, 从大到小排序, ...

  2. Keras 获取中间某一层输出

    1.使用函数模型API,新建一个model,将输入和输出定义为原来的model的输入和想要的那一层的输出,然后重新进行predict. #coding=utf-8 import seaborn as ...

  3. react 生命周期函数介绍

    constructor():构造函数 执行:组件加载钱最先调用一次,仅调用一次. 作用:定义状态机变量. 注意:第一个语句必须为super(), 否则会报错:'this' is not allowed ...

  4. ArrayDataProvider数据分页

    模型 public function search($page=10){ $lists = self::find()->orderBy('id DESC')->all(); $dataPr ...

  5. [ZJOI2019]线段树(线段树)

    看到这题,首先想到将求和转期望,即每次操作进行概率为1/2,求节点打标记概率. 首先对于每次区间修改操作,对节点进行分类: 1.这个点和其父亲都和修改区间无交,这种情况可以无视. 2.这个点和修改区间 ...

  6. GWAS研究中case和control的比例是有讲究的?

    GWAS研究中,表型分两种.第一种是线性的表型,如果身高.体重.智力等:第二种是二元的表型,比如患病和未患病,即通常所说的case和control.对于表型是线性的样本来说,是不存在case和cont ...

  7. C#使用WindowsMediaPlayer实现视频播放

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  8. sigaction 的使用

    linux内核会发射一些信号,应用程序可以捕捉信号执行特定函数 :失败:-,设置errno act:传入参数,新的处理方式.oldact:传出参数,旧的处理方式. struct sigaction结构 ...

  9. 06--STL序列容器(priority_queue)

    一:优先队列priority_queue简介 同队列,不支持迭代 (一)和队列相比 同: 优先队列容器与队列一样,只能从队尾插入元素,从队首删除元素. 异: 但是它有一个特性,就是队列中最大的元素总是 ...

  10. 重新安装了环境报错{"error":"could not find driver"}

    前言:最近新工作开发oa系统,没有借助工具安装wamp环境,结果在测试项目时候出现了bug,找了很久,发现方向没有对 报错信息: {"error":"could not ...