-
Notifications
You must be signed in to change notification settings - Fork 0
/
D.cpp
85 lines (79 loc) · 2.37 KB
/
D.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
#include <bits/stdc++.h>
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifdef QUYNX_DEBUG
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
vector<int> child;
vector<vector<int>> adj;
vector<bool> vis, tvis;
bool containCycle = false;
void dfs(int u) {
if (vis[u]) return;
vis[u] = true;
tvis[u] = true;
if (containCycle) return;
for (auto& v: adj[u]) {
if (!vis[v]) {
dfs(v);
tvis[v] = false;
}
else if (tvis[v]) {
containCycle = true;
return;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
#ifndef QUYNX_DEBUG
cin.tie(nullptr);
#endif
int N, M; cin >> N >> M;
adj.resize(N+1);
child.resize(N+1);
vis.resize(N+1);
unordered_map<int, unordered_map<int, bool>> mp;
for (int i = 0; i < M; ++i) {
int a, b; cin >> a >> b;
if (!mp[a][b]) {
mp[a][b] = true;
adj[a].push_back(b);
++child[b];
}
}
for (int i = 1; i <= N; ++i) {
if (vis[i]) continue;
tvis.assign(N+1, false);
stack<int> st;
dfs(i);
if (containCycle) {
break;
}
}
if (containCycle) {
cout << "-1\n";
}
else {
vector<int> ans;
set<int> s;
for (int i = 1; i <= N; ++i) {
if (child[i] == 0) s.insert(i);
}
while (!s.empty()) {
int u = *s.begin(); s.erase(s.begin());
ans.push_back(u);
for (auto& v: adj[u]) {
--child[v];
if (child[v] == 0) s.insert(v);
}
}
for (int &a: ans) cout << a << " ";
cout << "\n";
}
}