-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathCourse Schedule
More file actions
55 lines (51 loc) · 961 Bytes
/
Course Schedule
File metadata and controls
55 lines (51 loc) · 961 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
46
47
48
49
50
51
52
53
54
55
//g++ 5.4.0
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define pii pair<int,int>
#define ff first
#define ss second
#define fast ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
const int nax= 1e5+10;
vector<int> adj[nax];
vector<int> visit(nax,0);
int n,m;
stack<int> s;
void topologicalsort(int i)
{
visit[i] = 1;
for(int j=0;j<adj[i].size();j++)
{
if(!visit[adj[i][j]])
topologicalsort(adj[i][j]);
else if( visit[adj[i][j]] == 1 )
{
cout << "IMPOSSIBLE";
exit(0);
}
}
visit[i] = 2;
s.push(i);
}
int main()
{
fast;
cin>>n>>m;
for(int i=0;i<m;i++)
{
int u,v;
cin>>u>>v;
adj[u].push_back(v);
}
for(int i=1;i<=n;i++)
{
if(!visit[i])
topologicalsort(i);
}
while(!s.empty())
{
cout<<s.top()<<" ";
s.pop();
}
}