拓扑排序入门详解&&Educational Codeforces Round 72 (Rated for Div. 2)-----D
https://codeforces.com/contest/1217
D:给定一个有向图,给图染色,使图中的环不只由一种颜色构成,输出每一条边的颜色
不成环的边全部用1染色
ps:最后输出需要注意,一个环上的序号必然是非全递增的,若有环且有一条边u->v,u的序号<v则输出1否则输出2(反过来也可以)
可以用dfs染色或者用拓扑排序做
顺便复习一下拓扑排序:
拓扑排序是将有向无环图的所有顶点排成一个线性序列,使得图中任意两个顶点u,v若存在u->v,那么序列中u一定在v前面。
了解一个概念: DAG-->有向无环图,一个有向图的任意顶点都无法通过一些有向边回到自身,称之为DAG
算法过程:
(1)定义一个队列,把所有入度为0的结点加入队列(图有n个点)
(2)取队首节点,输出,删除所有从他出发的边,并令这些边的入度-1,若某个顶点的入度减为0,则将其加入队列
(3)反复进行(2)操作,直到队列为空;
注意:若队列为空时入过队的节点数目恰好为n,说明拓扑排序成功,图为DAG,否则图中有环
这位博主写的挺好的 https://blog.csdn.net/qq_41713256/article/details/80805338

