• 1146 Topological Order (25 分)


    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.

    gre.jpg

    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:

    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

    拓扑排序的板子题,只要判断该排序是否符合就行。

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 int n,m,k;
     5 vector<int> v[1005], vt;
     6 int val[1005],vals[1005], vis[1005], an[1005];
     7 
     8 int main(){
     9     cin >> n >> m;
    10     int x, y;
    11     for(int i = 0 ; i < m; ++i){
    12         cin >> x >> y;
    13         v[x].push_back(y);
    14         val[y]++;
    15     }
    16     cin >> k;
    17     for(int l = 0; l < k; l++){
    18         for(int i = 1; i <= n; i++){
    19             cin >>an[i];
    20             vals[i] = val[i];
    21         }
    22         memset(vis,0,sizeof(vis));
    23         int pos = 0;
    24         while(pos++ < n){
    25             bool flag = false;
    26             if(vis[an[pos]] == 0&&vals[an[pos]] == 0){
    27                 flag = true;
    28                 vis[an[pos]] = 1;
    29                 for(int j = 0; j < v[an[pos]].size(); j++){
    30                     vals[v[an[pos]][j]]--;
    31                 }
    32             }
    33             if(!flag){
    34                 break;
    35             }
    36         }
    37         if(pos != n+1)
    38             vt.push_back(l);
    39     }
    40     for(int i = 0 ; i < vt.size(); i++)
    41         printf("%d%c", vt[i], i == vt.size()-1?'
    ':' ');
    42 
    43     return 0;
    44 }




  • 相关阅读:
    561. Array Partition I
    448. Find All Numbers Disappeared in an Array
    136. Single Number
    485. Max Consecutive Ones
    463. Island Perimeter
    496. Next Greater Element I
    344. Reverse String
    【.net项目中。。】.net一般处理程序使用方法
    【ExtAspNet学习笔记】ExtAspNet控件库中常见问题
    用VS2010创建三层架构开发模式及三层架构的研究
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/11275863.html
Copyright © 2020-2023  润新知