题意:判断序列是否为拓扑序列。

思路:理解什么是拓扑排序就好了,简单题。需要注意的地方就是,因为这里要判断多个,每次判断都会改变入度indegree[],因此记得要把indegree[]留个备份。ps.这是第一次考到拓扑序列,我觉得9月8号会考如何求一个拓扑序列,外加求关键路径!?详见:图算法的总结

代码:

#include <cstdio>
#include <vector>
using namespace std;
;
vector<int> adj[maxn];

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    vector<),temp;
    int u,v;
    ;i<m;i++){
        scanf("%d%d",&u,&v);
        adj[u].push_back(v);
        indegree[v]++;
    }
    int query;
    scanf("%d",&query);
    vector<int> ans;
    ;i<query;i++){
        bool flag=true;
        temp=indegree;//初始化,注意每一轮判断前都先赋值,因为每一轮都会改动结点的入度
        ;j<n;j++){
            scanf("%d",&u);
             && flag){
                //遍历结点u的所有出边,把相应的结点入度减1
                ;k<adj[u].size();k++)
                    temp[adj[u][k]]--;
            }else{
                flag=false;
            }
        }
        if(flag==false) ans.push_back(i);
    }
    ;i<ans.size();i++){
        printf("%d",ans[i]);
        ) printf(" ");
    }
    ;
}

1146 Topological Order的更多相关文章

  1. PAT 1146 Topological Order[难]

    1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which o ...

  2. PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)

    1146 Topological Order (25 分)   This is a problem given in the Graduate Entrance Exam in 2018: Which ...

  3. PAT 甲级 1146 Topological Order

    https://pintia.cn/problem-sets/994805342720868352/problems/994805343043829760 This is a problem give ...

  4. [PAT] 1146 Topological Order(25 分)

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  5. PAT 1146 Topological Order

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  6. 1146. Topological Order (25)

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  7. PAT甲级——1146 Topological Order (25分)

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  8. A1146. Topological Order

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  9. PAT A1146 Topological Order (25 分)——拓扑排序,入度

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

随机推荐

  1. Mongodb笔记(二) Index

    版本:mongodb3.4; Index  : 如果mongodb不能使用索引进行排序,就会将数据放入内存中进行排序,而当内存使用超过32MB时,就会报错. 在创建索引时,应确保索引的选择力,避免多余 ...

  2. 状态管理(Vuex、 Flux、Redux、The Elm Architecture)

    1.https://vuex.vuejs.org/zh-cn/intro.html (vuex) 这就是 Vuex 背后的基本思想,借鉴了 Flux.Redux.和 The Elm Architect ...

  3. day5-shelve模块

    一.概述 前面章节我们讲述了json和pickle模块的序列化和反序列化处理,他们有一个不足是在python 3中不能多次dump和load,shelve模块则可以规避这个问题.shelve模块是一个 ...

  4. 【spark】示例:求极值

    我们有这样的数据 1.建立SparkContext读取数据 (1)建立sc (2)通过sc.textFile()读取数据创建Rdd 2.过滤数据 通过filter(line => line.tr ...

  5. android Handler的使用(二)

     Handler的使用(二) 一. Handler与线程的关系 Handler在默认情况下,实际上它和调用它的Activity是处于同一个线程的. 例如在Handler的使用(一)的示例1中,虽然 ...

  6. 正则 去除html标签

    String.prototype.stripHtml=function(){ var re=/<(?:.)*?>/g; // *? 意味着匹配任意数量的重复 return this.rep ...

  7. Solr初步使用

    参考此文:https://blog.csdn.net/frankcheng5143/article/details/71159936

  8. asp.net viewstate 数据过大 导致错误

    当在ViewState中放入dataSet的数据量比较大的时候,当再点页面上的控件时,不会返回到后台,并且会出现如下错误: 或者是上面的12030改成500的错误. --解决方法:Viewstate绑 ...

  9. linux多线程全面解析

      引入:     在传统的Unix模型中,当一个进程需要由另一个实体执行某件事时,该进程派生(fork)一个子进程,让子进程去进行处理.Unix下的大多数网络服务器程序都是这么编写的,即父进程接受连 ...

  10. RPi 3.5寸 电阻屏

    /***************************************************************************** * RPi 3.5寸 电阻屏 * 说明: ...