<题目链接>

题目大意:
给定一个无向图,该无向图不含自环,且无重边。现在要你将这个无向图定向,使得不存在任何一条路径长度大于等于2。然后根输入边的顺序,输出构造的有向图。如果构造的边与输入的方向一致,就输出1,方向不一致就输出0。

解题分析:
因为定向后的图不能存在长度大于等于2的路径,所以我们直接对原图进行奇偶染色。如果碰到了奇环,就直接输出"NO",否则就对该图奇偶染色,进行地定向。$col[u]$表示以$u$为起点的边所染的颜色。

#include <bits/stdc++.h>
using namespace std; #define pb push_back
const int N = 2e5+; template<typename T>
inline void read(T&x){
x=;int f=;char c=getchar();
while(c<'' || c>''){ if(c=='-')f=-;c=getchar(); }
while(c>='' && c<=''){ x=x*+c-'';c=getchar(); }
x*=f;
}
int n,m;
int vis[N],col[N];
vector<int>G[N],index;
bool ok=true; void dfs(int u,int cur){
col[u]=cur;
vis[u]=;
for(auto v:G[u]){
if(col[v] && col[u]==col[v]) ok=false; //出现冲突
if(!vis[v]){
if(cur&)dfs(v,);
else dfs(v,);
}
}
}
int main(){
read(n);read(m);
for(int i=;i<=m;i++){
int u,v;read(u);read(v);
G[u].pb(v);G[v].pb(u);
index.pb(u); //记录下第i条边的起点
}
dfs(,);
if(!ok)return puts("NO"),;
puts("YES");
for(auto u:index){
printf("%d",col[u]-);
}puts("");
}

Codeforces 1144F Graph Without Long Directed Paths (DFS染色+构造)的更多相关文章

  1. Codeforces 1144F Graph Without Long Directed Paths DFS染色

    题意: 输入一张有向图,无自回路和重边,判断能否将它变为有向图,使得图中任意一条路径长度都小于2. 如果可以,按照输入的边的顺序输出构造的每条边的方向,构造的边与输入的方向一致就输出1,否则输出0. ...

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

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

  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 ...

  4. 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 ...

  5. 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 ...

  6. CodeForces - 780C Andryusha and Colored Balloons(dfs染色)

    Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, ...

  7. Codeforces 776D:The Door Problem(DFS染色)

    http://codeforces.com/problemset/problem/776/D 题意:有n个门,m个开关,每个门有一个当前的状态(0表示关闭,1表示打开),每个开关控制k个门,但是每个门 ...

  8. [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs)

    [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs) 题面 题意:给你一个无向图,1为起点,求生成树让起点到其他个点的距离最小,距离最小 ...

  9. hdu 5313 Bipartite Graph(dfs染色 或者 并查集)

    Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...

随机推荐

  1. 02--STL序列容器(Vector)

    一:vector容器简介 图片和顺序栈相似,但是vector数组是动态数组,支持随机存取--->但是在尾部添加或者溢出元素非常快速,中间插入删除费时 vector是将元素置于一个动态数组中加以管 ...

  2. Mac 下 Eclipse 添加 Dynamic Web Project 并配置 Tomcat

    最近拿到了一个 Dynamic Web Project,我的 Mac 上的 Eclipse 之前没有过这类型的项目,所以导入之后无法正常运行.下面是我记录的如何配置 Eclipse 使之能够运行 Dy ...

  3. k8s网络之设计与实现

    k8s网络主题系列: 一.k8s网络之设计与实现 二.k8s网络之Flannel网络 三.k8s网络之Calico网络 K8s网络设计与实现是在学习k8s网络过程中总结的内容.在学习k8s网络各种插件 ...

  4. mysql MHA扩展haproxy搭建从库只读负载均衡

    [环境介绍] 系统环境:Red Hat Enterprise Linux 7 + 5.7.18 + MHA version 0.57 MHA架构中从库之间的负责均衡可选择mysql_route& ...

  5. 《C#手札》--基础知识

    第一记 C#基本语法: 语言分隔符: 分号 (;) :语句的分割,表示一句话结束: 花括号 ({}):表示一个代码号,是一个整体,花括号要成对使用: 方括号 ([]): 定义数组和访问数组元素时使用: ...

  6. Java中ArrayList类的用法

    1.什么是ArrayList ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处: 动态的增加和减少元素 实现了ICollection和ILis ...

  7. Fastjson反序列化漏洞研究

    0x01 Brief Description java处理JSON数据有三个比较流行的类库,gson(google维护).jackson.以及今天的主角fastjson,fastjson是阿里巴巴一个 ...

  8. CentOS命令登录MySQL时,报错ERROR 1045 (28000): Access denied for user root@localhost (using password: NO)错误解决方法

    1.停用mysql服务:# /etc/rc.d/init.d/mysqld stop 2.输入命令:# mysqld_safe --user=mysql --skip-grant-tables --s ...

  9. eclipse快速查看工程代码行数

    1.点击要统计的项目或许文件夹,在菜单栏点击Search,然后点击File... 2.选中正则表达式(Regular expression),并在搜索文本框输入\n ;3.在文件名中输入*或*.jav ...

  10. day 22 - 2 面向对象练习

    练习一 在终端输出如下信息 小明,10岁,男,上山去砍柴小明,10岁,男,开车去东北小明,10岁,男,最爱大保健老李,90岁,男,上山去砍柴老李,90岁,男,开车去东北老李,90岁,男,最爱大保健老张 ...