A1146 Topological Order (25 minutes | topological sort, with detailed comments, logic analysis)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_24452475/article/details/100569928

EDITORIAL

  • Ideas analysis
    • V adjacency list is stored to the FIG., The degree of each node and stored in the array indeg.
    • Each one is to judge whether the topological node traversal sequence, the current node is not 0 degree indicates the degree of all nodes is not the extension of topological columns, select a point every time you want it points minus 1
    • According to the decision of whether or not there have been the point 0 to be output if the current number i
      • flag variable to determine whether the output space
      • judge whether the variable is used to determine the topology sequence
  • Simple title, storing, comparing sequence conforms topological properties
    • 25 minutes a title

Test Case

  • 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
    
    output:
    3 4
    

ac Code

  • Reference links
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
        int n, m, a, b, k, flag = 0, indeg[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);
            // 入度加1
            indeg[b]++;
        }
    
        scanf("%d", &k);
        for(int i=0; i<k; i++)
        {
            int judge = 1;
            // 复制数组
            vector<int> tin(indeg,indeg+n+1);
            for(int j=0; j<n; j++)
            {
                scanf("%d", &a);
                // 入度不为0,非拓扑排序
                if(tin[a] != 0) judge = 0;
                // 当前结点,所有子节点入度减1
                for(int it: v[a]) tin[it]--;
            }
            if(judge == 1) continue;
    
            printf("%s%d", flag == 1 ? " " : "", i);
            flag = 1;
        }
        return 0;
    }
    

Knowledge Point Summary

  • Copy the array to a variable container
    int a[4]={0,10,22,3};
    
    std::vector<int> array(a, a + 4);
    

Guess you like

Origin blog.csdn.net/qq_24452475/article/details/100569928