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# 怎么读取web.config中的配置项

    ConfigurationManager.AppSettings["templateId"]

  2. 从零开始学安全(二十五)●用nmap做端口扫描

    以上是常用的端口扫描 -T 用法 每个级别发包时间  当没有使用T 时默认的使用T3级别发包 半开扫描  先探测主机是否存活 再用-sS  扫描端口   容易造成syn 包攻击 就是利用僵尸主机  进 ...

  3. 从零开始学安全(二十四)●用Nmap发现主机

    首先了解端口状态 常用 都是 open closed  filtered    状态 打开  cmd  输入  nmap -A  192.168.11.11   后面是你的 ip 可以扫描打开的端口状 ...

  4. Android Studio 管理所有程序退出

    import android.app.Activity; import java.util.ArrayList; import java.util.List; public class fa { pu ...

  5. JavaScript 获取按键,并屏蔽系统 Window 事件

    // JavaScript 获取按键,并屏蔽系统 Window 事件 window.document.onkeydown = onkeydown; function onkeydown(event) ...

  6. 【Java每日一题】20170301

    20170228问题解析请点击今日问题下方的“[Java每日一题]20170301”查看(问题解析在公众号首发,公众号ID:weknow619) package Mar2017; public cla ...

  7. 自定义mvc或mtv框架:基于wsgiref的web框架

    把mvc或mtv框架的model数据库,view:html,control逻辑处理,url判别,wsgiref集中在一个文件 代码如下 #!/usr/bin/env python #-*- codin ...

  8. 在 ELK Docker 容器中查看,删除索引

    使用 Docker 搭建好 ELK ( https://www.cnblogs.com/klvchen/p/9268510.html ) 环境后,如需查看 elasticsearch 的索引可采取以下 ...

  9. 洛谷P4590 [TJOI2018]游园会(状压dp LCS)

    题意 题目链接 Sol 这个题可能是TJOI2018唯一的非模板题了吧.. 考虑LCS的转移方程, \[f[i][j] = max(f[i - 1][j], f[i][j - 1], f[i - 1] ...

  10. phpcms中content主要使用的详情列表关系

    从首页(index.html)中点开的内容网页叫单网页(page.html) 从列表(list.html)中点开的网页叫内容页(show.html) 从导航栏的栏目中下拉的列表栏目叫栏目列表页(cat ...