Codeforces 1144F Graph Without Long Directed Paths (DFS染色+构造)
<题目链接>
题目大意:
给定一个无向图,该无向图不含自环,且无重边。现在要你将这个无向图定向,使得不存在任何一条路径长度大于等于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染色+构造)的更多相关文章
- Codeforces 1144F Graph Without Long Directed Paths DFS染色
题意: 输入一张有向图,无自回路和重边,判断能否将它变为有向图,使得图中任意一条路径长度都小于2. 如果可以,按照输入的边的顺序输出构造的每条边的方向,构造的边与输入的方向一致就输出1,否则输出0. ...
- 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 ...
随机推荐
- 二周工作总结(php方向)
前言:年后回来在忙着换工作,最终拿到了三家的offer,最后权衡去了一家实业公司做oa系统的开发,刚入职做一些技术的总结同时记录自己的技术进步 (一) 用mysql视图实现多个表之间的联查 优点:在实 ...
- react动态路由以及获取动态路由
业务中会遇到点击列表跳转到详情页, 1.在index.js修改我们的跟组件 新建router2的文件 import React from 'react' import { HashRouter as ...
- react 中使用阿里彩色图标
1. 不光要引入css ,还要引入js 2. 在需要引入icon的地方添加 <svg className={styles.menuIcon} aria-hidden="true&quo ...
- asp.netMVC4使用Bootstrap4
使用: 添加: <script src="../../Scripts/jquery-1.7.1.min.js" type="text/javascript" ...
- Python 高级特性之:生成器(generator)和迭代器(Iterator)
前言: 之前学习Python自动化,接触了不少python的学习,对生成器印象尤其深,网上也看了很多介绍,下面主要是这些概念的个人学习整理(如侵删). 正文: 如要创建一个非常大的列表,受到内存限制, ...
- get 和free
1.ngx_pool_t ** ngx_get_pool()//use:getngx_pool_t **pool_address;ngx_pool_t *pool;pool_address = ngx ...
- .Net core----mongodb在插入数据时,会产生时间差的问题
今天在给mongodb插入日期格式的数据时发现,日期时间相差8个小时,原来存储在mongodb中的时间是标准时间UTC +0:00,而中国的时区是+8.00 . 因此在插入的时候需要对时间进行处理: ...
- LeetCode 14.Longest Common Prefix(C++)
最长公共前缀问题,考虑没有或只有一个字符串的情况,然后只需要逐个比对就可以了. class Solution { public: string longestCommonPrefix(vector&l ...
- NOI2004郁闷的出纳员
传送门 题目看起来玄乎,但其实只需要一点点小 trick 就可以了. 我们可以用一个全局的 delta 来维护工资的调整记录 对于每一个新加入的员工,先判断是否低于最低工资下限,如果是,直接踢出,不做 ...
- Celery提交任务出错?
跟着官方的入门教程部署和运行的,为啥报这个错? tasks.py # -*- encoding:UTF-8 -*- from celery import Celery brokers = 'redis ...