F. Graph Without Long Directed Paths Codeforces Round #550 (Div. 3)
2 seconds
256 megabytes
standard input
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).
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).
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.
6 5
1 5
2 1
1 4
3 1
6 1
YES
10100
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)的更多相关文章
- 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 ...
- CodeForces Round #550 Div.3
http://codeforces.com/contest/1144 A. Diverse Strings A string is called diverse if it contains cons ...
- 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 ...
- Codeforces Round #550 (Div. 3) F. Graph Without Long Directed Paths (二分图染色)
题意:有\(n\)个点和\(m\)条无向边,现在让你给你这\(m\)条边赋方向,但是要满足任意一条边的路径都不能大于\(1\),问是否有满足条件的构造方向,如果有,输出一个二进制串,表示所给的边的方向 ...
- 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 ...
- 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 ...
- 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 ...
- (原创)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 ...
- (原创)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 ...
随机推荐
- 最全的.NET Core跨平台微服务学习资源没有之一
一.Asp.net Core基础 微软英文官网:https://docs.microsoft.com/en-us/aspnet/core/?view=aspnetcore-2.1 .NET Core: ...
- [android] 请求码和结果码的作用
当一个界面中要要开启多个带有返回值的activity时,这个时候,就需要用到请求码和结果码了 调用startActivityForResult(intent,requestCode)方法,开启acti ...
- 模拟获取post数据的方式
使用下面两种方法可以获取post数据 .通过$HTTP_RAW_POST_DATA获取 $post=$GLOBALS['HTTP_RAW_POST_DATA']; 但需要修改相应的php.ini指令 ...
- mybatis全局属性(全局变量)
mybatis全局属性(全局变量):方法1:在 properties 元素体内,使用<property>标签定义的属性方法2:在 properties 元素中, 使用 resource 或 ...
- 查看linux 服务器还剩多少空间
df -hl 或者 df -m
- express 对数据库数据增删改查
接着 http://www.cnblogs.com/cynthia-wuqian/p/6560548.html (1)概念 Schema(属性) :一种以文件形式存储的数 据库模型骨架,不具备数据库的 ...
- 1.Odoo产品分析系列 – 目录
Odoo产品分析 (一) – 一切为零 Odoo产品分析 (二) – 商业板块(1) – 销售(1) Odoo产品分析 (二) – 商业板块(1) – 销售(2) Odoo产品分析 (二) – 商业板 ...
- C# RichTextBox 制作文本编辑器
本文利用一个简单的小例子[文本编辑器],讲解RichTextBox的用法,仅供学习分享使用,如有不足之处,还请指正. Windows窗体中的RichTextBox控件用于显示,输入和操作格式化的文本, ...
- 图说OOP基础(一)
本文用图形化的形式描述OOP的相关知识.对OOP进行系统化的梳理,以便掌握,仅供学习分享使用,如有不足之处,还请指正. 涉及知识点: OOP的相关知识 OOP知识总图 [Object-Orientat ...
- 小程序实践(六):view内部组件排版
涉及知识点: 1.垂直排列,水平排列 2.居中对齐 示例: 1.默认排版 , 一个父组件里面两个子view 显示效果: 2.先给父view设置一个高度和颜色值,用于看效果 3.实现水平排列和垂直排列的 ...