-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcses_1679.cpp
More file actions
47 lines (45 loc) · 925 Bytes
/
cses_1679.cpp
File metadata and controls
47 lines (45 loc) · 925 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
#include <bits/extc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<bool> visited(n + 1);
vector<int> deg(n + 1), ans;
vector<vector<int>> G(n + 1);
queue<int> q;
while (m--)
{
int u, v;
cin >> u >> v;
deg[v]++;
G[u].emplace_back(v);
}
for (int i = 1; i <= n; i++)
if (deg[i] == 0)
q.emplace(i);
while (q.size())
{
auto u = q.front();
q.pop();
if (visited[u])
continue;
visited[u] = true;
ans.emplace_back(u);
for (auto v : G[u])
{
deg[v]--;
if (deg[v] == 0)
q.emplace(v);
}
}
if (ans.size() == n)
for (auto v : ans)
cout << v << ' ';
else
cout << "IMPOSSIBLE";
cout << '\n';
return 0;
}