// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define pb push_back
#define sz(x) int(x.size())
template<class T> using V = vector<T>;
using vi = V<int>;
int main() {
cin.tie(0)->sync_with_stdio(0);
int N, M; cin >> N >> M;
V<vi> radj(N); vi out(N), dead(N);
set<int> left; for(int i = 0; i < N; i++) left.insert(i);
vi q;
auto rem = [&](int u) {
left.erase(u);
for(auto& v : radj[u]) if (!dead[v]) {
out[v]--;
if (out[v] == 0) q.pb(v);
}
};
for(int i = 0; i < M; i++) {
int u, v; cin >> u >> v; --u, --v;
radj[v].pb(u); out[u]++;
}
for(int i = 0; i < N; i++) if (out[i] == 0) q.pb(i);
vi ans;
while(sz(left)) {
int u;
if (sz(q)) {
u = q.back(); q.pop_back();
} else u = *begin(left);
// cout << u << endl;
assert(dead[u]);
dead[u] = 1;
for(auto& v : radj[u]) dead[v] = 1;
ans.pb(u);
for(auto& v : radj[u]) rem(v);
}
cout << sz(ans) << nl;
for(auto& x : ans) cout << x + 1 << " ";
cout << nl;
exit(0-0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
201 ms |
106724 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
200 ms |
106728 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
1624 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
201 ms |
106724 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |