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. xml错误之cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:annotation-driven'.

    今天从svn导入项目的时候,一个xml文件里面报错:‘cvc-complex-type.2.4.c: The matching wildcard is strict, but no declarati ...

  2. python + django + echart 构建中型项目

    1. python生产环境, 多层modules 导入问题: 多个modules 如何导入不同级别的包: 在每个modules下新建 __init__.pyimport os, sys dir_myt ...

  3. P2023 [AHOI2009]维护序列

    震惊,双倍经验,依旧是线段树的乘法修改 #include<bits/stdc++.h> using namespace std; ; struct sege_tree { int l; i ...

  4. c提高第四课

    1.一维数组的初始化 , , }; //3个元素 ] = { , , }; //a[3], a[4]自动初始化为0 ] = { }; //全部元素初始化为0 memset(c, , sizeof(c) ...

  5. Python基础:数据类型-数字(5)

    在Python中,所有的数据类型都是类,每一个变量都是类的实例. Python中有6种标准数据类型:数字(Number).字符串(String).列表(List).元组(Tuple).集合(Sets) ...

  6. Vue.js 2.x笔记:基本语法(2)

    1. Vue实例及选项 1.1 创建Vue实例(new Vue instance) 每个Vue Application必须创建一个root Vue Instance. <script> v ...

  7. Redis快问快答

    本随笔的回答来自 http://www.runoob.com/redis/redis-tutorial.html 另一个不错的教程: https://www.yiibai.com/redis/redi ...

  8. 火眼发布Windows攻击工具集

    导读 渗透测试员的喜讯:安全公司火眼发布Windows攻击工具集--足足包含140个程序. Kali Linux 已成为攻击型安全专家的标配工具,但对需要原生Windows功能的渗透测试员来说,维护良 ...

  9. Msi中文件替换

    转自https://blog.csdn.net/davidhsing/article/details/9962377 ※说明:目前可以用于MSI编辑的软件很多,但是有些软件在保存时会在MSI文件中写入 ...

  10. mysql-SELECT子句的顺序