<题目链接>

题目大意:
给定一个无向图,该无向图不含自环,且无重边。现在要你将这个无向图定向,使得不存在任何一条路径长度大于等于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. 如何部署WebSphere服务器的开发环境

    WebSphere Liberty 简介 IBM WebSphere Application Server Liberty 或WebSphere Liberty Profile Server(简称 L ...

  2. 远程代理模式-Remote Proxy(Java实现)

    远程代理模式-Remote Proxy 服务端通过rmi将对象注册到远程服务, 客户端使用时, 只需要通过rmi协议获取即可, 只要接口统一, 即可不需要知道内部具体实现, 直接调用使用. Compa ...

  3. StackExchange.Redis 异步超时解决方案

    Timeout awaiting response (outbound=0KiB, inbound=45417KiB, 5891ms elapsed, timeout is 5000ms), comm ...

  4. Redux thunk中间件

    redux-thunk https://github.com/reduxjs/redux-thunk Why Do I Need This? Thunks are the recommended mi ...

  5. spring依赖注入之构造函数注入,set方法注入

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  6. pycharm+selenium搭建环境

    1.在你的python安装目录D:\Python36\Scripts下执行pip install selenium 2.安装完成后最好直接打开python,在下面输入from selenium imp ...

  7. 【干货】提取图片元数据之exiftool

    知识源:UC3Mx: INF.2x网络安全基础:实践方法 课程  第1周.讲座2.计算机取证  常见的法医痕迹  2.2.1.元数据 exiftool是一种查看,更新或删除元数据的工具.是Window ...

  8. [Kubernetes]容器日志的收集与管理

    在开始这篇文章之前,首先要明确一点: Kubernetes 中对容器日志的处理方式,都叫做 cluster-level-logging ,也就是说,这个日志处理系统,与容器, Pod 以及 Node ...

  9. Non-decreasing Array

    Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...

  10. 内核模块中filp->open对文件的读写【转】

    转自:http://guiltcool.blog.chinaunix.net/uid-9950859-id-98917.html 平时网络部分的东西碰的多些,这块一开始还真不知道怎么写,因为肯定和在用 ...