1.topology:

 1 #include <fstream>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <cmath>
 7 #include <cstdlib>
 8 
 9 using namespace std;
10 
11 #define EPS 1e-6
12 #define ll long long
13 #define INF 0x7fffffff
14 
15 const int N=1000;
16 const int N2=2000;
17 int fir[N],next_[N2],u[N2],v[N2];
18 int du[N];
19 int n,m;
20 
21 void Topology();
22 
23 int main(){
24     //freopen("D:\\input.in","r",stdin);
25     //freopen("D:\\output.out","w",stdout);
26     scanf("%d%d",&n,&m);
27     for(int i=1;i<=n;i++)   fir[i]=-1,du[i]=0;
28     for(int i=1;i<=m;i++){
29         scanf("%d%d",&u[i],&v[i]);
30         next_[i]=fir[u[i]];
31         fir[u[i]]=i;
32         du[v[i]]++;
33     }
34     Topology();
35     return 0;
36 }
37 void Topology(){
38     int top=-1;
39     for(int i=1;i<=n;i++)
40         if(du[i]==0)    du[i]=top,top=i;
41     for(int i=1;i<=n;i++){
42         if(top==-1){
43             cout<<"A cycle exists in the graph!"<<endl;
44             return;
45         }else{
46             int j=top;
47             top=du[top];
48             cout<<j<<endl;
49             for(int k=fir[j];k!=-1;k=next_[k]){
50                 if(--du[v[k]]==0)
51                     du[v[k]]=top,top=v[k];
52             }
53         }
54     }
55 }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-24
  • 2021-08-27
  • 2021-06-08
  • 2022-12-23
  • 2022-12-23
  • 2022-01-09
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-30
  • 2021-06-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案