-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCourse_Schedule.cpp
More file actions
79 lines (67 loc) · 1.16 KB
/
Copy pathCourse_Schedule.cpp
File metadata and controls
79 lines (67 loc) · 1.16 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<bits/stdc++.h>
using namespace std;
using ll= long long;
bool topo(vector<ll>adj[], vector<ll> &ans, ll n)
{
vector<ll>indeg(n+1,0);
for(ll i=0;i<=n; i++)
{
for(auto v : adj[i])
{
indeg[v]++;
}
}
queue<ll>q;
for(ll i=0; i<=n; i++)
{
if(indeg[i]==0)
{
q.push(i);
}
}
while(!q.empty())
{
ll node =q.front();
q.pop();
ans.push_back(node);
for(auto v :adj[node])
{
indeg[v]--;
if(indeg[v]==0)
{
q.push(v);
}
}
}
if(ans.size()-1==n)
{
return true;
}
return false;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll n,m;
cin>>n>>m;
vector<ll>adj[n+1];
for(ll i =0; i<m; i++ )
{
ll u,v;
cin>> u>> v;
adj[u].push_back(v);
}
vector<ll>ans;
if(topo(adj,ans,n))
{
for(ll i=1; i<=n; i++)
{
cout<<ans[i]<<" ";
}
}
else{
cout<<"IMPOSSIBLE";
}
}