Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序
Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序

【Problem Description】
给你一个有向图,给用最少的颜色给每条边染色,要保证不存在一个环中的所有边都是同一个颜色。
【Solution】
用拓扑排序判断图中是否存在环,若图中不存在环,则所有边都是同一种颜色。否则,同一个环中,只要用两种颜色就可以满足题目条件,所以总的颜色数就是两种,对于一个环,一定会存在两种边:1、节点号小的顶点指向节点号大的顶点,2、节点号大的顶点指向节点号小的顶点。所以将这两种不同的边,染成不同的颜色即可。
【Code】
/*
* @Author: Simon
* @Date: 2019-09-16 10:46:37
* @Last Modified by: Simon
* @Last Modified time: 2019-09-16 10:54:35
*/
#include<bits/stdc++.h>
using namespace std;
typedef int Int;
#define int long long
#define INF 0x3f3f3f3f
#define maxn 5005
vector<int>ans,g[maxn];
int deg[maxn];
bool bfs(int n){ //拓扑排序判断是否有环
queue<int>q;
for(int i=1;i<=n;i++){
if(deg[i]==0) q.push(i);
}
while(!q.empty()){
int u=q.front();q.pop();
for(auto v:g[u]){
deg[v]--;
if(deg[v]==0) q.push(v);
}
}
for(int i=1;i<=n;i++) if(deg[i]) return 1;
return 0;
}
Int main(){
#ifndef ONLINE_JUDGE
//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n,m;cin>>n>>m;
for(int i=1;i<=m;i++){
int u,v;cin>>u>>v;
g[u].push_back(v);
if(u>v) ans.push_back(1); //根据边类型染色。
else ans.push_back(2);
deg[v]++;
}
if(bfs(n)){
cout<<"2"<<endl;
for(auto v:ans) cout<<v<<' ';
}else{
cout<<"1"<<endl;
for(int i=1;i<=m;i++) cout<<1<<' ';
}
cout<<endl;
#ifndef ONLINE_JUDGE
cout<<endl;system("pause");
#endif
return 0;
}
Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序的更多相关文章
- 拓扑排序入门详解&&Educational Codeforces Round 72 (Rated for Div. 2)-----D
https://codeforces.com/contest/1217 D:给定一个有向图,给图染色,使图中的环不只由一种颜色构成,输出每一条边的颜色 不成环的边全部用1染色 ps:最后输出需要注意, ...
- Educational Codeforces Round 72 (Rated for Div. 2)
https://www.cnblogs.com/31415926535x/p/11601964.html 这场只做了前四道,,感觉学到的东西也很多,,最后两道数据结构的题没有补... A. Creat ...
- Educational Codeforces Round 72 (Rated for Div. 2) C题
C. The Number Of Good Substrings Problem Description: You are given a binary string s (recall that a ...
- Educational Codeforces Round 72 (Rated for Div. 2) B题
Problem Description: You are fighting with Zmei Gorynich — a ferocious monster from Slavic myths, a ...
- Educational Codeforces Round 72 (Rated for Div. 2) A题
Problem Description: You play your favourite game yet another time. You chose the character you didn ...
- Coloring Edges(有向图环染色)-- Educational Codeforces Round 72 (Rated for Div. 2)
题意:https://codeforc.es/contest/1217/problem/D 给你一个有向图,要求一个循环里不能有相同颜色的边,问你最小要几种颜色染色,怎么染色? 思路: 如果没有环,那 ...
- Educational Codeforces Round 72 (Rated for Div. 2) Solution
传送门 A. Creating a Character 设读入的数据分别为 $a,b,c$ 对于一种合法的分配,设分了 $x$ 给 $a$ 那么有 $a+x>b+(c-x)$,整理得到 $x&g ...
- Educational Codeforces Round 72 (Rated for Div. 2)E(线段树,思维)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;#define BUF_SIZE 100000 ...
- Educational Codeforces Round 72 (Rated for Div. 2)C(暴力)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;char s[200007];int a[20 ...
随机推荐
- .mmap文件如何打开
.mmap是一种思维导图文件的后缀,可以用Xmind软件打开.
- cordon、drain、delete node区别
cordon.drain.delete node区别 主要目的:导致node处于不可调度状态,新创建的pod容器不会调度在node上. cordon将node置为SchedulingDisabled不 ...
- EM算法概念
EM算法是一种非常经典的alternative optimizing算法.alternative optimizing的思想就是对于一个最优化问题,可以计算分为两步或者参数分为两个,就可以随机任意的选 ...
- sublime text3 修改快捷键为eclipse
Preferences -> Key bindings - User [ { "keys": ["shift+enter"], "command ...
- lnmp+tp5安装纪要
1: lnmp : https://lnmp.org/install.html 官网安装帮助 运行命令:wget http://soft.vpser.net/lnmp/lnmp1.6.tar.gz ...
- Python中遍历整个列表及注意点(参考书籍Python编程从入门到实践)
1. 利用for循环遍历整个列表 magicians = ['alice', 'dsvid', 'carolina'] # 遍历整个列表 for magician in magicians: prin ...
- Python重要配置大全
PYTHON 环境安装 安装虚拟环境 pip install virtualenv 卸载包是用:pip uninstall virtualenv 快捷下载安装可用豆瓣源,方法为: pip instal ...
- MySQL8.0新特性实验1
Server层,选项持久化 mysql> show variables like '%max_connections%';+------------------------+-------+| ...
- 使用docker-compose搭建WordPress
今天博主使用typecho各种不爽,索性干掉typecho,使用WordPress 依赖 mysql nginx yml 文件 version: '3' services: nginx: image: ...
- Quartz入门以及相关表达式使用
目的: 1.Quartz简介及应用场景 2.Quartz简单触发器 SimpleTrigger介绍 3.Quartz表达式触发器CronTirgger介绍 4.Quartz中参数传递 5.Spring ...