图论_拓扑排序

对一个有向无环图(Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若边(u,v)∈E(G),则u在线性序列中出现在v之前。

通常,这样的线性序列称为满足拓扑次序(Topological Order)的序列,简称拓扑序列。简单的说,由某个集合上的一个偏序得到该集合上的一个全序,这个操作称之为拓扑排序。

执行步骤

由AOV网构造拓扑序列的拓扑排序算法主要是循环执行以下两步,直到不存在入度为0的顶点为止。

(1) 选择一个入度为0的顶点并输出之;

(2) 从网中删除此顶点及所有出边。

循环结束后,若输出的顶点数小于网中的顶点数,则输出“有回路”信息,否则输出的顶点序列就是一种拓扑序列。

例5.7 Leagal or Not (1448)

题目描述:ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many "holy cows" like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to exchange their ideas. When someone has questions, many warm-hearted cows like Lost will come to help. Then the one being helped will call Lost "master", and Lost will have a nice "prentice". By and by, there are many pairs of "master and prentice". But then problem occurs: there are too many masters and too many prentices, how can we know whether it is legal or not?We all know a master can have many prentices and a prentice may have a lot of masters too, it‘s legal. Nevertheless,some cows are not so honest, they hold illegal relationship. Take HH and 3xian for instant, HH is 3xian‘s master and, at the same time, 3xian is HH‘s master,which is quite illegal! To avoid this,please help us to judge whether their relationship is legal or not. Please note that the "master and prentice" relation is transitive. It means that if A is B‘s master ans B is C‘s master, then A is C‘s master.
输入:The input consists of several test cases. For each case, the first line contains two integers, N (members to be tested) and M (relationships to be tested)(2 <= N, M <= 100). Then M lines follow, each contains a pair of (x, y) which means x is y‘s master and y is x‘s prentice. The input is terminated by N = 0.TO MAKE IT SIMPLE, we give every one a number (0, 1, 2,..., N-1). We use their numbers instead of their names.
输出:For each test case, print in one line the judgement of the messy relationship.If it is legal, output "YES", otherwise "NO".
样例输入:
3 2
0 1
1 2
2 2
0 1
1 0
0 0
样例输出:
YES
NO
#include<iostream>
#include<vector>
#include<queue>
#include<stdio.h>
using namespace std;
vector<int> edge[501];
queue<int>Q;

int main(){
    int inDegree[501];
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        if(n==0&&m==0) break;
        for(int i=0;i<n;i++){
            inDegree[i]=0;
            edge[i].clear();
        }
        while(!Q.empty()) Q.pop();
        while(m--){
            int a,b;
            scanf("%d%d",&a,&b);
            inDegree[b]++;
            edge[a].push_back(b);
        }
        for(int i=0;i<n;i++)
            if(inDegree[i]==0)
                Q.push(i);
        int cnt=0;
        while(!Q.empty()){
            int nowP=Q.front();
            Q.pop();
            cnt++;
            for(int i=0;i<edge[nowP].size();i++){
                inDegree[edge[nowP][i]]--;
                if(inDegree[edge[nowP][i]]==0)
                    Q.push(edge[nowP][i]);
            }
        }
        if(cnt==n) puts("YES");
        else puts("NO");
    }
    return 0;
}

该代码所有节点至多进入队列一次,但在每个结点被取出时我们都要遍历以其为弧尾的边,故复杂度为O(N+E),其中N为结点的个数,E为边的个数。

原文地址:https://www.cnblogs.com/exciting/p/9058533.html

时间: 2024-07-29 21:12:40

图论_拓扑排序的相关文章

图论_拓扑排序_练习1(优先队列小顶堆)

