前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >拓扑排序实例C++实现

拓扑排序实例C++实现

作者头像
里克贝斯
发布2021-05-21 12:13:07
5650
发布2021-05-21 12:13:07
举报
文章被收录于专栏:图灵技术域图灵技术域

简介

图论中,拓扑排序(Topological Sorting)是一个有向无环图(DAG, Directed Acyclic Graph)的所有顶点的线性序列。且该序列必须满足下面两个条件:

  • 每个顶点出现且只出现一次。
  • 若存在一条从顶点 A 到顶点 B 的路径,那么在序列中顶点 A 出现在顶点 B 的前面。

注意:有向无环图(DAG)才有拓扑排序。拓扑排序有一个或多个结果。

拓扑排序简介

例题1 HDU 3342 Legal or Not

Problem Description

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.

Input

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.

Output

For each test case, print in one line the judgement of the messy relationship.

If it is legal, output “YES”, otherwise “NO”.

Sample Input

  • 3 2
  • 0 1
  • 1 2
  • 2 2
  • 0 1
  • 1 0
  • 0 0

Sample Output

  • YES
  • NO

C++代码

代码语言:javascript
复制
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
//topological order

int main(){
    int n, m;
    while(cin>>n>>m){
        if(n == 0 && m == 0)break;
        int a, b;
        int cnt[100]; //统计每个节点的入度
        vector<int> v[100];
        for(int i = 0; i < m; i++){
            cin>>a>>b;
            v[a].push_back(b); //存入后继
            cnt[b]++; //入度增加
        }
        queue<int> Q; //保存入度为0的节点和队列
        for(int i = 0; i < n; i++){ //找到入度为0的节点存起来
            if(cnt[i] == 0)Q.push(i);
        }
        int times = 0;
        while(!Q.empty()){
            int nowP = Q.front(); //取出第一个入度为0的节点
            Q.pop();
            times++;
            for(int i = 0; i < v[nowP].size(); i++){
                cnt[v[nowP][i]]--; //当前节点所有后继的入度减1
                if(cnt[v[nowP][i]] == 0){
                    Q.push(v[nowP][i]);
                }
            }
        }
        if(times == n)cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}

例题2 PAT 1146 Topological Order (25 分)

This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topological order obtained from the given directed graph? Now you are supposed to write a program to test each of the options.

Input Specification

Each input file contains one test case. For each case, the first line gives two positive integers N (≤ 1,000), the number of vertices in the graph, and M (≤ 10,000), the number of directed edges. Then M lines follow, each gives the start and the end vertices of an edge. The vertices are numbered from 1 to N. After the graph, there is another positive integer K (≤ 100). Then K lines of query follow, each gives a permutation of all the vertices. All the numbers in a line are separated by a space.

Output Specification

Print in a line all the indices of queries which correspond to “NOT a topological order”. The indices start from zero. All the numbers are separated by a space, and there must no extra space at the beginning or the end of the line. It is graranteed that there is at least one answer.

Sample Input

代码语言:javascript
复制
6 8
1 2
1 3
5 2
5 4
2 3
2 6
3 4
6 4
5
1 5 2 3 6 4
5 1 2 6 3 4
5 1 2 3 6 4
5 2 1 6 3 4
1 2 3 4 5 6

Sample Output

3 4

C++代码

代码语言:javascript
复制
#include <iostream> 
#include <vector> 
using namespace std; 
int main() {
    int n, m, a, b, k, flag = 0, in[1010]; 
    vector<int> v[1010]; 
    scanf("%d %d", &n, &m); 
    for (int i = 0; i < m; i++) { 
        scanf("%d %d", &a, &b); 
        v[a].push_back(b); 
        in[b]++; 
    } 
    scanf("%d", &k); 
    for (int i = 0; i < k; i++) { 
        int judge = 1; 
        vector<int> tin(in, in+n+1); 
        for (int j = 0; j < n; j++) { 
            scanf("%d", &a); 
            if (tin[a] != 0) 
                judge = 0; 
            for (int it : v[a]) tin[it]--; 
        } 
        if (judge == 1) continue;
        printf("%s%d", flag == 1 ? " ": "", i); 
        flag = 1;
    } 
    return 0;
}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-05-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简介
  • 例题1 HDU 3342 Legal or Not
  • 例题2 PAT 1146 Topological Order (25 分)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档