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. 【Leetcode_easy】872. Leaf-Similar Trees

    problem 872. Leaf-Similar Trees 参考 1. Leetcode_easy_872. Leaf-Similar Trees; 完

  2. sharp.js中文文档

    高性能node.js图像处理库,使用libvips库来实现. 英文地址:sharp.pixelplumbing.com/ 中文文档地址:yunlzhang.github.io/sharp-docum…

  3. jvm(3)---常用监控工具指令

    1.jps 查看jvm中运行的进程(获取对应pid) 参数:默认-V(大写) -v: 列出jvm启动参数. ---------------------------------- 2.jstack pi ...

  4. Flutter 路由传入中文参数报错无法push问题

    flutter自带路由传递参数和使用第三方库fluro路由传递参数都可以通过一下方式解决问题 String jsonString = json.encode(mapValue); var jsons ...

  5. 查询SQL时间段内执行过的哪些SQL语句

    QS.creation_time, ) , (( ) ) AS statement_text, ST.text, QS.total_worker_time, QS.last_worker_time, ...

  6. laravel 提交空字符串会被转成null解决方法

    在app\Http\Kernel.php文件夹中,注释全局中间件: \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull:: ...

  7. 创建线程的三种方式(Thread、Runnable、Callable)

    方式一:继承Thread类实现多线程: 1. 在Java中负责实现线程功能的类是java.lang.Thread 类. 2. 可以通过创建 Thread的实例来创建新的线程. 3. 每个线程都是通过某 ...

  8. nginx 二级目录高级写法

    nginx二级目录高级配置: location ~ .*\.(html)$ { expires 1m; error_page 404 = /test/index.html; access_log /d ...

  9. PAT(B) 1049 数列的片段和(C)规律

    题目链接:1049 数列的片段和 (20 point(s)) 题目描述 给定一个正数数列,我们可以从中截取任意的连续的几个数,称为片段.例如,给定数列 { 0.1, 0.2, 0.3, 0.4 },我 ...

  10. 机器学习-EM算法-GMM模型笔记

    GMM即高斯混合模型,下面根据EM模型从理论公式推导GMM: 随机变量X是有K个高斯分布混合而成,取各个高斯分布的概率为φ1,φ2,... ,φK,第i个高斯分布的均值为μi,方差为Σi.若观测到随机 ...