#include <bits/stdc++.h>
using namespace std;
#define int long long
#define arr array
#define vec vector
#define pii pair<int, int>
#define fir first
#define sec second
const int N = 5e5 + 5;
int n, m;
arr<vec<int>, N> in, out;
arr<int, N> cnt;
arr<bool, N> dlt;
set<pii> ord;
void rmv(int u) {
ord.erase({cnt[u], u});
dlt[u] = true;
for (int v : in[u]) {
if (dlt[v]) continue;
ord.erase({cnt[v], v});
cnt[v]--;
ord.insert({cnt[v], v});
}
}
void prcmp() {
for (int u = 1; u <= n; u++)
cnt[u] = out[u].size(), ord.insert({cnt[u], u});
}
vec<int> ans;
void cmp() {
while (ord.size()) {
assert(ord.begin()->fir == 0);
int u = ord.begin()->sec;
ord.erase(ord.begin());
ans.push_back(u);
rmv(u);
for (int v : in[u])
if (!dlt[v]) rmv(v);
}
}
signed main() {
// freopen("a.in", "r", stdin);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v; cin >> u >> v;
out[u].push_back(v), in[v].push_back(u);
}
prcmp();
cmp();
cout << ans.size() << '\n';
for (int u : ans) cout << u << " ";
cout << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |