Codeforces 1144F Graph Without Long Directed Paths DFS染色
题意:
输入一张有向图,无自回路和重边,判断能否将它变为有向图,使得图中任意一条路径长度都小于2。
如果可以,按照输入的边的顺序输出构造的每条边的方向,构造的边与输入的方向一致就输出1,否则输出0。
题解:
当我看到“图中任意一条路径长度都小于2”这句话的时候我都懵了,不知道这道题让干啥的。
最后没想到就是句面意思,因为题目中给你了m条无向边,每条无向边长度都是1,那么所有路径长度都小于2,就要这样做:
比如无向图边为:
1 2
2 3
3 4
那么变成有向图就要
1->2
2<-3
3->4
即,这条边的方向要与上一条边相反
那么这个时候我们就可以用DFS染色判断二分图的方法来处理这道题。
染色过程中的处理:
1、不能出现奇环(DFS染色判断二分图不可能出现奇环,因为如果出现奇环,那么它就肯定不是一个二分图,因为如果出现奇环那么同样的颜色的点之间也会连线)

由上图可知,偶环可满足题意。
观察这个图你会发现有一句话很适合它:“构造的有向图中,对于每个顶点,要么所有边都是出,要么所有边都是入。”
那么就可以把它转变成两个颜色0,1染色,0代表所有以该点为起点的边变成有向边时,方向要改变。1代表所有以该点为起点的边变成有向边时,方向不改变。
代码:
1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<algorithm>
5 #include<math.h>
6 #include<vector>
7 #include<queue>
8 #include<stack>
9 #include<map>
10 using namespace std;
11 typedef long long ll;
12 const int maxn=2e5+10;
13 const int INF=0x3f3f3f3f;
14 const double eps=1e-10;
15 const int mod = 1e9+7;
16 #define mt(A,B) memset(A,B,sizeof(A))
17 #define lson l,m,rt*2
18 #define rson m+1,r,rt*2+1
19 #define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
20 map<string,int> r;
21 vector<int> g[maxn], a; //a是输入的第i条边的起点
22 bool vis[maxn];
23 int c[maxn]; //第i个点的颜色
24 bool isok = true;
25
26 void dfs(int x, int cur) { //第x个点,涂cur颜色
27 vis[x] = true;
28 c[x] = cur;
29 for(int i = 0; i < g[x].size(); i++) {
30 if(vis[g[x][i]]) {
31 if(c[x] == c[g[x][i]])
32 isok = false;
33 else
34 continue;
35 }
36 else {
37 if(cur == 0)
38 dfs(g[x][i], 1);
39 else
40 dfs(g[x][i], 0);
41 }
42 }
43 }
44
45 int main() {
46 // freopen("in.txt", "r", stdin);
47 // freopen("out.txt", "w", stdout);
48 int n, m, x, y;
49 scanf("%d%d", &n, &m);
50 for(int i = 1; i <= m; i++) {
51 scanf("%d%d", &x, &y);
52 g[x].push_back(y);
53 g[y].push_back(x);
54 a.push_back(x);
55 }
56 dfs(1, 0);
57 if(!isok)
58 printf("NO\n");
59 else {
60 printf("YES\n");
61 for(int i = 0; i < a.size(); i++) {
62 printf("%d", c[a[i]]);
63 }
64 }
65 return 0;
66 }
哪有错误的话@我一下^_^
Codeforces 1144F Graph Without Long Directed Paths DFS染色的更多相关文章
- Codeforces 1144F Graph Without Long Directed Paths (DFS染色+构造)
<题目链接> 题目大意:给定一个无向图,该无向图不含自环,且无重边.现在要你将这个无向图定向,使得不存在任何一条路径长度大于等于2.然后根输入边的顺序,输出构造的有向图.如果构造的边与输入 ...
- Codeforces Round #550 (Div. 3) F. Graph Without Long Directed Paths (二分图染色)
题意:有\(n\)个点和\(m\)条无向边,现在让你给你这\(m\)条边赋方向,但是要满足任意一条边的路径都不能大于\(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 ...
- 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 ...
- 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 - 780C Andryusha and Colored Balloons(dfs染色)
Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, ...
- Codeforces 776D:The Door Problem(DFS染色)
http://codeforces.com/problemset/problem/776/D 题意:有n个门,m个开关,每个门有一个当前的状态(0表示关闭,1表示打开),每个开关控制k个门,但是每个门 ...
- [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs)
[Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs) 题面 题意:给你一个无向图,1为起点,求生成树让起点到其他个点的距离最小,距离最小 ...
- hdu 5313 Bipartite Graph(dfs染色 或者 并查集)
Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...
随机推荐
- requests基础封装-get/post封装
字符串转化成字典: convert_to_dict.py: import jsonstr1 = '{"grant_type":"client_credential&qu ...
- 【Web】CSS实现鼠标悬停实现显示与隐藏 特效
鼠标悬停实现显示与隐藏特效 简单记录 - 慕课网 Web前端 步骤四:鼠标悬停实现显示与隐藏特效 初步掌握定位的基本使用,以及CSS选择器更高级的运用,完成一个网页中必会的鼠标经过隐藏显示特效. 实现 ...
- 学习rac管理
文章转自:http://blog.itpub.net/7728585/viewspace-752185/ crsctl query crs activeversion 查看版本 ocrconfig - ...
- kubernets之Ingress资源
一 Ingress集中式的kubernets服务转发控制器 1.1 认识Ingress的工作原理 注意:图片来源于kubernets in action一书,如若觉得侵权,请第一时间联系博主进行删 ...
- oracle rac切换到单实例DG后OGG的处理
在RAC切换到单实例DG后,将OGG目录复制过去,在使用alter extract ext_name,begin now的时候报错 2016-04-10 11:27:03 WARNING OGG-01 ...
- 利用sql_tuning_Advisor调优sql
1.赋权给调优用户 grant ADVISOR to xxxxxx; 2.创建调优任务 使用sql_text创建 DECLARE my_task_name VARCHAR2 (30); my_sqlt ...
- Python批量 png转ico
Python 批量 png 转 ico 一.前言: 首先说一下ico文件的作用:ico是windows的图标文件格式,可以用于浏览器首段图标显示,也可以用于Windows软件.我的话一般用来美化文件夹 ...
- CMU数据库(15-445)Lab0-环境搭建
0.写在前面 从这篇文章开始.开一个新坑,记录以下自己做cmu数据库实验的过程,同时会分析一下除了要求我们实现的代码之外的实验自带的一些代码.争取能够对实现一个数据库比较了解.也希望能写进简历.让自己 ...
- 接收的参数为日期类型、controller控制层进行数据保存、进行重定向跳转
目录 1.接收的参数为日期类型 2.controller控制层进行数据保存 3.controller层如何进行重定向跳转(因为默认是请求转发) 4.静态资源的映射 1.接收的参数为日期类型 WEB-I ...
- YOLOv4
@ 目录 YOLO v4源码 CMake安装 CUDA安装 cuDNN安装 OpenCV安装 Cmake编译 VS编译 图像测试 测试结果 YOLOv4是最近开源的一个又快又准确的目标检测器. 首先看 ...