<题目链接>

题目大意:
给定一个无向图,该无向图不含自环,且无重边。现在要你将这个无向图定向,使得不存在任何一条路径长度大于等于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. 微信小程序 开发文档

    官方开发文档: 小程序公众平台 小程序开发者指南 小程序开发者文档 学习资源: 微信:官方入门教程 微信:WeUI 是一套同微信原生视觉体验一致的基础样式库 微信:微信小程序示例 视频: 学堂在线:学 ...

  2. c编译动态库可以编译但是无法导入解决方法

    $(CC) $(CFLAGS) -Wl,--whole-archive ${libusb} -Wl,--no-whole-archive $(lib_objs) $(LFLAGS) -o $(lib) ...

  3. Docker配置镜像加速

    1.获取镜像地址 1.1.阿里云 打开网址:https://cr.console.aliyun.com/#/accelerator        注册.登录.设置密码        然后在页面上可以看 ...

  4. [C#] .NET4.0中使用4.5中的 async/await 功能实现异步

    在.NET Framework 4.5中添加了新的异步操作库,但是在.NET Framework 4.0中却无法使用.这时不免面临着抉择,到底是升级整个解决方案还是不使用呢? 如果你的软件还没发布出去 ...

  5. 小程序 input type number 键盘没有小数点

    在此备注一下: <input type="digit"> number:数字键盘(无小数点)idcard:数字键盘(无小数点.有个 X 键)digit:数字键盘(有小数 ...

  6. VS打开项目 提示Asp.net4.0未在web服务器上注册的解决方案

    Visual Studio2013 下载的Visual Studio2013更新4 http://go.microsoft.com/fwlink/?LinkId=510314有关的Visual Stu ...

  7. 如何局部覆盖element-ui的默认样式

  8. 【汇编语言】Win10 安装 DOXBox0.74

    1.下载package包,有用的只有前两个. 2.解压 masm 压缩文件,我把它解压到D盘. 3.双击 图1 中的 DOXBox 0.74.exe 进行安装. 4.打开 DOXBox0.74 (参考 ...

  9. android shape 圆圈 圆环 圆角

    定义圆圈:比如角标: xml布局文件 <TextView android:id="@+id/item_order_pay_count" android:layout_widt ...

  10. ssh免密登陆:sshpass -p [passwd] ssh -p [port] root@192.168.X.X

    正文: ssh免密登陆:sshpass -p [passwd] ssh -p [port] root@192.168.X.X