Codeforces Round #550 (Div. 3) F. Graph Without Long Directed Paths (二分图染色)

题意:有\(n\)个点和\(m\)条无向边,现在让你给你这\(m\)条边赋方向,但是要满足任意一条边的路径都不能大于\(1\),问是否有满足条件的构造方向,如果有,输出一个二进制串,表示所给的边的方向.
题解:我们先单独拿出\(3\)个点来看,选择一个点,那么与它相连的另外两个点到自己的方向一定是相同的,同理,我们可以推广到任意一个点,与它相连的所有点到它的方向必须都是一样的才行,其实到这儿就不难看出可以用二分图染色来写了,然后打个板子就能很愉快的AC啦~
代码:
int n,m;
int a,b;
vector<int> v[N];
int color[N];
PII edge[N]; bool dfs(int u,int c){
color[u]=c; for(auto w:v[u]){
if(!color[w]){
if(!dfs(w,3-c)) return false;
}
else{
if(color[w]==c) return false;
}
}
return true;
} int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>m; rep(i,1,m){
cin>>a>>b;
edge[i].fi=a;
edge[i].se=b;
v[a].pb(b);
v[b].pb(a);
} bool flag=true; if(!dfs(1,1)){
flag=false;
} if(!flag) cout<<"NO\n";
else{
cout<<"YES\n";
rep(i,1,m){
int x=edge[i].fi;
int y=edge[i].se;
if(color[x]==1 && color[y]==2) cout<<1;
else cout<<0;
}
} return 0;
}
Codeforces Round #550 (Div. 3) F. Graph Without Long Directed Paths (二分图染色)的更多相关文章
- 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 ...
- 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 ...
随机推荐
- ssl证书与java keytool工具
ssl协议 SSL(Secure Sockets Layer 安全套接字协议),及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安全及数据完整性的一种安 ...
- alter column和modify column
5.6中,发现其实alter column 和更改modify column 步骤是一样的 mysql> create table xs(name varchar(12),age int def ...
- 2019 Eclipse的下载与安装教程
Eclipse 是一个开放源代码的.基于Java的可扩展开发平台,可以免费下载使用. 首先我们先进入这个软件的官网:https://www.eclipse.org/ 点击这个网页download下载: ...
- dig的安装和使用
-bash: dig: command not found 解决办法: yum -y install bind-utils dig www.baid bu.com 查看a记录 dig www.ba ...
- linux7下修改主机名的方式
在基于linux发行版的众多linux kernel 3.0以上,包括rhel7,centos7等提供多种修改linux主机名的方式 1.通过编辑/etc/hostname文件 2.命令hostnam ...
- guava eventbus 原理+源码分析
前言: guava提供的eventbus可以很方便的处理一对多的事件问题, 最近正好使用到了,做个小结,使用的demo网上已经很多了,不再赘述,本文主要是源码分析+使用注意点+新老版本eventbus ...
- Py层次递进与文件修改大程序,模块,name与file
层次的递进与返回 #输入quit的时候返回上一阶层,输入exit退出所有的循环 tag=True while tag==True: level1=input('level1:') if level1= ...
- 转 6 jmeter元件的作用域与执行顺序
6 jmeter元件的作用域与执行顺序 元件的作用域 配置元件(config elements)会影响其作用范围内的所有元件.前置处理程序(Per-processors)在其作用范围内的每一个sa ...
- Python+Selenium+Unittest实现PO模式web自动化框架(5)
1.PageObjects目录下的模块 该目录下是存放各页面功能点. 比如:login_page.py模块下就是存放登录页面上的各个功能点的.(登录功能.获取登录失败的提示信息) # --^_^-- ...
- 记一次压测问题定位:connection reset by peer,TCP三次握手后服务端发送RST_网络_c359719435的专栏-CSDN博客 https://blog.csdn.net/c359719435/article/details/80300433
记一次压测问题定位:connection reset by peer,TCP三次握手后服务端发送RST_网络_c359719435的专栏-CSDN博客 https://blog.csdn.net/c3 ...