• 1146 Topological Order 甲级 xp_xht123


    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:

    1. 6 8
    2. 1 2
    3. 1 3
    4. 5 2
    5. 5 4
    6. 2 3
    7. 2 6
    8. 3 4
    9. 6 4
    10. 6
    11. 5 2 3 6 4 1
    12. 1 5 2 3 6 4
    13. 5 1 2 6 3 4
    14. 5 1 2 3 6 4
    15. 5 2 1 6 3 4
    16. 1 2 3 4 5 6

    Sample Output:

    0 4 5

    所谓拓扑顺序就是做事顺序,也就是做到下一个节点时必须经过上一个节点

    解题思路:第一种方法,记录每个节点度数,根据给定的序列判断是否是拓扑顺序

    第二种方法

    首先,先将给定的集合按照给定顺序存入下标p
    其次,遍历整张图,判断每个有向边的两个节点 a,b 的在p中的位置
    最后,如果发现p[a] > p[b] 就不是拓扑排序
    无需出度和入度的方法

    第一种 

    1. #include
    2. #include
    3. using namespace std;
    4. const int N = 10100;
    5. int n,m;
    6. vector<int>gra[N];
    7. vector<int>pan_cnt_front(N);//统计出现的次数
    8. bool pan_is_toporder(vector<int>&exam)
    9. {
    10. vector<int>pan_cnt_front_copy = pan_cnt_front;
    11. int len = exam.size();
    12. for(int i:exam)
    13. {
    14. if(pan_cnt_front_copy[i]) return false;
    15. for(int j:gra[i]) pan_cnt_front_copy[j]--;
    16. }
    17. return true;
    18. }
    19. int main()
    20. {//拓扑顺序 做事顺序做到下一个节点时必须经过上一个节点
    21. scanf("%d %d",&n,&m);
    22. for(int i=0;i
    23. {
    24. int num1,num2;
    25. cin>>num1>>num2;
    26. gra[num1].push_back(num2);//有向图
    27. pan_cnt_front[num2]++;
    28. }
    29. int k;
    30. cin>>k;
    31. vector<int>res;
    32. for(int j=0;j
    33. {
    34. vector<int>exam(n);
    35. for(int i=0;i
    36. scanf("%d",&exam[i]);
    37. if(!pan_is_toporder(exam)) res.push_back(j);
    38. }
    39. for(int i=0;isize();i++)
    40. {
    41. if(i!=0) cout<<" ";
    42. cout<
    43. }
    44. }

    第二种

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. const int N = 1010 , M = 1e4 + 10;
    6. int n , m;
    7. int p[N];
    8. struct edge
    9. {
    10. int a , b;
    11. }w[M];
    12. int main()
    13. {
    14. cin >> n >> m;
    15. for(int i = 0;i < m;i ++) cin >> w[i].a >> w[i].b;
    16. int k;
    17. cin >> k;
    18. vector<int>v;
    19. for(int i = 0;i < k;i ++)
    20. {
    21. for(int j = 1;j <= n;j ++)
    22. {
    23. int x;
    24. cin >> x;
    25. p[x] = j;
    26. }
    27. bool flag = true;
    28. for(int j = 0;j < m;j ++)
    29. {
    30. if(p[w[j].a] > p[w[j].b])
    31. {
    32. flag = false;
    33. break;
    34. }
    35. }
    36. if(!flag) v.push_back(i);
    37. }
    38. for(int i = 0;i < v.size();i ++)
    39. {
    40. if(i) cout << ' ';
    41. cout << v[i];
    42. }
    43. return 0;
    44. }

  • 相关阅读:
    Sophon KG升级3.1:打破数据间壁垒,解放企业生产力
    presto插件机制揭秘:探索无限可能的数据处理舞台
    GBase 8a MPP集群规划
    开源项目-基于小熊派STM32红外热成像仪
    安全保障基于软件全生命周期-PSP应用
    2023-9-29 JZ33 二叉搜索树的后序遍历序列
    Shell(3)管道与重定向
    Lwip之SNMP协议与实现
    7.区块链系列之hardhat框架部署合约
    MySql创建分区
  • 原文地址:https://blog.csdn.net/xp_xht123/article/details/126334464