forked from AllAlgorithms/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoposort.cpp
More file actions
45 lines (45 loc) · 776 Bytes
/
Copy pathtoposort.cpp
File metadata and controls
45 lines (45 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[10001];
priority_queue<int,vector<int>,greater<int> >q;
queue<int>a;
bool visited[10001];
int indegree[10001];
int main()
{
int n,m,u,v;
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%d%d",&u,&v);
adj[u].push_back(v);
indegree[v]++;
}
for(int i=1;i<=n;i++)
if(!indegree[i])
q.push(i);
int k=0;
while(!q.empty())
{
int u=q.top();
q.pop();
a.push(u);
for(auto it:adj[u])
{
indegree[it]--;
if(!indegree[it])
q.push(it);
}
++k;
}
if(k!=n)
printf("Sandro fails.");
else
{
while(!a.empty())
{
printf("%d ",a.front());
a.pop();
}
}
}