Skip to content

Commit 927b894

Browse files
topological sort
1 parent 3433164 commit 927b894

2 files changed

Lines changed: 152 additions & 0 deletions

File tree

CSES/courseSchedule

22.7 KB
Binary file not shown.

CSES/courseSchedule.cpp

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*
2+
ID: Koral Kulacoglu
3+
TASK: test
4+
LANG: C++
5+
*/
6+
7+
#include <bits/stdc++.h>
8+
9+
using namespace std;
10+
11+
typedef long long ll;
12+
typedef long double ld;
13+
typedef unsigned long long ull;
14+
typedef long double lld;
15+
typedef complex<ld> cd;
16+
17+
typedef pair<int, int> pi;
18+
typedef pair<ll,ll> pll;
19+
typedef pair<ld,ld> pld;
20+
21+
typedef vector<int> vi;
22+
typedef vector<string> vs;
23+
typedef vector<char> vc;
24+
typedef vector<ld> vld;
25+
typedef vector<ll> vll;
26+
typedef vector<pi> vpi;
27+
typedef vector<pll> vpll;
28+
typedef vector<cd> vcd;
29+
30+
template<class T> using pq = priority_queue<T>;
31+
template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
32+
33+
#define FOR(i, a, b) for (int i=a; i<(b); i++)
34+
#define F0R(i, a) for (int i=0; i<(a); i++)
35+
#define FORd(i, a, b) for (int i=(a)-1; i >= b; i--)
36+
#define F0Rd(i, a) for (int i=(a)-1; i >= 0; i--)
37+
#define trav(a, x) for (auto& a : x)
38+
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
39+
40+
#define sz(x) (int)(x).size()
41+
#define all(x) x.begin(), x.end()
42+
#define mp make_pair
43+
#define pb push_back
44+
#define fir first
45+
#define sec second
46+
#define ins insert
47+
#define lbound(a, v) lower_bound(all(a), v)-a.begin()
48+
#define ubound(a, v) upper_bound(all(a), v)-a.begin()
49+
#define gcd(a, b) __gcd(a, b)
50+
#define lcm(a, b) (a*b)/gcd(a, b)
51+
52+
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
53+
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
54+
55+
void __print(int x) {cerr << x;}
56+
void __print(long x) {cerr << x;}
57+
void __print(long long x) {cerr << x;}
58+
void __print(unsigned x) {cerr << x;}
59+
void __print(unsigned long x) {cerr << x;}
60+
void __print(unsigned long long x) {cerr << x;}
61+
void __print(float x) {cerr << x;}
62+
void __print(double x) {cerr << x;}
63+
void __print(long double x) {cerr << x;}
64+
void __print(char x) {cerr << '\'' << x << '\'';}
65+
void __print(const char *x) {cerr << '\"' << x << '\"';}
66+
void __print(const string &x) {cerr << '\"' << x << '\"';}
67+
void __print(bool x) {cerr << (x ? "true" : "false");}
68+
69+
template<typename T, typename V>
70+
void __print(const pair<T, V> &x);
71+
template<typename T>
72+
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
73+
template<typename T, typename V>
74+
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
75+
void _print() {cerr << "]\n";}
76+
template <typename T, typename... V>
77+
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
78+
79+
#ifdef DEBUG
80+
#define dbg(x...) cerr <<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << endl;
81+
#else
82+
#define dbg(x...)
83+
#endif
84+
85+
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
86+
87+
struct custom_hash {
88+
size_t operator()(uint64_t x) const {
89+
x ^= rng();
90+
return x ^ (x >> 16);
91+
}
92+
};
93+
94+
const int MOD = 1000000007;
95+
const char nl = '\n';
96+
const int MX = 100001;
97+
98+
void solve() {
99+
int n, m; cin >> n >> m;
100+
101+
vector<vi> edges(n);
102+
FOR (i, 0, m) {
103+
int a, b; cin >> a >> b; a--; b--;
104+
edges[a].pb(b);
105+
}
106+
107+
vector<int> state(n, 0);
108+
vi res;
109+
110+
function<bool(int)> dfs = [&](int node) {
111+
state[node] = 1;
112+
113+
trav (neigh, edges[node]) {
114+
if (state[neigh] == 1) return false;
115+
if (state[neigh] == 2) continue;
116+
if (!dfs(neigh)) return false;
117+
}
118+
119+
state[node] = 2;
120+
res.pb(node);
121+
122+
return true;
123+
};
124+
125+
FOR (i, 0, n) {
126+
if (state[i] == 0) {
127+
if (!dfs(i)) {
128+
cout << "IMPOSSIBLE" << nl;
129+
return;
130+
}
131+
}
132+
}
133+
134+
reverse(all(res));
135+
136+
trav (i, res) cout << i+1 << ' ';
137+
cout << nl;
138+
}
139+
140+
int main() {
141+
cin.tie(0)->sync_with_stdio(0);
142+
cin.exceptions(cin.failbit);
143+
144+
int T = 1;
145+
// cin >> T;
146+
while(T--) {
147+
solve();
148+
}
149+
150+
return 0;
151+
}
152+

0 commit comments

Comments
 (0)