-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path1679 CouseShedule.cpp
94 lines (77 loc) · 1.49 KB
/
1679 CouseShedule.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// JAI BAJARANG BALI
// manitianajay45
// give me some sunshine, give me some rain, give me another chance to grow up once again....
// sab moh maya hai....
#include <bits/stdc++.h>
using namespace std;
#define ll int
ll n, m;
vector<ll> graph[100005];
vector<bool> visited(100005,false);
bool flag=false;
vector<ll> order;
vector<ll> ans;
vector<ll> visited1(100005,0);
ll x=-1;
void dfs1(ll v){
if(flag){
return ;
}
visited1[v]=1;
// cout<<v<<" "<<endl;
for(auto u:graph[v]){
if(visited1[u]==1){
flag=true;
return ;
}
if(visited1[u]==0){
dfs1(u);
}
}
visited1[v]=2;
}
void dfs(ll v){
visited[v]=true;
// cout<<v<<" ";
for(auto u:graph[v]){
if(!visited[u]){
dfs(u);
}
}
order.push_back(v);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for(ll i=0;i<m;i++){
ll u,v;
cin>>u>>v;
graph[u].push_back(v);
}
for(ll i=1;i<=n;i++){
if(visited1[i]==0){
dfs1(i);
}
if(flag){
break;
}
}
if(flag){
cout<<"IMPOSSIBLE"<<endl;
return 0;
}
for(ll i=1;i<=n;i++){
if(!visited[i]){
dfs(i);
}
}
reverse(order.begin(),order.end());
visited.assign(n+1,false);
for(auto i:order){
cout<<i<<" ";
}
cout<<endl;
return 0;
}