Codeforces Round #550 (Div. 3) F. Graph Without Long Directed Paths
2 seconds
256 megabytes
standard input
standard output
You are given a connected undirected graph consisting of nn vertices and mm edges. There are no self-loops or multiple edges in the given graph.
You have to direct its edges in such a way that the obtained directed graph does not contain any paths of length two or greater (where the length of path is denoted as the number of traversed edges).
The first line contains two integer numbers nn and mm (2≤n≤2⋅1052≤n≤2⋅105, n−1≤m≤2⋅105n−1≤m≤2⋅105) — the number of vertices and edges, respectively.
The following mm lines contain edges: edge ii is given as a pair of vertices uiui, vivi (1≤ui,vi≤n1≤ui,vi≤n, ui≠viui≠vi). There are no multiple edges in the given graph, i. e. for each pair (ui,viui,vi) there are no other pairs (ui,viui,vi) and (vi,uivi,ui) in the list of edges. It is also guaranteed that the given graph is connected (there is a path between any pair of vertex in the given graph).
If it is impossible to direct edges of the given graph in such a way that the obtained directed graph does not contain paths of length at least two, print "NO" in the first line.
Otherwise print "YES" in the first line, and then print any suitable orientation of edges: a binary string (the string consisting only of '0' and '1') of length mm. The ii-th element of this string should be '0' if the ii-th edge of the graph should be directed from uiui to vivi, and '1' otherwise. Edges are numbered in the order they are given in the input.
6 5
1 5
2 1
1 4
3 1
6 1
YES
10100 这个题的题意就是修改一些边的方向让有向图不包含长度为2或更大的任何路径(其中路径长度表示为遍历边的数量),修改的边标记为1,没修改的标记为0,最后按顺序输出所有的边的标记
如下图
思路在下面代码的注释中
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
const int maxn=2e5+;
const int inf=0x3f3f3f3f;
int n,m;
int first[maxn],sign;
int vis[maxn];
int a[maxn][];
struct node
{
int to,w,next;
} edge[maxn<<];
void init()
{
for(int i = ; i <=n; i++)
first[i]=-;
sign=;
}
void add_edge(int u,int v,int w)
{
edge[sign].to=v;
edge[sign].w=w;
edge[sign].next=first[u];
first[u]=sign++;
}
bool flag1=;
void dfs(int x,int flag)///对整个图进行染色1 0 1 0...,
{
vis[x]=flag;
for(int i=first[x]; ~i; i=edge[i].next)
{
int to=edge[i].to;
if(vis[to]==vis[x])///判断是是否有环
{
flag1=;
return ;
}
if(vis[to]==-)
{
dfs(to,flag^);
}
}
return ; }
int main()
{
scanf("%d%d",&n,&m);
init();
for(int i=; i<=m; i++)
{
int u,v;
scanf("%d%d",&u,&v);
add_edge(u,v,);
add_edge(v,u,);
a[i][]=u;
a[i][]=v;
}
memset(vis,-,sizeof(vis));
dfs(,);
if(flag1)
puts("NO");///如果图中有环的话,肯定就会存在dist>=2的点,可以自己画一个三角形看看,就很容易理解了
else
{
puts("YES");
for(int i=; i<=m; i++)
{
if(vis[a[i][]]==)///a[i][0]为第i条边的起点,a[i][1]为第i条边的终点。
///这里我们已经将整个图染色,整个图分为两类,标记为1的点和标记为0的点
///以标记为1的点为起点,出发的边不做修改,即边的标记为0
///以标记为0的点为起点,出发的边改为相反的方向,即边的标记为1,
///这里其实是不难理解的,两个相邻的点必定为1 0或者0 1,1->0->1
///(1出发的边保持原来的方向,0出发的边改为相反的方向)或者(0出发
///的边保持原来的方向,1出发的边改为相反的方向)才能使得整个图的不出现长度为2或者更大的路径
printf("");
else
printf("");
}
} return ;
}
Codeforces Round #550 (Div. 3) F. Graph Without Long Directed Paths的更多相关文章
- Codeforces Round #550 (Div. 3) F. Graph Without Long Directed Paths (二分图染色)
题意:有\(n\)个点和\(m\)条无向边,现在让你给你这\(m\)条边赋方向,但是要满足任意一条边的路径都不能大于\(1\),问是否有满足条件的构造方向,如果有,输出一个二进制串,表示所给的边的方向 ...
- Codeforces Round #496 (Div. 3) F - Berland and the Shortest Paths
F - Berland and the Shortest Paths 思路:还是很好想的,处理出来最短路径图,然后搜k个就好啦. #include<bits/stdc++.h> #defi ...
- 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 ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- CodeForces Round #550 Div.3
http://codeforces.com/contest/1144 A. Diverse Strings A string is called diverse if it contains cons ...
- Codeforces Round #375 (Div. 2) F. st-Spanning Tree 生成树
F. st-Spanning Tree 题目连接: http://codeforces.com/contest/723/problem/F Description You are given an u ...
随机推荐
- 美化ubuntu18.04,并安装搜狗输入法
目录 美化Ubuntu 下载主题和图标文件 下载GNOME3 美化过程 安装输入法 下载并安装搜狗输入法 安装fcitx框架 安装过程 美化Ubuntu 下载主题和图标文件 下载地址:https:// ...
- 数据结构排序算法插入排序Java实现
public class InsertDemo { public static void main(String args[]) { int[] sort ={4,2,1,3,6,5,9,8,10,7 ...
- HTML 5 & checkbox & switch components
HTML 5 & checkbox & switch components <!DOCTYPE html> <html lang="zh-Hans" ...
- call, apply 和 bind 方法
我们知道,每个函数在调用的时候会产生一个执行上下文环境,而这个执行上下文环境中包含了诸如 this 等等信息.即当我们调用函数的时候,内部的 this 已经明确地隐式绑定到了某一个对象上.如果我们希望 ...
- 小米Play获取ROOT权限的经验
小米Play通过什么方式开通了Root权限?大家知道,android机器有Root权限,一旦手机开通了root相关权限,就能够实现更多的功能,举个栗子大家企业的营销部门,使用一些营销应用都需要在Roo ...
- input密码框输入后设置显示为星号或其他样式
预览效果 核心代码 <div class="text-input" :class="right?'textinput-right':''"> < ...
- shiro多Realm第一次调用不生效问题
1. 由于最近自己写的一个项目上用到了多realm的使用,遇到了一个这样的问题: 1. 自己继承了BasicHttpAuthenticationFilter,实现了获取token,然后直接请求api的 ...
- 微信小程序之:wepy(二)
一大堆实例:人家的博客园 代码规范: 1.尽量使用驼峰命名,避免使用$开头,框架内建属性都已$开头,可以使用this直接调用. 2.入口文件.页面.组件后缀都为.wpy. 3.使用ES6语法开发. 4 ...
- Nginx安装及使用
安装 设置安装位置 切换到root下安装:CentOS: #su root Ubuntu: #sudo su 切换文件夹: #cd /usr/local/src/ 安装编译环境 ububtu平台编 ...
- LOJ#2304 泳池
题意:有一个1001 * n的矩形,每个位置有q的概率为1.求紧贴下边界的最大的全1子矩形面积恰为k的概率.n <= 1e9,k <= 1000. 解:只需考虑每一列最下面一个0的位置. ...