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 ...
随机推荐
- windows2008R2下iis7.5中的url重写(urlrewrite)
以前在windows2003里,使用的是iis6.0,那时常使用的URL重写组件是iisrewrite,当服务器升级到windows2008R2时,IIS成了64位的7.5,结果iisreite组件是 ...
- 【Leetcode_easy】872. Leaf-Similar Trees
problem 872. Leaf-Similar Trees 参考 1. Leetcode_easy_872. Leaf-Similar Trees; 完
- 2019年Java面试题基础系列228道(6)
51.ArrayList 与 LinkedList 的不区别? 最明显的区别是 ArrrayList 底层的数据结构是数组,支持随机访问,而LinkedList 的底层数据结构书链表,不支持随机访问. ...
- 03.linux入门命令
1.linux命令的格式 命令 [选项] [参数] eg: ls ls -l ls -l /home 注: a.选项与参数不一定存在 b.选项用 "-" 来指明 c.命令,选项,参 ...
- Echart timeline 高级用法!!!!
一.前言 在使用 echart timeline 来着图形可视化时,我使用的和官网也不一样,因为我有使用映射关系.比如我将 no 映射到X轴,将 d4 映射到Y轴. 二.参考 echart官网:htt ...
- Python3实现自动查询成绩(主要使用的包有Tesseract-OCR、PIL、execjs、pytesseract、BeautifulSoup)
前提:本文仅作为技术训练,不可利用技术做非法的事. 某考试的成绩查询页面如下:查询成绩需要的数据有准考证号或者身份证.考生姓名.验证码.现在使用python来实现自动查询指定人员的考试成绩(不知道准考 ...
- (3)Spring Boot日志
文章目录 选择哪一种日志框架 日志的使用 Spring Boot 日志使用 选择哪一种日志框架 市面上日志门面有: JCL(Jakart Commons Logging) .SLF4J(Simple ...
- WUSTOJ 1333: Sequential game(Java)
题目链接:1333: Sequential game Description Sequential detector is a very important device in Hardware ex ...
- linux服务器安装oracle
Linux安装Oracle 11g服务器(图文) 应该是最完整的Oracle安装教程了,全程在测试服务器上完成,软件环境:Red Hat Enterprise Linux 6:Oracle 11g ( ...
- Webpack将静态资源拷贝并压缩至输出文件夹
就拿Vue项目来说,比如要将src/assets/js下的静态js文件,直接在public/index.html中引用: 这时候没有在项目中引用,不会经过wenpack的loader,也就不会自己打包 ...