priority_queue 基本操作: empty()   如果队列为空,则返回真 pop() 删除对顶元素,删除第一个元素 push()     加入一个元素 size()  返回优先队列中拥有的元素个数 top() 返回优先队列队顶元素,返回优先队列中有最高优先级的元素( #队列中为front() ) back()             返回优先队列队尾元素,返回优先队列中有最低优先级的元素 在默认的优先队列中,优先级高的先出队.在默认的int型中先出队的为较大的数. priority_

【图论】拓扑排序应用

拓扑排序虽是一种排序,但是它跟平时所接触的sort或者qsort不同,排序的意义不同.拓扑排序针对有向无回路图(DAG)而言的,不应用与存在回路的有向图. [图论]广度优先搜索和深度优先搜索 有说到了BFS和DFS,拓扑排序是DFS的一个应用. 有向无回路图能说明事件的发生的先后的顺序.比如穿衣服,士兵排队等.一个具体的例子,有N个物体,下面给出物体的重量比较,比如(a,b)表示a比b重等等,问已给出的条件是否会矛盾?其实就是判断用所给条件所组织的一个图中是否会存在环? 在DFS中加入时间戳,完

数据结构:图论:拓扑排序! 两种方法!

拓扑排序:(1)由偏序变成全序的过程!直观的说,偏序指集合中仅有部分成员之间可比较!而全序指集合中全体成员之间均可比较! (2)将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若边(u,v)∈E(G),则u在线性序列中出现在v之前. 数据结构中进行拓扑排序的方法: 方法一: (1)在有向图中选一个没有前驱的顶点且输出之! (2)从图中删除该顶点和所有以它为尾的弧. (3)重复上述两部,直至全部顶点均已输出,或者当前图中不存在无前驱的顶点为止.后一种情况说明有向图中存在环! 代码: #

b2OJ_1565_[NOI2009]植物大战僵尸_拓扑排序+最大权闭合子图

b2OJ_1565_[NOI2009]植物大战僵尸_拓扑排序+最大权闭合子 题意:n*m个植物,每个植物有分数(可正可负),和能保护植物的位置.只能从右往左吃,并且不能吃正被保护着的,可以一个不吃,求获得的最大分数. 分析:把每个植物向能保护它的植物连边.源点连正权点,负权点连汇点. 考虑在一个环上的植物是吃不到的,我们可以用拓扑排序确定哪些是能吃的. 然后求一遍最大权闭合子图就是答案. 代码: 1 #include <stdio.h> 2 #include <string.h>

数据结构课程笔记_拓扑排序

何谓拓扑排序? 由某个集合上的一个偏序得到该集合上的一个全序,这个操作叫做拓扑排序. 如何得到一个有向图的拓扑排序? 按照有向图给出的次序关系,将图中顶点排成一个线性序列,对于有向图中没有限定次序关系的顶点,则可以人为加上任意的次序关系,由此所得顶点的线性序列称之为拓扑有序序列. 如何进行拓扑排序? 1.从有向图中选取一个没有前驱的顶点: 2.从有向图中删去此顶点以及所有以它为尾的弧: 重复上述两步直至图空,或者图中找不到无前驱的顶点为止,后一种情况说明图中有环. 算法中需要用定量描述代替定性概

图论之拓扑排序 poj 2367 Genealogical tree

题目链接 http://poj.org/problem?id=2367 题意就是给定一系列关系,按这些关系拓扑排序. #include<cstdio> #include<cstring> #include<queue> #include<vector> #include<algorithm> using namespace std; const int maxn=200; int ans; int n; int in[maxn]; //记录入度

[bzoj1565][NOI2009]植物大战僵尸_网络流_拓扑排序

植物大战僵尸 bzoj1565 题目大意:给你一张网格图,上面种着一些植物.你从网格的最右侧开始进攻.每个植物可以对僵尸提供能量或者消耗僵尸的能量.每个植物可以保护一个特定网格内的植物,如果一个植物被保护,那么如果僵尸想吃掉该植物就必须先吃掉保护它的植物.问:僵尸最多能获得多少能量. 注释:1<=N(网格的宽)<=20,1<=M(网格的长)<=30,-20,000<=代价和收益<=20,000. 想法:前置题目([NOI2006]最大获利).这道题和最大获利比较相像,如

luogu P3387 【模板】缩点_拓扑排序

Code: #include <stack> #include <cstdio> #include <algorithm> #include <queue> #include <cstring> #include <map> #define setIO(s) freopen(s".in","r",stdin) using namespace std; namespace Tarjan{ #def

洛谷P1137 旅行计划 拓扑排序 图论

洛谷P1137 旅行计划 拓扑排序   图论在拓扑排序中把每个点能够浏览的点加上去 但是这样会有重复 因为我们要求一个点向前多能浏览的点 所以我们只要求连向这个点中能向前浏览的点数最多的点这一路就是能浏览的最多的点 然后这个点就相当于是拓扑排序中使该点的入度为 0 的那个点用那个点来更新当前点就行了 1 #include <bits/stdc++.h> 2 #define For(i,j,k) for(int i=j;i<=k;i++) 3 #define LL long long 4