1146 Topological Order (25 point(s))

思路:

按照给定序列,如果是拓扑序列,那么就输出序号,序号从 0 开始。

1146 Topological Order (25 point(s))

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.

gre.jpg

Example:

#include<iostream>
#include<vector>
#include<unordered_set>
#include<vector>
using namespace std;

struct Graph {
    int Nv;
    int Ne;
    vector<unordered_set<int>> in;
    vector<unordered_set<int>> out;
};

bool isTopological(Graph G, vector<int> &ans)
{
    for(auto &x : ans) {
        if(!G.in[x].empty()) return false;
        for(auto y : G.out[x]) G.in[y].erase(x);
    }
    return true;
}

int main()
{
    int  N, M, K;
    cin >> N >> M;
    Graph G;
    G.Nv = N, G.Ne = M, G.in.resize(N+1), G.out.resize(N+1);
    for(int i = 0 ; i < M; i++) {
        int v1, v2;
        cin >> v1 >> v2;
        G.out[v1].insert(v2), G.in[v2].insert(v1);
    }
    cin >> K;
    vector<int> ans;
    for(int i = 0; i < K; i++) {
        vector<int> tmp(N);
        for(int k = 0; k < N; k++) cin >> tmp[k];
        if(!isTopological(G, tmp)) ans.push_back(i);
    }
    for(auto x = ans.begin(); x != ans.end(); x++)
        cout<< (x == ans.begin() ? "" : " ") << *x ;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值