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. 【刷题】Git工作流-相关知识点

    参考资料:[学习总结]Git学习-GIT工作流-千峰教育(来自B站) 1-Git工作流 GitFlow流五大分支: 主干分支 热修复分支 预发布分支 开发分支 功能分支 GitFlow 工作流定义了一 ...

  2. 用redis实现分布式锁,秒杀案例(转)

    分布式锁的简单实现代码: 需要的jar包: jedis-2.9.0.jar. commons-pool2-2.4.2.jar import java.util.List; import java.ut ...

  3. mysql 索引中的USING BTREE 的意义

    索引是在存储引擎中实现的,因此每种存储引擎的索引都不一定完全相同,并且每种存储引擎也不一定支持所有索引类型. 根据存储引擎定义每个表的最大索引数和最大索引长度.所有存储引擎支持每个表至少16个索引,总 ...

  4. Spring Boot(一):入门篇+前端访问后端

    转自:Spring Boot(一):入门篇 什么是Spring Boot Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发 ...

  5. 上传图片,通过node服务器存储在指定目录

    最近做毕设,需要上传图片,因为在本地服务器运行,所以想着前端上传后,通过node服务器接收图片,存储在指定的目录下. 一.前端实现 1.前端的页面和上传图片是利用element-ui组件实现的,&qu ...

  6. 本部jdk切换的坑!!!

    https://www.cnblogs.com/ll409546297/p/6593173.html 如果你参考其他博主没有解决,可以来参考下这个. 1.我们打开注册表,然后找到这个路径: HKEY_ ...

  7. 洛谷P5119 Convent 题解

    题目 很好想的一道二分题,首先,二分一定满足单调性,而题目中非常明显的就是用的车越多,所用时间越少,所以可以枚举时间,判断是否可以比\(m\)少. 然后在二分时,更是要注意下标的一些问题,也要注意车和 ...

  8. Vue状态管理之Vuex

    Vuex是专为Vue.js设计的状态管理模式.采用集中存储组件状态它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 1.首先让我们从一个vue的计数应用开始 ...

  9. Zabbix监控磁盘IO值

    iostat取硬盘IO值. iostat -x 3 2 | grep vdb | sed -n '2p' | awk '{print $14}' 每3s取一次值,输出第二次vdb硬盘的负载值. 添加Z ...

  10. 响应式菜单栏: bootstrap + jQuery

    bootstrap库能够很方便的实现PC和移动上的响应式操作. jQuery库大大的简化了脚本的开发: html: <html> <body> <div class='m ...