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.

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思路  定义一个count数组,保存每个结点对应的指向它的弧的个数,如上图中,count[1]=count[5]=0,count[2]=2,然后对待检验序列进行遍历,对遍历到的每个结点:                    如果当前结点的count数组值不等于0,则返回false                    否则,对与该结点连接的每个结点的count值都减1
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<string.h>
#include<cstdio>
#include<cmath>
using namespace std;

vector<vector<int>> num;

bool check(int a[],int n,vector<int>& cnt)
{
    for(int i=0;i<n;i++)
    {
        int temp=a[i];
        if(cnt[temp]!=0)
            return false;
        for(auto &va:num[temp])
        {
            cnt[va]--;
           // cout<<va<<" "<<cnt[va]<<endl;
        }

    }
    return true;
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    num.resize(n+1);
    vector<int> cnt(n+1,0);
   // cnt=(n+1,0);
    for(int i=0;i<m;i++)
    {
        int start,endL;
        cin>>start>>endL;
        num[start].push_back(endL);
        cnt[endL]++;
    }
    int k;
    cin>>k;
    int a[n];
    vector<int>result;
    vector<int>temp(n+1);
    for(int i=0;i<k;i++)
    {
        for(int j=0;j<n;j++)
        {
            temp[j+1]=cnt[j+1];
            cin>>a[j];
        }
        if(!check(a,n,temp))
            result.push_back(i);
    }
    cout<<result[0];
    for(int i=1;i<result.size();i++)
        cout<<" "<<result[i];
    return 0;
}
 

原文地址:https://www.cnblogs.com/zhanghaijie/p/10323168.html

时间: 2024-10-08 14:42:48

1146 Topological Order (25 分)判断拓扑序列的相关文章

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 in

PAT Advanced 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

PAT 1146 Topological Order[难]

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

PAT 1146 Topological Order

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 in

Topological Sort (25分)

Write a program to find the topological order in a digraph. Format of functions: bool TopSort( LGraph Graph, Vertex TopOrder[] ); where LGraph is defined as the following: typedef struct AdjVNode *PtrToAdjVNode; struct AdjVNode{ Vertex AdjV; PtrToAdj

1110 Complete Binary Tree (25分) 判断一棵二插树是否是完全二叉树

Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (≤) which is the total number of nodes in the tree -- and he

7-31 笛卡尔树 (25分)--判断二叉搜索树,小顶堆

先初步判断是否满足二叉搜索树和小顶堆(针对每一颗最小的子树),如果都满足,进一步判断整棵树是否满足. 1 #include <iostream> 2 #include <string> 3 #include <cstring> 4 using namespace std; 5 typedef struct node 6 { 7 int K1; 8 int K2; 9 int L; 10 int R; 11 }node_arr[1001]; 12 node_arr s;

拓扑排序(Topological Order)UVa10305 Ordering Tasks

2016/5/19 17:39:07 拓扑排序,是对有向无环图(Directed Acylic Graph , DAG )进行的一种操作,这种操作是将DAG中的所有顶点排成一个线性序列,使得图中的任意一对顶点u,v满足如下条件: 若边(u,v)∈E(G),则在最终的线性序列中出现在v的前面 好了,说人话:拓扑排序的应用常常和AOV网相联系,在一个大型的工程中,某些项目不是独立于其他项目的,这意味着这种非独立的项目的完成必须依赖与其它项目的完成而完成,不妨记为u,v,则若边(u,v)∈E(G),代

图结构练习——判断给定图是否存在合法拓扑序列

图结构练习——判断给定图是否存在合法拓扑序列 Time Limit: 1000MS Memory limit: 65536K 题目描述 给定一个有向图,判断该有向图是否存在一个合法的拓扑序列. 输入 输入包含多组,每组格式如下. 第一行包含两个整数n,m,分别代表该有向图的顶点数和边数.(n<=10) 后面m行每行两个整数a b,表示从a到b有一条有向边. 输出 若给定有向图存在合法拓扑序列,则输出YES:否则输出NO. 示例输入 1 0 2 2 1 2 2 1 示例输出 YES NO #inc