其他分享
首页 > 其他分享> > 2021级ACM班&2022年寒假集训《数据结构》专题12--拓扑排序和关键路径

2021级ACM班&2022年寒假集训《数据结构》专题12--拓扑排序和关键路径

作者:互联网

A - 数据结构实验之图论十:判断给定图是否存在合法拓扑序列

题目链接 https://acm.sdut.edu.cn/onlinejudge3/contests/3990/problems/A

判定拓扑序列模板题

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n,m,flag;
 4 int mapp[1010][1010];
 5 int vis[1010];
 6 int in[1010];
 7 
 8 void topsort()
 9 {
10     flag=n;
11     for(int i=1; i<=n; i++)
12     {
13         for(int j=1; j<=n; j++)
14         {
15             if(in[j]==0 && vis[j]==0)
16             {//找入度为0且没有被访问过的点
17                 flag--;
18                 vis[j]=1;
19                 for(int k=1; k<=n; k++)//遍历寻找与被删除点有关的结点,使其入度-1
20                     if(mapp[j][k])
21                         in[k]--;
22                 break;//每次只找一个度为0的点
23             }
24         }
25     }
26     if(flag) cout<<"NO"<<endl;
27     else cout<<"YES"<<endl;
28 }
29 
30 int main()
31 {
32     while(cin>>n>>m)
33     {
34         memset(mapp,0,sizeof(mapp));
35         memset(vis,0,sizeof(vis));
36         memset(in,0,sizeof(in));
37         while(m--)
38         {
39             int a,b;
40             cin>>a>>b;
41             mapp[a][b]=1;
42             in[b]++;
43         }
44         topsort();
45     }
46     return 0;
47 }

 

标签:vis,--,memset,ACM,int,mapp,2021,sizeof,1010
来源: https://www.cnblogs.com/marswithme/p/16258296.html