-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopological-sort.cpp
More file actions
55 lines (53 loc) · 1.07 KB
/
Copy pathtopological-sort.cpp
File metadata and controls
55 lines (53 loc) · 1.07 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
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod (int)(1e9+7)
#define sz(s) (int)s.size()
#define all(v) v.begin(),v.end()
#define input(v) for (auto &i : v) cin >> i;
#define print(v) for (auto &j : v) cout << j << " "; cout << "\n";
void solve()
{
int n,m;
cin>>n>>m;
vector<vector<int>>adj(n+1);
vector<int>indegree(n+1);
vector<int>order;
while(m--)
{
int a,b;
cin>>a>>b;
adj[a].push_back(b);
indegree[b]++;
}
queue<int>q;
for(int i=1;i<=n;i++)
{
if(indegree[i]==0) q.push(i);
}
while(!q.empty())
{
int x = q.front();
q.pop();
order.push_back(x);
for(auto u:adj[x])
{
indegree[u]--;
if(indegree[u]==0) q.push(u);
}
}
if(sz(order)==n)
{
print(order);
}
else cout<<"IMPOSSIBLE\n";
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
int testcases=1;
//cin>>testcases;
while(testcases--) solve();
return 0;
}