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-拓扑排序的更多相关文章

  1. 拓扑排序入门详解&&Educational Codeforces Round 72 (Rated for Div. 2)-----D

    https://codeforces.com/contest/1217 D:给定一个有向图,给图染色,使图中的环不只由一种颜色构成,输出每一条边的颜色 不成环的边全部用1染色 ps:最后输出需要注意, ...

  2. Educational Codeforces Round 72 (Rated for Div. 2)

    https://www.cnblogs.com/31415926535x/p/11601964.html 这场只做了前四道,,感觉学到的东西也很多,,最后两道数据结构的题没有补... A. Creat ...

  3. 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 ...

  4. Educational Codeforces Round 72 (Rated for Div. 2) B题

    Problem Description: You are fighting with Zmei Gorynich — a ferocious monster from Slavic myths, a ...

  5. 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 ...

  6. Coloring Edges(有向图环染色)-- Educational Codeforces Round 72 (Rated for Div. 2)

    题意:https://codeforc.es/contest/1217/problem/D 给你一个有向图,要求一个循环里不能有相同颜色的边,问你最小要几种颜色染色,怎么染色? 思路: 如果没有环,那 ...

  7. 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 ...

  8. Educational Codeforces Round 72 (Rated for Div. 2)E(线段树,思维)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;#define BUF_SIZE 100000 ...

  9. 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 ...

随机推荐

  1. <统计学>统计学开篇

    我们或多多少都接触学习过统计学,可统计学到底是一种什么样的学科呢? 我们将统计学定义为:对数据进行收集.整理.展示.分析和解释,以帮助人们更有效地进行决策的科学. 关于统计学的研究通常分为两类:描述统 ...

  2. [计算机视觉][ARM-Linux开发]OpenCV 3.1下载 ippicv_linux_20151201失败

    安装OpenCV 3.1的过程中要下载ippicv_linux_20151201,由于网络的原因,这个文件经常会下载失败. 解决的办法是手动下载: 先下载 OpenCV 3.1 Download MD ...

  3. EscaperWrapper 转义和反转义

    System.out.println("转义HTML,注意汉字:"+StringEscapeUtils.escapeHtml4("<font>chen磊  x ...

  4. activeMq学习应用

    一.下载 ActiveMQ 5.15.0下载地址 二.安装 解压apache-activemq-5.15.0-bin.zip D:\apache-activemq-5.15.7-bin\apache- ...

  5. 017 Android 获取手机SIM卡序列号和读取联系人

    1.获取手机SIM卡序列号 //5.存储sim卡系列号 //5.1获取sim卡系列号 TelephonyManager manager = (TelephonyManager) getSystemSe ...

  6. python基础学习(八)

    17.嵌套循环 # 嵌套循环 nested loop # 在一个循环中使用另外一个循环 num_list1 = [1, 2, 3, 4] num_list2 = [6, 7, 8, 9] # 组合li ...

  7. Web服务器和Tomcat

    Web服务器常用: WebLogic:是BEA公司的推出的产品,现在已经被oracle收购,是目前应用最广泛的Web服务器,支持JavaEE规范,商用收费,开发者可以免费使用. WebSphere:I ...

  8. 以php中的比较运算符操作整型,浮点型,字符串型,布尔型和空类型

    字符,数字,特殊符号的比较依赖ASC II表,本表原先有127个,后来又扩充了一些,里面包含了奇奇奇怪的符号. ASC II表 https://baike.baidu.com/item/ASCII/3 ...

  9. php异常处理面向对象和面向函数使用

    要使用异常,首先得知道那些部分会产生异常,产生什么类型异常(php常见异常见下方符表),对产生的异常该怎么办. 如果知道程序的那些部分会产生异常,那么就对这一部分使用try关键字: 如果知道了产生异常 ...

  10. python学习-58 configparse模块

    configparse模块 1.生成文件 import configparser # 配置解析模块 config = configparser.ConfigParser() # config = { ...