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 ...
随机推荐
- HAProxy-1.8.20 根据后缀名转发到后端服务器
global maxconn 100000 chroot /data/soft/haproxy stats socket /var/lib/haproxy/haproxy.sock mode 600 ...
- linux下的命令自动补齐增强
linux 7 下 安装 bash-completion 可以实现命令的参数的自动补齐
- salesforce零基础学习(一百)Mobile Device Tracking
本篇参考: Mobile Device Tracking (salesforce.com) UserDevice | SOAP API Developer Guide | Salesforce Dev ...
- ElasticSearch-命令行客户端操作
1.引言 实际开发中,主要有三种方式可以作为elasticsearch服务的客户端: 第一种,elasticsearch-head插件(可视化工具) 第二种,使用elasticsearch提供的Res ...
- 2021年官网下载各个版本JDK最全版与官网查阅方法
版本说明 1.安装部署JDK (1)环境 (2)官网下载JDK 由于官网的地址会随着时间的修改而更改修改下载地址,现在讲述下通用的界面操作下载JDK,以后JDK收费更严重,估计就只能下载开源的了. A ...
- Win2008 server R2重置登录密码Administrator
1.PE方式修改密码 背景:https://www.cnblogs.com/Crazy-Liu/p/11245730.html 上述连接中的有AD域的机器系统使用哑巴式老毛桃等启动PE出现以下: 原因 ...
- 你可能会问,为什么不直接进入 CLOSED 状态,而要停留在 TIME_WAIT 这个状态?
你可能会问,为什么不直接进入 CLOSED 状态,而要停留在 TIME_WAIT 这个状态? 划重点,2MSL 的时间是从主机 1 接收到 FIN 后发送 ACK 开始计时的:如果在 TIME_WAI ...
- Elasticsearch--ES-Head--docker版安装
1.0ElasticSearch安装 # 拉取ES镜像docker pull elasticsearch:6.5.0 # 设置vm.max_map_count大小sysctl -w vm.max_ma ...
- 洛谷P4180
被教练安排讲题 可恶 这道题我是十月初上课时花了一下午做出来的,当时连倍增都不会,过程比较困难,现在看看还可以 本来想口胡一发,后来想了想可能以后要用,还是写成文章吧 Description 求一棵严 ...
- (四)Springboot以jar包方式启动、关闭、重启脚本
Springboot以jar包方式启动.关闭.重启脚本 1.启动 2.关闭 3.重启 4.脚本授权 SpringBoot: nohup java -jar zlkj-data-server-1.0-S ...