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:

这个题目就是一个dfs,不是特别难,但是开始我比较蠢,我对每条边都for了一次,但是这个是一个图,所以如果我这样的话,很容易被数据把YES卡成NO

既然是一个图,那就随意找一个点进行dfs就好了,之前看了很多题解说的对每一个点进行染色(不太懂。。。)不过这个实际意思也差不多。

就是对每一个点进行标记,标记成0,1 指出去是1被指是0,然后就看代码好了

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 100;
int vis[maxn];//1表示指出去,0表示被指
vector<int>vec[maxn];
int a[maxn], b[maxn],ok; void dfs(int s,int flag)
{
vis[s] = flag;
int len = vec[s].size();
for(int i=0;i<len;i++)
{
if(vis[s]==vis[vec[s][i]]&&vis[s]>=0)
{
ok = 0;
return;
}
if(vis[vec[s][i]]<0)
{
dfs(vec[s][i], !flag);
}
}
} int main()
{
int n, m;
cin >> n >> m;
for(int i=1;i<=m;i++)
{
scanf("%d%d", &a[i], &b[i]);
vec[a[i]].push_back(b[i]);
vec[b[i]].push_back(a[i]);
}
memset(vis, -1, sizeof(vis));
ok = 1;
dfs(1,1);
if (!ok) printf("NO\n");
else
{
printf("YES\n");
for(int i=1;i<=m;i++)
{
if (vis[a[i]] == 0 && vis[b[i]] == 1) printf("1");
if (vis[a[i]] == 1 && vis[b[i]] == 0) printf("0");
}
printf("\n");
}
return 0;
}

  

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

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

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

  2. CodeForces Round #550 Div.3

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

  3. Graph Without Long Directed Paths CodeForces - 1144F (dfs染色)

    You are given a connected undirected graph consisting of nn vertices and mm edges. There are no self ...

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

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

  5. Codeforces 715B & 716D Complete The Graph 【最短路】 (Codeforces Round #372 (Div. 2))

    B. Complete The Graph time limit per test 4 seconds memory limit per test 256 megabytes input standa ...

  6. D. Equalize Them All Codeforces Round #550 (Div. 3)

    D. Equalize Them All time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. Codeforces Round #550 (Div. 3) E. Median String (模拟)

    Median String time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  8. (原创)Codeforces Round #550 (Div. 3) D. Equalize Them All

    D. Equalize Them All time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. (原创)Codeforces Round #550 (Div. 3) A Diverse Strings

    A. Diverse Strings time limit per test 1 second memory limit per test 256 megabytes input standard i ...

随机推荐

  1. C#常量和字段以及各种方法的语法总结

    目录 一. 常量和字段.... 1 1. 常量.... 1 2.字段.... 1 二.方法.... 2 1.实例构造器和类(引用类型).... 2 2.实例构造器和结构(值类型).... 2 3.类型 ...

  2. Java学习笔记之——单例模式

      (1)懒汉式:对象在方法中,第一次调用时创建对象,线程不安全的 public class Singleton { //外部不可以创建对象,就要在内部创建一个对象,还能够在外部获取 private ...

  3. Java学习笔记之——封装

    1. 属性和方法放到类中 2. 信息的隐藏 (1) 属性的隐藏 (2) 方法实现的细节隐藏 3. 权限修饰符: 从小到大的顺序:private->默认的(什么都不写)->protected ...

  4. 提取Chrome插件为crx文件

    在Chrome浏览器输入 chrome://extensions/,点开右上角开发者模式 记录上图中的ID:gidgenkbbabolejbgbpnhbimgjbffefm 在资源管理器中找到Chro ...

  5. Vue Document

    目录 VUE笔记 环境搭建 Vue学习笔记 1.Vue指令 VUE笔记 环境搭建 node -v npm -v npm i -g cnpm --registry=https://registry.np ...

  6. ACM ICPC 2017 Warmup Contest 9 L

    L. Sticky Situation While on summer camp, you are playing a game of hide-and-seek in the forest. You ...

  7. 四个O(n^2)级别的排序性能测试

    测试环境为DEV-C++,并且选择排序,插入排序,冒泡排序,均为优化后的,若想了解具体优化过程,请参照:https://blog.csdn.net/qq_40164152 测试用例: #ifndef ...

  8. Python面试题之Python面试题汇总

    在这篇文章中: Python基础篇 1:为什么学习Python 2:通过什么途径学习Python 3:谈谈对Python和其他语言的区别 Python的优势: 4:简述解释型和编译型编程语言 5:Py ...

  9. 现在有两个变量,分别是a = 3, b = 4,那么我们不用第三个变量来调换a和b的值。

    现在有两个变量,分别是a = 3, b = 4,那么我们不用第三个变量来调换a和b的值. <!DOCTYPE html><html><head>    <me ...

  10. RabbitMQ 生产消息并放入队列

    前提已有 Exchange, Queue, Routing Key, 可以在 web 页面点击鼠标创建, 也可在消费端通过代码自动创建 web 页面配置步骤: https://www.cnblogs. ...