#include<bits/stdc++.h>
using namespace std;
inline int read(){
,w=;;
while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
)+(X<<)+(ch^),ch=getchar();
return w?-X:X;
}
/*-------------------------------------------------------------------*/
typedef long long ll;
;
int du[maxn];
ll cnt;
int n,m;
int s;
vector<int>G[maxn];
pair<int,int>ans[maxn];
queue<int>q;
int main()
{
ios_base::sync_with_stdio(); cin.tie(); cout.tie();
//一个环上的序号必然是非全递增的
cin>>n>>m;
;i<=m;i++){
int u,v;
cin>>u>>v;
G[u].push_back(v);
ans[i].first=u;ans[i].second=v;
//入度
du[v]++;
}
;i<=n;i++) ) q.push(i);
while(!q.empty()){
int now=q.front();q.pop();
int len=G[now].size();
;i<len;i++){
du[G[now][i]]--;//该点入度-1
)q.push(G[now][i]);
}
}
;
;i<=n;i++)
){
flag=;//标记是否还存在入度不为0的点
break;
}
//一个环上的序号必然是非全递增的
if(flag){//说明有环
cout<<<<endl;
//for(int i=1;i<=m;i++)cout<<1<<" ";
;i<=m;i++){
if(ans[i].first>ans[i].second)
cout<<<<" ";
<<" ";
}
}
else{ //无环直接输出1
cout<<<<endl;
;i<=m;i++)
cout<<<<" ";
}
;
}
DFS版:
dfs过程中有3个状态1,-1,0,1表示当前搜索路径,-1表示已搜索过且无环路径,0表示还未搜索,可以用前向星存边或者vector存边过
其他需要注意的代码有注释
ps:讲个大家不容易理解的地方,这个DFS的点是不是可以从任意起点搜索?答案:是的,这个对拓扑序列没有影响,可通过代码自由验证。
在有向图DFS过程中(不判环的情况下),我们用栈去存储他的拓扑序列,当一个点没有后驱节点时,这个节点入栈,记住栈的性质(后进先出),然后回溯,这样,越是后面的节点就会被压进栈底
比如说有向边u->v,u是v的前驱,若存在u->v>t,t在拓扑序列中一定在u的后面(拓扑排序的性质),我们从v开始搜索,到t终止(无后驱节点),回溯,入栈,v入栈,回溯。
最后我们搜索u,发现u的后驱节点已标记,所以入栈,退出完成拓扑排序
所以,以DFS回溯+栈的形式就可以很好地完成一次拓扑排序
前向星版本:
#include<bits/stdc++.h>
using namespace std;
inline int read(){
,w=;;
while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
)+(X<<)+(ch^),ch=getchar();
return w?-X:X;
}
/*-------------------------------------------------------------------*/
typedef long long ll;
;
struct node{
int to,next;
}star[];
ll cnt;
int n,m;
int vis[maxn],head[maxn];
void add(int u,int v){
star[cnt].to=v;
star[cnt].next=head[u];
head[u]=cnt++;
}
bool dfs(int idx){
vis[idx]=;
;i=star[i].next){
int v=star[i].to;
)return false;
//若搜索过程中发现回到本次搜索过的点,说明有环,退出
&&!dfs(v))return false;
}
vis[idx]=-;//目前路径上不存在环,所以标记为-1
return true;
}
pair<int,int >p[maxn];
int main()
{
ios_base::sync_with_stdio(); cin.tie(); cout.tie();
//一个环上的序号必然是非全递增的
memset(head,-,sizeof(head));
cin>>n>>m;
;i<=m;i++){
int u,v;
cin>>u>>v;
add(u,v);
p[i].first=u,p[i].second=v;
}
;
;i<=n;++i){
if(!vis[i]){
if(!dfs(i)){
flag=;
break;
}
}
}
if(flag){
cout<<<<endl;
;i<=m;i++){
<<" ";
<<" ";
}
}
else{
cout<<<<endl;
;i<=m;i++){
cout<<<<" ";
}
}
;
}
vector版本:
#include<bits/stdc++.h>
using namespace std;
inline int read(){
,w=;;
while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
)+(X<<)+(ch^),ch=getchar();
return w?-X:X;
}
/*-------------------------------------------------------------------*/
typedef long long ll;
;
struct node{
int to,next;
}star[];
vector<int>edge[maxn];
ll cnt;
int n,m;
int vis[maxn],head[maxn];
bool dfs(int idx){
int len=edge[idx].size();
vis[idx]=;
;i<len;++i){
);
;
}
vis[idx]=-;
;
}
pair<int,int >p[maxn];
int main()
{
ios_base::sync_with_stdio(); cin.tie(); cout.tie();
//一个环上的序号必然是非全递增的
memset(head,-,sizeof(head));
cin>>n>>m;
;i<=m;i++){
int u,v;
cin>>u>>v;
p[i].first=u,p[i].second=v;
edge[u].push_back(v);
}
;
;i<=n;++i){
if(!vis[i]){
if(!dfs(i)){
flag=;
break;
}
}
}
if(flag){
cout<<<<endl;
;i<=m;i++){
<<" ";
<<" ";
}
}
else{
cout<<<<endl;
;i<=m;i++){
cout<<<<" ";
}
}
;
}
对拓扑排序讲得还不错的博客:深入理解拓扑排序(Topological sort) - 简书 https://www.jianshu.com/p/3347f54a3187拓扑排序dfs版+判环_Python_姬小野的博客-CSDN博客 https://blog.csdn.net/wjh2622075127/article/details/82712940
拓扑排序入门详解&&Educational Codeforces Round 72 (Rated for Div. 2)-----D的更多相关文章
- Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序
Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序 [Problem Description] 给你 ...
- 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) 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) 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)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 ...
随机推荐
- Jenkins的制品管理
Jenkins的制品管理 制品是什么? 也叫产出物或工件.制品是软件开发过程中产生的多种有形副产品之一.广义的制品包括用例.UML图.设计文档等.而狭义的制品就可以简单地理解为二进制包.虽然有些代码是 ...
- ijkplayer中遇到的问题汇总
在做音频播放的时候,很多公司使用的是开源的ijkplayer播放器,ijkplayer底层是基于ffmpeg,在某机型上面可能常常遇到各种问题.今天整理了大家在使用ijkplayer中遇到的问题,以及 ...
- Multi-batch TMT reveals false positives, batch effects and missing values(解读人:胡丹丹)
文献名:Multi-batch TMT reveals false positives, batch effects and missing values (多批次TMT定量方法中对假阳性率,批次效应 ...
- 五分钟学Java:一篇文章搞懂spring和springMVC
原创声明 本文作者:黄小斜 转载请务必在文章开头注明出处和作者. 本文思维导图 什么是Spring,为什么你要学习spring? 你第一次接触spring框架是在什么时候?相信很多人和我一样,第一次了 ...
- Python3之turtle的基本用法#Python学习01#
一.turtle基本语法 1.导入turtle 模块import turtle 2.显示箭头turtle.showturtle() 3.写字符串turtle.write("因小米" ...
- 【C++】Strassen算法代码
本文仅代码,无理论解释 实话实说,我觉得这个算法在C系列的语言下,简直垃圾到爆炸--毕竟是一群完全不懂程序数学家对着纸弄出来的,看起来好像非常的有用,实际上耗时是非常爆炸的. 但是<算法导论&g ...
- 2019牛客多校第四场 A meeting
链接:https://ac.nowcoder.com/acm/contest/884/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他语言10485 ...
- 工作流--Activiti
一.工作流 1.工作流介绍 工作流(Workflow),就是通过计算机对业务流程自动化执行管理.它主要解决的是“使在多个参与者 之间按照某种预定义的规则自动进行传递文档.信息或任务的过程,从而实现某 ...
- CF 631C report
Each month Blake gets the report containing main economic indicators of the company "Blake Tech ...
- stm32CubeMx+TrueSTUDIO+uc/os-III移植开发(二)
(三)复制相关文件 (1)继上次的代码生成后会显示如下的图 点击第一个,打开文件夹如下 (2)新建文件夹UCOSIII 在UCOSIII文件夹下,新建如下的文件夹 (3)将uc/os源文件中 Micr ...